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, **args) ⇒ Object
- #has_extra_children(**args) ⇒ Object
- #min_children ⇒ Object
- #refine_child(child_name = nil, **args) ⇒ Object
Instance Method Details
#check_children_types(nodes) ⇒ Object
58 59 60 61 |
# File 'lib/deep_cover/node/mixin/has_child.rb', line 58 def check_children_types(nodes) types = expected_types(nodes) nodes_mismatches(nodes, types) end |
#child_index_to_name(index, nb_children) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/deep_cover/node/mixin/has_child.rb', line 50 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, **args) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/deep_cover/node/mixin/has_child.rb', line 29 def has_child(rest_: false, refine_: false, **args) raise "Needs exactly one custom named argument, got #{args.size}" if args.size != 1 name, types = args.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(**args) ⇒ Object
39 40 41 |
# File 'lib/deep_cover/node/mixin/has_child.rb', line 39 def has_extra_children(**args) has_child(**args, rest_: true) end |
#min_children ⇒ Object
63 64 65 |
# File 'lib/deep_cover/node/mixin/has_child.rb', line 63 def min_children self::CHILDREN.values.grep(Integer).size end |
#refine_child(child_name = nil, **args) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/deep_cover/node/mixin/has_child.rb', line 43 def refine_child(child_name = nil, **args) if child_name args = {child_name => self::CHILDREN_TYPES.fetch(child_name), **args} end has_child(**args, refine_: true) end |