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

#inspectObject



28
29
30
# File 'lib/typed_params/path.rb', line 28

def inspect
  "#<#{self.class.name}: #{to_s.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



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

def to_s
  keys.map { transform_key(_1) }.reduce(+'') do |s, key|
    next s << key if s.blank?

    case key
    when Integer
      s << "[#{key}]"
    else
      s << ".#{key}"
    end
  end
end