class Reorm::FieldPath

Public Class Methods

new(*path) click to toggle source
# File lib/reorm/field_path.rb, line 7
def initialize(*path)
  @path = [].concat(path)
end

Public Instance Methods

exists?(document) click to toggle source
# File lib/reorm/field_path.rb, line 25
def exists?(document)
  locate(document)[1]
end
name() click to toggle source
# File lib/reorm/field_path.rb, line 11
def name
  @path.last
end
to_s() click to toggle source
# File lib/reorm/field_path.rb, line 29
def to_s
  @path.join(" -> ")
end
value(document) click to toggle source
# File lib/reorm/field_path.rb, line 15
def value(document)
  locate(document).first
end
value!(document) click to toggle source
# File lib/reorm/field_path.rb, line 19
def value!(document)
  result = locate(document)
  raise Error, "Unable to locate the #{name} (full path: #{self}) field for an instance of the #{document.class.name} class." if !result[1]
  result[0]
end

Private Instance Methods

locate(document) click to toggle source
# File lib/reorm/field_path.rb, line 35
def locate(document)
  result = [nil, false]
  value = document
  @path.each_with_index do |field, index|
    if !value || !value.respond_to?(:include?) || !value.include?(field)
      value = nil
      break
    else
      if index == @path.length - 1
        result = [value_from_object(field, value), true]
      else
        value = value_from_object(field, value)
      end
    end
  end
  result
end
value_from_object(value, object) click to toggle source
# File lib/reorm/field_path.rb, line 53
def value_from_object(value, object)
  if object.respond_to?(value)
    object.send(value)
  else
    object[value]
  end
end