module Ceres::Children
Public Class Methods
new(*args, &block)
click to toggle source
Calls superclass method
# File lib/ceres/children.rb, line 12 def initialize(*args, &block) super(*args, &block) @children = {} end
Public Instance Methods
[](key)
click to toggle source
# File lib/ceres/children.rb, line 63 def [](key) @children[key] end
add_child(child, replace: false)
click to toggle source
# File lib/ceres/children.rb, line 26 def add_child(child, replace: false) key = child_key_from_object(child) if replace || !@children.has_key?(key) @children[key] = child_value_from_object(child) else message = "overwriting #{key.inspect} is not allowed" message += "\n new value: #{child.inspect}" message += "\n old value: #{@children[key].inspect}" raise ArgumentError, message end end
dig(key, *argv, &block)
click to toggle source
# File lib/ceres/children.rb, line 90 def dig(key, *argv, &block) if self.has_key?(key) object = self[key] argv.count > 0 ? object.dig(*argv) : object end end
each_child(&block)
click to toggle source
# File lib/ceres/children.rb, line 53 def each_child(&block) @children.each_value(&block) self end
fetch(key, *argv, &block)
click to toggle source
# File lib/ceres/children.rb, line 71 def fetch(key, *argv, &block) if self.has_key?(key) @children[key] elsif argv.count > 0 argv[0] elsif block block.call(key) else raise KeyError.new("key not found #{key.inspect}") # TODO (ruby 2.6) # raise KeyError.new("key not found #{key.inspect}", key: name, receiver: self) end end
fetch_values(*keys, &block)
click to toggle source
# File lib/ceres/children.rb, line 86 def fetch_values(*keys, &block) keys.map { |k| self.fetch(k, &block) } end
has_key?(key)
click to toggle source
# File lib/ceres/children.rb, line 67 def has_key?(key) @children.has_key?(key) end
remove_child(key, &block)
click to toggle source
# File lib/ceres/children.rb, line 40 def remove_child(key, &block) if result = @children.delete(key) result elsif block block.call(key) else raise KeyError.new("key not found #{key.inspect}") # TODO (ruby 2.6) # raise KeyError.new("key not found #{key.inspect}", key: name, receiver: self) end end
select_children(&block)
click to toggle source
# File lib/ceres/children.rb, line 59 def select_children(&block) @children.values.select(&block) end
Private Instance Methods
child_key_from_object(object)
click to toggle source
# File lib/ceres/children.rb, line 18 def child_key_from_object(object) object.name end
child_value_from_object(object)
click to toggle source
# File lib/ceres/children.rb, line 22 def child_value_from_object(object) object end