class ActiveSet::AttributeInstruction

Attributes

keypath[R]
processed[RW]
value[R]

Public Class Methods

new(keypath, value) click to toggle source
# 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

Public Instance Methods

associations_array() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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: '==') click to toggle source
# 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?() click to toggle source
# File lib/active_set/attribute_instruction.rb, line 16
def processed?
  @processed
end
resource_for(item:) click to toggle source
# 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:) click to toggle source
# 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

Private Instance Methods

operator_regex() click to toggle source
# File lib/active_set/attribute_instruction.rb, line 70
def operator_regex
  /\((.*?)\)/
end