Class: DocoptNG::Pattern

Inherits:
Object
  • Object
show all
Defined in:
lib/docopt_ng/pattern.rb

Direct Known Subclasses

ChildPattern, ParentPattern

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



3
4
5
# File 'lib/docopt_ng/pattern.rb', line 3

def children
  @children
end

Instance Method Details

#==(other) ⇒ Object



5
6
7
# File 'lib/docopt_ng/pattern.rb', line 5

def ==(other)
  inspect == other.inspect
end

#dumpObject



13
14
15
# File 'lib/docopt_ng/pattern.rb', line 13

def dump
  puts ::Docopt.dump_patterns(self)
end

#eitherObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/docopt_ng/pattern.rb', line 62

def either
  ret = []
  groups = [[self]]
  while groups.count.positive?
    children = groups.shift
    types = children.map(&:class)

    if types.include?(Either)
      either = children.find { |child| child.instance_of?(Either) }
      children.slice!(children.index(either))
      either.children.each do |c|
        groups << ([c] + children)
      end
    elsif types.include?(Required)
      required = children.find { |child| child.instance_of?(Required) }
      children.slice!(children.index(required))
      groups << (required.children + children)

    elsif types.include?(Optional)
      optional = children.find { |child| child.instance_of?(Optional) }
      children.slice!(children.index(optional))
      groups << (optional.children + children)

    elsif types.include?(AnyOptions)
      anyoptions = children.find { |child| child.instance_of?(AnyOptions) }
      children.slice!(children.index(anyoptions))
      groups << (anyoptions.children + children)

    elsif types.include?(OneOrMore)
      oneormore = children.find { |child| child.instance_of?(OneOrMore) }
      children.slice!(children.index(oneormore))
      groups << ((oneormore.children * 2) + children)

    else
      ret << children
    end
  end

  args = ret.map { |e| Required.new(*e) }
  Either.new(*args)
end

#fixObject



17
18
19
20
21
# File 'lib/docopt_ng/pattern.rb', line 17

def fix
  fix_identities
  fix_repeating_arguments
  self
end

#fix_identities(uniq = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/docopt_ng/pattern.rb', line 23

def fix_identities(uniq = nil)
  unless instance_variable_defined?(:@children)
    return self
  end

  uniq ||= flat.uniq

  @children.each_with_index do |c, i|
    if c.instance_variable_defined?(:@children)
      c.fix_identities(uniq)
    else
      unless uniq.include?(c)
        raise RuntimeError
      end

      @children[i] = uniq[uniq.index(c)]
    end
  end
end

#fix_repeating_argumentsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/docopt_ng/pattern.rb', line 43

def fix_repeating_arguments
  either.children.map(&:children).each do |case_|
    case_.select { |c| case_.count(c) > 1 }.each do |e|
      if e.instance_of?(Argument) || (e.instance_of?(Option) && e.argcount.positive?)
        if e.value.nil?
          e.value = []
        elsif e.value.class != Array
          e.value = e.value.split
        end
      end
      if e.instance_of?(Command) || (e.instance_of?(Option) && e.argcount.zero?)
        e.value = 0
      end
    end
  end

  self
end

#to_strObject



9
10
11
# File 'lib/docopt_ng/pattern.rb', line 9

def to_str
  inspect
end