Class: OpenStruct

Inherits:
Object
  • Object
show all
Extended by:
L43::Core::Forwarder
Defined in:
lib/l43/open_struct.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from L43::Core::Forwarder

forward, forward_all

Class Method Details

.to_procObject



9
10
11
12
13
# File 'lib/l43/open_struct.rb', line 9

def self.to_proc
  -> hashy do
    new hashy
  end
end

Instance Method Details

#merge(other) ⇒ Object



15
16
17
# File 'lib/l43/open_struct.rb', line 15

def merge(other)
  self.class.new(to_h.merge(other.to_h))
end

#merge_values(*keys, &merger) ⇒ Object



19
20
21
# File 'lib/l43/open_struct.rb', line 19

def merge_values(*keys, &merger)
  self.class.new(to_h).update_values(*keys, &merger)
end

#update(hashy) ⇒ Object



23
24
25
26
27
28
# File 'lib/l43/open_struct.rb', line 23

def update(hashy)
  hashy.to_h.each do |k, v|
    self[k] = v
  end
  self
end

#update_values(*keys, &updater) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/l43/open_struct.rb', line 30

def update_values(*keys, &updater)
  keys.flatten.each do |key|
    value = fetch(key) { raise KeyError, "must not update missing key: #{key.inspect}" }
    value = updater.(value)
    self[key] = value
  end
  self
end