Class: ECG::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/ecg/store.rb

Instance Method Summary collapse

Constructor Details

#initialize(values = {}) ⇒ Store

Returns a new instance of Store.



5
6
7
# File 'lib/ecg/store.rb', line 5

def initialize(values = {})
  @values = values.map { |k, v| [k.to_sym, transform_value(v)] }.to_h
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



27
28
29
30
31
32
33
34
35
# File 'lib/ecg/store.rb', line 27

def method_missing(name, *args, &block)
  if @values.key?(name)
    @values[name]
  elsif @values.respond_to?(name)
    @values.public_send(name, *args, &block)
  else
    super
  end
end

Instance Method Details

#==(other) ⇒ Object



55
56
57
58
59
# File 'lib/ecg/store.rb', line 55

def ==(other)
  return false unless other.is_a?(self.class)

  to_h == other.to_h
end

#initialize_copy(obj) ⇒ Object



9
10
11
12
# File 'lib/ecg/store.rb', line 9

def initialize_copy(obj)
  super
  @values = @values.dup
end

#inspectObject



14
15
16
17
# File 'lib/ecg/store.rb', line 14

def inspect
  detail = @values.map { |k, v| " @#{k}=#{v.inspect}" }.join(',')
  "#<#{self.class}:#{object_id << 1}#{detail}>"
end

#merge(*others) ⇒ Object



19
20
21
# File 'lib/ecg/store.rb', line 19

def merge(*others)
  others.inject(dup, &method(:deep_merge))
end

#merge!(*others) ⇒ Object



23
24
25
# File 'lib/ecg/store.rb', line 23

def merge!(*others)
  others.inject(self, &method(:deep_merge))
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/ecg/store.rb', line 37

def respond_to_missing?(name, include_private = false)
  @values.key?(name) || @values.respond_to?(name) || super
end

#store(key, value) ⇒ Object Also known as: []=



41
42
43
# File 'lib/ecg/store.rb', line 41

def store(key, value)
  @values.store(key.to_sym, transform_value(value))
end

#to_hObject



46
47
48
# File 'lib/ecg/store.rb', line 46

def to_h
  @values.transform_values(&method(:intransform_value))
end

#to_json(*args) ⇒ Object



50
51
52
53
# File 'lib/ecg/store.rb', line 50

def to_json(*args)
  require 'json'
  to_h.to_json(args)
end