Module: Injectable::ClassMethods
- Defined in:
- lib/injectable/class_methods.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#argument(name, options = {}) ⇒ Object
Declare the arguments for ‘#call` and initialize the accessors This helps us clean up the code for memoization:.
-
#call(args = {}) ⇒ Object
Use the service with the params declared with ‘.argument’.
-
#dependency(name, options = {}) { ... } ⇒ Object
Declare dependencies for the service.
- #find_required_arguments(hash) ⇒ Object
- #inherited(base) ⇒ Object
- #initialize_with(name, options = {}) ⇒ Object
-
#required_call_arguments ⇒ Object
Get the #call arguments declared with ‘.argument’ with no default.
-
#required_initialize_arguments ⇒ Object
Get the #initialize arguments declared with ‘.initialize_with’ with no default.
-
#simple_class_attribute(*attrs) ⇒ Object
Blatantly stolen from rails’ ActiveSupport.
Class Method Details
.extended(base) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/injectable/class_methods.rb', line 3 def self.extended(base) base.class_eval do simple_class_attribute :dependencies, :call_arguments, :initialize_arguments self.dependencies = DependenciesGraph.new(namespace: base) self.initialize_arguments = {} self.call_arguments = {} end end |
Instance Method Details
#argument(name, options = {}) ⇒ Object
Declare the arguments for ‘#call` and initialize the accessors This helps us clean up the code for memoization:
“‘ private
def player
# player_id exists in the context because we added it as an argument
@player ||= player_query.call(player_id)
end “‘
Every argument is required unless given an optional default value
131 132 133 134 |
# File 'lib/injectable/class_methods.rb', line 131 def argument(name, = {}) call_arguments[name] = attr_accessor name end |
#call(args = {}) ⇒ Object
Use the service with the params declared with ‘.argument’
55 56 57 |
# File 'lib/injectable/class_methods.rb', line 55 def call(args = {}) new.call(args) end |
#dependency(name, options = {}) { ... } ⇒ Object
Declare dependencies for the service
96 97 98 99 100 101 102 103 104 |
# File 'lib/injectable/class_methods.rb', line 96 def dependency(name, = {}, &block) [:block] = block if block_given? [:depends_on] = Array(.fetch(:depends_on, [])) [:name] = name dependencies.add() define_method name do instance_variable_get("@#{name}") || dependencies_proxy.get(name) end end |
#find_required_arguments(hash) ⇒ Object
153 154 155 |
# File 'lib/injectable/class_methods.rb', line 153 def find_required_arguments(hash) hash.reject { |_arg, | .key?(:default) }.keys end |
#inherited(base) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/injectable/class_methods.rb', line 15 def inherited(base) base.class_eval do self.dependencies = dependencies.with_namespace(base) self.initialize_arguments = initialize_arguments.dup self.call_arguments = call_arguments.dup end end |
#initialize_with(name, options = {}) ⇒ Object
136 137 138 139 |
# File 'lib/injectable/class_methods.rb', line 136 def initialize_with(name, = {}) initialize_arguments[name] = attr_accessor name end |
#required_call_arguments ⇒ Object
Get the #call arguments declared with ‘.argument’ with no default
149 150 151 |
# File 'lib/injectable/class_methods.rb', line 149 def required_call_arguments find_required_arguments call_arguments end |
#required_initialize_arguments ⇒ Object
Get the #initialize arguments declared with ‘.initialize_with’ with no default
143 144 145 |
# File 'lib/injectable/class_methods.rb', line 143 def required_initialize_arguments find_required_arguments initialize_arguments end |
#simple_class_attribute(*attrs) ⇒ Object
Blatantly stolen from rails’ ActiveSupport. This is a simplified version of class_attribute
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/injectable/class_methods.rb', line 25 def simple_class_attribute(*attrs) attrs.each do |name| define_singleton_method(name) { nil } ivar = "@#{name}" define_singleton_method("#{name}=") do |val| singleton_class.class_eval do define_method(name) { val } end if singleton_class? class_eval do define_method(name) do if instance_variable_defined? ivar instance_variable_get ivar else singleton_class.send name end end end end val end end end |