Module: DeepCover::Node::Mixin::HasChild::ClassMethods
- Defined in:
- lib/deep_cover/node/mixin/has_child.rb
Instance Method Summary collapse
- #check_children_types(nodes) ⇒ Object
- #child_index_to_name(index, nb_children) ⇒ Object
- #has_child(rest_: false, refine_: false, **h) ⇒ Object
- #has_extra_children(**h) ⇒ Object
- #min_children ⇒ Object
- #refine_child(child_name = nil, **h) ⇒ Object
Instance Method Details
#check_children_types(nodes) ⇒ Object
54 55 56 57 |
# File 'lib/deep_cover/node/mixin/has_child.rb', line 54 def check_children_types(nodes) types = expected_types(nodes) nodes_mismatches(nodes, types) end |
#child_index_to_name(index, nb_children) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/deep_cover/node/mixin/has_child.rb', line 46 def child_index_to_name(index, nb_children) self::CHILDREN.each do |name, i| return name if i == index || (i == index - nb_children) || (i.is_a?(Range) && i.begin <= index && i.end + nb_children >= index) end raise IndexError, "index #{index} does not correspond to any child of #{self}" end |
#has_child(rest_: false, refine_: false, **h) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/deep_cover/node/mixin/has_child.rb', line 25 def has_child(rest_: false, refine_: false, **h) raise "Needs exactly one custom named argument, got #{h.size}" if h.size != 1 name, types = h.first raise TypeError, "Expect a Symbol for name, got a #{name.class} (#{name.inspect})" unless name.is_a?(Symbol) update_children_const(name, rest: rest_) unless refine_ define_accessor(name) unless refine_ add_runtime_check(name, types) self end |
#has_extra_children(**h) ⇒ Object
35 36 37 |
# File 'lib/deep_cover/node/mixin/has_child.rb', line 35 def has_extra_children(**h) has_child(**h, rest_: true) end |
#min_children ⇒ Object
59 60 61 |
# File 'lib/deep_cover/node/mixin/has_child.rb', line 59 def min_children self::CHILDREN.values.grep(Integer).size end |
#refine_child(child_name = nil, **h) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/deep_cover/node/mixin/has_child.rb', line 39 def refine_child(child_name = nil, **h) if child_name h = {child_name => self::CHILDREN_TYPES.fetch(child_name), **h} end has_child(**h, refine_: true) end |