Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

object_path

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object_path

  • 1.0.2
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

Object Path Resolver

Ruby class representing an path through an object graph. Instance of the +ObjectPath+ class can be used to navigate through an object graph and the value(s) in the final step of the path are returned.

Installation

Add this line to your application's Gemfile:

gem 'object_path'

And then execute:

bundle install

Or install it yourself as:

gem install object_path

Usage

Paths are defined as a set of steps. These can be declared in a number of ways :-

  • As a +String+ with each step separated by a '/' delimiter.
  • As an array of +Strings+.
  • As an array of +Symbols+.
  • As an existing +ObjectPaths::ObjectPath+
require 'object_paths/object_path'

path = ObjectPaths::ObjectPath.new('address/street')
path = ObjectPaths::ObjectPath.new(['address', 'street'])
path = ObjectPaths::ObjectPath.new([:address, :street])
path = ObjectPaths::ObjectPath.new(ObjectPaths::ObjectPath.new('address/street'))

The path can then be used to navigate through an object graph :-

require 'object_paths/object_path'

class Address
  attr_accessor :street
end

class Person
  attr_accessor :address
end

address1 = Address.new
address1.street = '123 Main Street'

address2 = Address.new
address2.street = '456 High Street'

person1 = Person.new
person1.address = address1

person2 = Person.new
person2.address = address2

path.resolve(person1) # => '123 Main Street'
path.resolve(person2) # => '456 High Street'

If any step on the path is +Enumerable+ then the following steps with be resolved for each item and an +Array+ of results will be returned. If multiple steps are +Enumerable+ then the process will be repeated for each item. The resulting nested +Array+ will be flattened to a single +Array+. All +Array+ results are also comapected :-

require 'object_paths/object_path'

class Address
  attr_accessor :street
end

class Person
  attr_accessor :addresses
end

address1 = Address.new
address1.street = '123 Main Street'
address2 = Address.new
address2.street = '456 High Street'

person = Person.new
person.addresses = [address1, address2]

path = ObjectPaths::ObjectPath.new('addresses/street')
path.resolve(person) # => ['123 Main Street', '456 High Street']

String & Array Extensions

To ease the creating of new Object Paths the +String+ and +Array+ classes have been extended to allow them to be converted to Object Paths. In both cases the instance method +to_object_path+ can be called to perform the conversions.

require 'object_paths/object_path'

'address/street'.to_object_path
['address', 'street'].to_object_path
%i[address street].to_object_path

FAQs

Package last updated on 18 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc