Module: DeepCover::Node::Mixin::HasChild::ClassMethods

Defined in:
lib/deep_cover/node/mixin/has_child.rb

Instance Method Summary collapse

Instance Method Details

#check_children_types(nodes) ⇒ Object



51
52
53
54
# File 'lib/deep_cover/node/mixin/has_child.rb', line 51

def check_children_types(nodes)
  types = expected_types(nodes)
  nodes_mismatches(nodes, types)
end

#child_index_to_name(index, nb_children) ⇒ Object

Raises:

  • (IndexError)


43
44
45
46
47
48
49
# File 'lib/deep_cover/node/mixin/has_child.rb', line 43

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, **args) ⇒ Object

Raises:

  • (TypeError)


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, **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_)
  define_accessor(name)
  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_childrenObject



56
57
58
# File 'lib/deep_cover/node/mixin/has_child.rb', line 56

def min_children
  self::CHILDREN.values.grep(Integer).size
end