Class: NaturalBornSlugger::AttributeComposer
- Inherits:
-
Object
- Object
- NaturalBornSlugger::AttributeComposer
- Defined in:
- lib/natural_born_slugger/attribute_composer.rb
Class Method Summary collapse
- .default_options ⇒ Object
- .extract_dependencies_from_options(dependencies = {}) ⇒ Object
- .resolve_dependency(object, dependency) ⇒ Object
Instance Method Summary collapse
-
#add_to(klass) ⇒ Object
Adds composite attribute methods to the class: a getter, and updater method.
- #callback(object, old_value, new_value) ⇒ Object
- #compose_attribute(resolved_dependencies) ⇒ Object
- #evaluate(object) ⇒ Object
-
#initialize(name, options) ⇒ AttributeComposer
constructor
A new instance of AttributeComposer.
Constructor Details
#initialize(name, options) ⇒ AttributeComposer
Returns a new instance of AttributeComposer.
6 7 8 9 10 11 12 13 |
# File 'lib/natural_born_slugger/attribute_composer.rb', line 6 def initialize(name, ) @name = name.to_s @dependencies, = self.class.() .each do |option, value| self.instance_variable_set("@#{option}", value) end raise ConfigurationError.new(self.name, name, "no dependent attributes were specified") if @dependencies.empty? end |
Class Method Details
.default_options ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/natural_born_slugger/attribute_composer.rb', line 15 def self. { require_all: false, join_with: '', callback: false } end |
.extract_dependencies_from_options(dependencies = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/natural_born_slugger/attribute_composer.rb', line 23 def self.(dependencies={}) = .dup .keys.each do |option| if dependencies.has_key? option [option] = dependencies.delete(option) end end [dependencies, ] end |
.resolve_dependency(object, dependency) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/natural_born_slugger/attribute_composer.rb', line 33 def self.resolve_dependency(object, dependency) dependency_chain = dependency.to_s.split('.') dependency_chain.each do |method| object = object.try :send, method break unless object end object end |
Instance Method Details
#add_to(klass) ⇒ Object
Adds composite attribute methods to the class:
a getter, and updater method.
Also adds a setter that either calls the updater
or throws an error, depending on configuration
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/natural_born_slugger/attribute_composer.rb', line 85 def add_to(klass) unless klass.respond_to? :composite_attributes klass.class_attribute(:composite_attributes) end klass.composite_attributes ||= {} klass.composite_attributes[@name] = self klass.send :define_method, "update_#{@name}".to_sym do name = __method__.to_s.gsub 'update_', '' new_value = self.class.composite_attributes[name].evaluate(self) if self.respond_to? "#{name}=".to_sym new_value = self.send "#{name}=".to_sym, new_value end self.instance_variable_set("@#{name}", new_value) #TODO: What is the fate of callbacks? Should the feature be removed? Potential race condition if the value was mutated by the callback #composer.callback(self, old_value, new_value) new_value end # Define instance attribute getter: calls setter klass.send :define_method, @name do name = __method__.to_s if frozen? self.instance_variable_get("@#{name}") else self.send "update_#{name}".to_sym end end end |
#callback(object, old_value, new_value) ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/natural_born_slugger/attribute_composer.rb', line 72 def callback(object, old_value, new_value) if @callback unless old_value == new_value object.instance_exec old_value, new_value, &@callback end end end |
#compose_attribute(resolved_dependencies) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/natural_born_slugger/attribute_composer.rb', line 43 def compose_attribute(resolved_dependencies) resolved_dependencies.map do |resolved_dependency, strategy| case strategy when Symbol # Symbols represent string methods to call on the resolved dependency resolved_dependency.to_s.send(strategy) when String # Strings represent formats to fit the resolved dependency into strategy % resolved_dependency when Regexp # Regexps represent patterns to pull out of the resolved dependency and join resolved_dependency.scan(strategy).join(@join_with) when Proc # Procs should take one parameter and return a string or nil strategy.call(resolved_dependency) else # If no strategy provided, use resolved dependency as is resolved_dependency.to_s end # Remove nil components if `compact_dependencies` is true end.reject{|string| not string or string.empty? }.join(@join_with) end |
#evaluate(object) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/natural_born_slugger/attribute_composer.rb', line 61 def evaluate(object) resolved_dependencies = @dependencies.map do |dependency, strategy| [self.class.resolve_dependency(object, dependency), strategy] end # Check existence of all attribute dependencies if `require_all` is true if @require_all ? resolved_dependencies.map(&:first).all? : true composite = compose_attribute(resolved_dependencies) composite.empty? ? nil : composite end end |