Class: TypedParams::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/typed_params/path.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*keys, casing: TypedParams.config.path_transform) ⇒ Path

Returns a new instance of Path.



7
8
9
10
# File 'lib/typed_params/path.rb', line 7

def initialize(*keys, casing: TypedParams.config.path_transform)
  @casing = casing
  @keys   = keys
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



5
6
7
# File 'lib/typed_params/path.rb', line 5

def keys
  @keys
end

Instance Method Details

#+(other) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
# File 'lib/typed_params/path.rb', line 15

def +(other)
  raise ArgumentError, 'must be a Path object or nil' unless other in Path | nil

  return self if
    other.nil?

  Path.new(*keys, *other.keys, casing:)
end

#inspectObject



36
37
38
# File 'lib/typed_params/path.rb', line 36

def inspect
  "#<#{self.class.name}: #{to_s.inspect} keys=#{keys.inspect} casing=#{casing.inspect}>"
end

#to_dot_notationObject



13
# File 'lib/typed_params/path.rb', line 13

def to_dot_notation = keys.map { transform_key(_1) }.join('.')

#to_json_pointerObject



12
# File 'lib/typed_params/path.rb', line 12

def to_json_pointer = '/' + keys.map { transform_key(_1) }.join('/')

#to_sObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/typed_params/path.rb', line 24

def to_s
  keys.map { transform_key(_1) }.reduce(+'') do |s, key|
    case key
    when Integer
      s << "[#{key}]"
    else
      s << '.' unless s.blank?
      s << key.to_s
    end
  end
end