Class: ActiveSet::AttributeInstruction

Inherits:
Object
  • Object
show all
Defined in:
lib/active_set/attribute_instruction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#keypathObject (readonly)

Returns the value of attribute keypath.



6
7
8
# File 'lib/active_set/attribute_instruction.rb', line 6

def keypath
  @keypath
end

#processedObject

Returns the value of attribute processed.



5
6
7
# File 'lib/active_set/attribute_instruction.rb', line 5

def processed
  @processed
end

#valueObject (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_arrayObject



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_hashObject



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

#attributeObject



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(options_regex, '')
  @attribute = attribute
end

#case_insensitive?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
# File 'lib/active_set/attribute_instruction.rb', line 20

def case_insensitive?
  return false unless options

  options.include? :i
end

#operatorObject



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

#optionsObject



42
43
44
45
46
# File 'lib/active_set/attribute_instruction.rb', line 42

def options
  return @options if defined? @options

  @options = @keypath.last[options_regex, 1]&.split('')&.map(&:to_sym)
end

#processed?Boolean

Returns:

  • (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