Class: Nugrant::Bag

Inherits:
Hash
  • Object
show all
Defined in:
lib/nugrant/bag.rb

Instance Method Summary collapse

Constructor Details

#initialize(elements = {}, config = {}) ⇒ Bag

Create a new Bag object which holds key/value pairs. The Bag object inherits from the Hash object, the main differences with a normal Hash are indifferent access (symbol or string) and method access (via method call).

Hash objects in the map are converted to Bag. This ensure proper nesting of functionality.

| Arguments

* `elements`
  The initial elements the bag should be built with it.'
  Must be an object responding to `each` and accepting
  a block with two arguments: `key, value`. Defaults to
  the empty hash.

* `config`
  A Nugrant::Config object or hash passed to Nugrant::Config
  constructor. Used for `key_error` handler.


26
27
28
29
30
31
32
33
34
# File 'lib/nugrant/bag.rb', line 26

def initialize(elements = {}, config = {})
  super()

  @__config = Config::convert(config)

  (elements || {}).each do |key, value|
    self[key] = value
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



40
41
42
# File 'lib/nugrant/bag.rb', line 40

def method_missing(method, *args, &block)
  self[method]
end

Instance Method Details

#[](input) ⇒ Object

Hash Overridden Methods (for string & symbol indifferent access)



60
61
62
63
64
65
# File 'lib/nugrant/bag.rb', line 60

def [](input)
  key = __convert_key(input)
  return @__config.key_error.call(key) if not key?(key)

  super(key)
end

#[]=(input, value) ⇒ Object



67
68
69
# File 'lib/nugrant/bag.rb', line 67

def []=(input, value)
  super(__convert_key(input), __convert_value(value))
end

#assoc(key) ⇒ Object



71
72
73
# File 'lib/nugrant/bag.rb', line 71

def assoc(key)
  super(__convert_key(key))
end

#config=(config) ⇒ Object



36
37
38
# File 'lib/nugrant/bag.rb', line 36

def config=(config)
  @__config = Config::convert(config)
end

#count(item) ⇒ Object

Enumerable Overridden Methods (for string & symbol indifferent access)



48
49
50
# File 'lib/nugrant/bag.rb', line 48

def count(item)
  super(__try_convert_item(item))
end

#delete(key) ⇒ Object



75
76
77
# File 'lib/nugrant/bag.rb', line 75

def delete(key)
  super(__convert_key(key))
end

#dupObject



99
100
101
# File 'lib/nugrant/bag.rb', line 99

def dup()
  self.class.new(self, @__config.dup())
end

#fetch(key, default = nil) ⇒ Object



79
80
81
# File 'lib/nugrant/bag.rb', line 79

def fetch(key, default = nil)
  super(__convert_key(key), default)
end

#find_index(item = nil, &block) ⇒ Object



52
53
54
# File 'lib/nugrant/bag.rb', line 52

def find_index(item = nil, &block)
  block_given? ? super(&block) : super(__try_convert_item(item))
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/nugrant/bag.rb', line 83

def has_key?(key)
  super(__convert_key(key))
end

#include?(item) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/nugrant/bag.rb', line 87

def include?(item)
  super(__try_convert_item(item))
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/nugrant/bag.rb', line 91

def key?(key)
  super(__convert_key(key))
end

#member?(item) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/nugrant/bag.rb', line 95

def member?(item)
  super(__try_convert_item(item))
end

#merge(other, options = {}) ⇒ Object



103
104
105
106
# File 'lib/nugrant/bag.rb', line 103

def merge(other, options = {})
  result = dup()
  result.merge!(other)
end

#merge!(other, options = {}) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/nugrant/bag.rb', line 108

def merge!(other, options = {})
  other.each do |key, value|
    current = __get(key)
    case
      when current == nil
        self[key] = value

      when current.kind_of?(Hash) && value.kind_of?(Hash)
        current.merge!(value, options)

      when current.kind_of?(Array) && value.kind_of?(Array)
        strategy = options[:array_merge_strategy]
        if not Nugrant::Config.supported_array_merge_strategy(strategy)
          strategy = @__config.array_merge_strategy
        end

        self[key] = send("__#{strategy}_array_merge", current, value)

      when value != nil
        self[key] = value
    end
  end

  self
end

#store(key, value) ⇒ Object



134
135
136
# File 'lib/nugrant/bag.rb', line 134

def store(key, value)
  self[key] = value
end

#to_hash(options = {}) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/nugrant/bag.rb', line 138

def to_hash(options = {})
  return {} if empty?()

  use_string_key = options[:use_string_key]

  Hash[map do |key, value|
    key = use_string_key ? key.to_s() : key
    value = __convert_value_to_hash(value, options)

    [key, value]
  end]
end

#walk(path = [], &block) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/nugrant/bag.rb', line 151

def walk(path = [], &block)
  each do |key, value|
    nested_bag = value.kind_of?(Nugrant::Bag)

    value.walk(path + [key], &block) if nested_bag
    yield path + [key], key, value if not nested_bag
  end
end