Commit 201d473

mokha <mokha@cisco.com>
2018-01-26 20:38:29
use an inversion of control container.
1 parent 3d43185
Changed files (6)
app/controllers/scim/v2/users_controller.rb
@@ -38,7 +38,7 @@ module Scim
       end
 
       def repository
-        UserRepository.new
+        $container.resolve(:user_repository)
       end
     end
   end
app/models/user_mapper.rb
@@ -1,6 +1,6 @@
 class UserMapper
-  def initialize(url = Rails.application.routes.url_helpers)
-    @url = url
+  def initialize(url_helpers)
+    @url_helpers = url_helpers
   end
 
   def map_from(user)
@@ -9,7 +9,7 @@ class UserMapper
       x.username = user.email
       x.created_at = user.created_at
       x.updated_at = user.updated_at
-      x.location = @url.scim_v2_users_url(user)
+      x.location = @url_helpers.scim_v2_users_url(user)
       x.version = user.lock_version
       x.emails do |y|
         y.add(user.email, primary: true)
app/models/user_repository.rb
@@ -1,7 +1,7 @@
 class UserRepository
   attr_reader :mapper
 
-  def initialize(mapper = UserMapper.new)
+  def initialize(mapper)
     @mapper = mapper
   end
 
config/initializers/spank.rb
@@ -0,0 +1,10 @@
+$container=Spank::Container.new
+$container.register(:user_repository) do |container|
+  UserRepository.new(container.resolve(:user_mapper))
+end.as_singleton
+$container.register(:user_mapper) do |container|
+  UserMapper.new(container.resolve(:url_helpers))
+end.as_singleton
+$container.register(:url_helpers) do |container|
+  Rails.application.routes.url_helpers
+end
Gemfile
@@ -69,3 +69,4 @@ gem 'jwt'
 gem 'activerecord-session_store'
 gem "email_validator"
 gem 'scim-shady', path: '../scim-shady'
+gem 'spank'
Gemfile.lock
@@ -208,6 +208,7 @@ GEM
     selenium-webdriver (3.6.0)
       childprocess (~> 0.5)
       rubyzip (~> 1.0)
+    spank (1.0.1441140881)
     spring (2.0.2)
       activesupport (>= 4.2)
     spring-watcher-listen (2.0.1)
@@ -285,6 +286,7 @@ DEPENDENCIES
   sass-rails (~> 5.0)
   scim-shady!
   selenium-webdriver
+  spank
   spring
   spring-watcher-listen (~> 2.0.0)
   sqlite3