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
#calland 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
135 136 137 138 |
# File 'lib/injectable/class_methods.rb', line 135 def argument(name, = {}) call_arguments[name] = attr_accessor name end |
#call(args = {}) ⇒ Object
Use the service with the params declared with ‘.argument’
59 60 61 |
# File 'lib/injectable/class_methods.rb', line 59 def call(args = {}) new.call(args) end |
#dependency(name, options = {}) { ... } ⇒ Object
Declare dependencies for the service
100 101 102 103 104 105 106 107 108 |
# File 'lib/injectable/class_methods.rb', line 100 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
157 158 159 |
# File 'lib/injectable/class_methods.rb', line 157 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
140 141 142 143 |
# File 'lib/injectable/class_methods.rb', line 140 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
153 154 155 |
# File 'lib/injectable/class_methods.rb', line 153 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
147 148 149 |
# File 'lib/injectable/class_methods.rb', line 147 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 51 52 53 54 |
# 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 the instance reader immediately when this attribute is declared # so instances always respond to the reader even if the singleton # class value has not been set yet. 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 define_singleton_method("#{name}=") do |val| singleton_class.class_eval do define_method(name) { val } end val end end end |