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 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() click to toggle source
# 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() click to toggle source
# 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?() click to toggle source
# File lib/active_set/attribute_instruction.rb, line 20
def case_insensitive?
  return false unless options

  options.include? :i
end
operator() click to toggle source
# 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() click to toggle source
# 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?() 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 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:) click to toggle source
# 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

Private Instance Methods

operator_regex() click to toggle source
# File lib/active_set/attribute_instruction.rb, line 94
def operator_regex
  /\((.*?)\)/
end
options_regex() click to toggle source
# File lib/active_set/attribute_instruction.rb, line 98
def options_regex
  %r{\/(.*?)\/}
end