Class: ParamsReady::Helpers::KeyMap::Mapping::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/params_ready/helpers/key_map.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, names = []) ⇒ Path

Returns a new instance of Path.



12
13
14
15
# File 'lib/params_ready/helpers/key_map.rb', line 12

def initialize(path, names = [])
  @path = path.map(&:to_sym).freeze
  @names = names
end

Instance Attribute Details

#namesObject (readonly)

Returns the value of attribute names.



10
11
12
# File 'lib/params_ready/helpers/key_map.rb', line 10

def names
  @names
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/params_ready/helpers/key_map.rb', line 10

def path
  @path
end

Class Method Details

.dig(name, hash, path) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/params_ready/helpers/key_map.rb', line 29

def self.dig(name, hash, path)
  result = path.reduce(hash) do |current, name|
    next unless Extensions::Hash.acts_as_hash?(current)

    Extensions::Hash.indifferent_access current, name, nil
  end

  return Extensions::Undefined unless Extensions::Hash.acts_as_hash?(result)

  Extensions::Hash.indifferent_access result, name, Extensions::Undefined
end

.store(name, value, hash, path) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/params_ready/helpers/key_map.rb', line 45

def self.store(name, value, hash, path)
  return if value == Extensions::Undefined

  result = path.reduce(hash) do |current, name|
    current[name] ||= {}
    current[name]
  end

  result[name] = value
  result
end

Instance Method Details

#==(other) ⇒ Object

Raises:



62
63
64
65
# File 'lib/params_ready/helpers/key_map.rb', line 62

def ==(other)
  raise ParamsReadyError, "Can't compare path with #{other.class.name}" unless other.is_a? Path
  path == other.path && names == other.names
end

#=~(other) ⇒ Object

Raises:



57
58
59
60
# File 'lib/params_ready/helpers/key_map.rb', line 57

def =~(other)
  raise ParamsReadyError, "Can't match path with #{other.class.name}" unless other.is_a? Path
  path == other.path
end

#add_name(name) ⇒ Object



21
22
23
# File 'lib/params_ready/helpers/key_map.rb', line 21

def add_name(name)
  @names << name
end

#add_names(names) ⇒ Object



17
18
19
# File 'lib/params_ready/helpers/key_map.rb', line 17

def add_names(names)
  names.each{ |name| add_name(name) }
end

#dig(name, hash) ⇒ Object



25
26
27
# File 'lib/params_ready/helpers/key_map.rb', line 25

def dig(name, hash)
  self.class.dig(name, hash, @path)
end

#store(name, value, hash) ⇒ Object



41
42
43
# File 'lib/params_ready/helpers/key_map.rb', line 41

def store(name, value, hash)
  self.class.store(name, value, hash, @path)
end