Class: Streetlib::Vault::Content

Inherits:
Object
  • Object
show all
Includes:
Mu
Defined in:
lib/streetlib/vault/content.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = {}) ⇒ Content



10
11
12
# File 'lib/streetlib/vault/content.rb', line 10

def initialize(value = {})
  self.value = value
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/streetlib/vault/content.rb', line 14

def method_missing(key)
  super unless is_a_dictionary?

  stringified_key = key.to_s
  unless value.keys.include?(stringified_key)
    raise Error.new("Key #{stringified_key} doesn't exist at the current path.")
  end

  new_value = value[stringified_key]

  # returning the wrapped sub hash to allow nested chaining
  wrap(new_value)
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



8
9
10
# File 'lib/streetlib/vault/content.rb', line 8

def value
  @value
end

Instance Method Details

#[](key) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/streetlib/vault/content.rb', line 51

def [](key)
  if value.kind_of?(Array) && key.kind_of?(Numeric)
    if key >= value.length
      raise Error.new("Can't access index #{key} on a list of #{value.length} items.")
    end

    new_value = value[key]
    wrap(new_value)
  else
    self.send(key)
  end
end

#eachObject



28
29
30
31
32
33
34
# File 'lib/streetlib/vault/content.rb', line 28

def each
  return super unless value.kind_of?(Array)

  value.each do |item|
    yield wrap(item)
  end
end

#has_key?(key) ⇒ Boolean



44
45
46
47
48
49
# File 'lib/streetlib/vault/content.rb', line 44

def has_key?(key)
  return false unless is_a_dictionary?

  stringified_key = key.to_s
  value.keys.include?(stringified_key)
end

#mapObject



36
37
38
39
40
41
42
# File 'lib/streetlib/vault/content.rb', line 36

def map
  return super unless value.kind_of?(Array)

  value.map do |item|
    yield wrap(item)
  end
end

#unwrapObject



64
65
66
# File 'lib/streetlib/vault/content.rb', line 64

def unwrap
  self.value
end