Class: ObjectPaths::ObjectPath

Inherits:
Object
  • Object
show all
Defined in:
lib/object_paths/object_path.rb

Overview

Object Graph Resolver

Represents a path to a value in an object graph. The steps in the path can be attributes, methods, or associations. The path can be resolved to get the value(s) at the end of the path. If any step in the path is nil, the path resolves to nil. If the step results in an array, the next step is applied to each element of the array, and the results are flattened.

When initializing a ObjectPath, the path can be defined as an array of strings or symbols, a string with the steps separated by slashes, or another ObjectPath.

To resolve a ObjectPath, call the resolve method with the root object to start from. The result will be the value at the end of the path, or an array of values if the path resolves to an array.

Example:

class Person
  attr_accessor :name, :address
end

class Address
  attr_accessor :street, :city
end

person = Person.new
person.name = 'John Doe'
address = Address.new
address.street = '123 Main St'
address.city = 'Anytown'
person.address = address

path = ObjectPaths::ObjectPath.new(%i[address city])
path.resolve(person) # => 'Anytown'

path = ObjectPaths::ObjectPath.new('address/city')
path.resolve(person) # => 'Anytown'

path = ObjectPaths::ObjectPath.new('address/name')
path.resolve(person) # => nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_definition) ⇒ ObjectPath

Initializes a ObjectPath with a path definition. The path definition can be an array of strings or symbols, a string with the steps separated by slashes, or another ObjectPath.

Arguments:

+path_definition+ (Array, String, ObjectPath) - The definition of the path.


53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/object_paths/object_path.rb', line 53

def initialize(path_definition)
  case path_definition
  when Array
    @path_steps = path_definition.map(&:to_s)
  when String
    @path_steps = path_definition.split('/')
  when ObjectPath
    @path_steps = path_definition.path_steps
  else
    raise ObjectPaths::Errors::IllegalObjectPathDefinitionType
  end
end

Instance Attribute Details

#path_stepsObject (readonly)

Returns the value of attribute path_steps.



46
47
48
# File 'lib/object_paths/object_path.rb', line 46

def path_steps
  @path_steps
end

Instance Method Details

#human(klass) ⇒ Object

Returns a human readable representation of the path. The steps in the path are converted to human readable attribute names using the human_attribute_name method of the class.

klass (Class) - The class to use for humanizing the attribute names.

Returns (String) - The human readable representation of the path.



85
86
87
88
89
# File 'lib/object_paths/object_path.rb', line 85

def human(klass)
  path_steps.map do |step|
    readable_attribute_name(klass, step)
  end.join(' > ')
end

#resolve(object) ⇒ Object

Resolves the path to the value(s) at the end of the path. If any step in the path is nil, the path resolves to nil. If the step results in an array, the next step is applied to each element of the array, and the results are flattened.

Arguments:

+object+ (Object) - The object to start resolving the path from.

Returns (Object) - The value at the end of the path, or an array of values if the path resolves to an array.



75
76
77
# File 'lib/object_paths/object_path.rb', line 75

def resolve(object)
  resolve_step(object, path_steps)
end