Class: ECG::Store

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

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Store

Returns a new instance of Store.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



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

def method_missing(name, *args)
  name = name.to_sym

  if @store.key?(name)
    transform_value(@store[name])
  elsif @store.respond_to?(name)
    @store.public_send(name)
  else
    super
  end
end

Instance Method Details

#bindingObject



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

def binding
  super # NOTE: Kernel.#binding is private
end

#initialize_copyObject



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

def initialize_copy
  super
  @store = @store.dup
end

#inspectObject



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

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

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

Returns:

  • (Boolean)


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

def respond_to_missing?(symbol, include_private = false)
  @store&.key?(symbol.to_sym) || super
end

#to_hObject



40
41
42
43
44
45
46
47
48
# File 'lib/ecg/store.rb', line 40

def to_h
  @store.transform_values do |v|
    case v
    when self.class then v.to_h
    when Array then v.map(&:to_h)
    else v
    end
  end
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