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
-
#initialize(keypath, value) ⇒ AttributeInstruction
constructor
A new instance of AttributeInstruction.
- #operator(default: '==') ⇒ 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
34 35 36 37 38 |
# File 'lib/active_set/attribute_instruction.rb', line 34 def associations_array return [] unless @keypath.any? @keypath.slice(0, @keypath.length - 1) end |
#associations_hash ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/active_set/attribute_instruction.rb', line 40 def associations_hash return {} unless @keypath.any? associations_array.reverse.reduce({}) do |hash, association| { association => hash } end end |
#attribute ⇒ Object
20 21 22 23 24 25 |
# File 'lib/active_set/attribute_instruction.rb', line 20 def attribute attribute = @keypath.last return attribute.sub(operator_regex, '') if attribute&.match operator_regex attribute end |
#operator(default: '==') ⇒ Object
27 28 29 30 31 32 |
# File 'lib/active_set/attribute_instruction.rb', line 27 def operator(default: '==') attribute = @keypath.last return attribute[operator_regex, 1] if attribute&.match operator_regex default end |
#processed? ⇒ Boolean
16 17 18 |
# File 'lib/active_set/attribute_instruction.rb', line 16 def processed? @processed end |
#resource_for(item:) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/active_set/attribute_instruction.rb', line 56 def resource_for(item:) associations_array.reduce(item) do |resource, association| return nil unless resource.respond_to? association resource.public_send(association) end rescue StandardError # :nocov: nil # :nocov: end |
#value_for(item:) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/active_set/attribute_instruction.rb', line 48 def value_for(item:) resource_for(item: item).public_send(attribute) rescue StandardError # :nocov: nil # :nocov: end |