Class: ActiveSet::AttributeInstruction
- Inherits:
-
Object
- Object
- ActiveSet::AttributeInstruction
- Defined in:
- lib/active_set/attribute_instruction.rb
Instance Attribute Summary collapse
-
#keypath ⇒ Object
readonly
Returns the value of attribute keypath.
-
#processed ⇒ Object
Returns the value of attribute processed.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #associations_array ⇒ Object
- #associations_hash ⇒ Object
- #attribute ⇒ Object
- #case_insensitive? ⇒ Boolean
-
#initialize(keypath, value) ⇒ AttributeInstruction
constructor
A new instance of AttributeInstruction.
- #operator ⇒ Object
- #options ⇒ Object
- #processed? ⇒ Boolean
- #resource_for(item:) ⇒ Object
- #value_for(item:) ⇒ Object
Constructor Details
#initialize(keypath, value) ⇒ AttributeInstruction
Returns a new instance of AttributeInstruction.
8 9 10 11 12 13 14 |
# File 'lib/active_set/attribute_instruction.rb', line 8 def initialize(keypath, value) # `keypath` can be an Array (e.g. [:parent, :child, :grandchild, :attribute]) # or a String (e.g. 'parent.child.grandchild.attribute') @keypath = Array(keypath).map(&:to_s).flat_map { |x| x.split('.') } @value = value @processed = false end |
Instance Attribute Details
#keypath ⇒ Object (readonly)
Returns the value of attribute keypath.
6 7 8 |
# File 'lib/active_set/attribute_instruction.rb', line 6 def keypath @keypath end |
#processed ⇒ Object
Returns the value of attribute processed.
5 6 7 |
# File 'lib/active_set/attribute_instruction.rb', line 5 def processed @processed end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
6 7 8 |
# File 'lib/active_set/attribute_instruction.rb', line 6 def value @value end |
Instance Method Details
#associations_array ⇒ Object
48 49 50 51 52 53 |
# File 'lib/active_set/attribute_instruction.rb', line 48 def associations_array return @associations_array if defined? @associations_array return [] unless @keypath.any? @associations_array = @keypath.slice(0, @keypath.length - 1) end |
#associations_hash ⇒ Object
55 56 57 58 59 60 61 62 |
# File 'lib/active_set/attribute_instruction.rb', line 55 def associations_hash return @associations_hash if defined? @associations_hash return {} unless @keypath.any? @associations_hash = associations_array.reverse.reduce({}) do |hash, association| { association => hash } end end |
#attribute ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/active_set/attribute_instruction.rb', line 26 def attribute return @attribute if defined? @attribute attribute = @keypath.last attribute = attribute&.sub(operator_regex, '') attribute = attribute&.sub(, '') @attribute = attribute end |
#case_insensitive? ⇒ Boolean
20 21 22 23 24 |
# File 'lib/active_set/attribute_instruction.rb', line 20 def case_insensitive? return false unless .include? :i end |
#operator ⇒ Object
35 36 37 38 39 40 |
# File 'lib/active_set/attribute_instruction.rb', line 35 def operator return @operator if defined? @operator attribute_instruction = @keypath.last @operator = attribute_instruction[operator_regex, 1]&.to_sym end |
#options ⇒ Object
42 43 44 45 46 |
# File 'lib/active_set/attribute_instruction.rb', line 42 def return @options if defined? @options @options = @keypath.last[, 1]&.split('')&.map(&:to_sym) end |
#processed? ⇒ Boolean
16 17 18 |
# File 'lib/active_set/attribute_instruction.rb', line 16 def processed? @processed end |
#resource_for(item:) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/active_set/attribute_instruction.rb', line 76 def resource_for(item:) @resources_for ||= Hash.new do |h, key| h[key] = associations_array.reduce(key) do |resource, association| break nil unless resource.respond_to? association resource.public_send(association) end end @resources_for[item] rescue StandardError # :nocov: nil # :nocov: end |
#value_for(item:) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/active_set/attribute_instruction.rb', line 64 def value_for(item:) @values_for ||= Hash.new do |h, key| h[key] = resource_for(item: key).public_send(attribute) end @values_for[item] rescue StandardError # :nocov: nil # :nocov: end |