Class: Stupidedi::Values::CompositeElementVal

Inherits:
AbstractElementVal show all
Defined in:
lib/stupidedi/values/composite_element_val.rb

Overview

See Also:

  • B.1.1.3.3 Composite Data Structure

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractElementVal

#element?, #size

Methods inherited from AbstractVal

#blank?, #characters, #component?, #definition, #element?, #empty?, #functional_group?, #interchange?, #invalid?, #loop?, #present?, #repeated?, #segment?, #separator?, #simple?, #size, #table?, #transaction_set?, #transmission?, #valid?

Methods included from Color

ansi, #ansi

Methods included from Inspect

#inspect

Constructor Details

#initialize(children, usage) ⇒ CompositeElementVal

Returns a new instance of CompositeElementVal



24
25
26
27
# File 'lib/stupidedi/values/composite_element_val.rb', line 24

def initialize(children, usage)
  @children, @usage =
    children, usage
end

Instance Attribute Details

#childrenArray<SimpleElementVal> (readonly) Also known as: component_vals

Returns:



16
17
18
# File 'lib/stupidedi/values/composite_element_val.rb', line 16

def children
  @children
end

#usageCompositeElementUse (readonly)

Returns:

  • (CompositeElementUse)


20
21
22
# File 'lib/stupidedi/values/composite_element_val.rb', line 20

def usage
  @usage
end

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
# File 'lib/stupidedi/values/composite_element_val.rb', line 90

def ==(other)
  eql?(other) or
   (other.definition == definition and
    other.children   == @children)
end

#composite?Boolean

Returns:

  • (Boolean)

    true



43
44
45
# File 'lib/stupidedi/values/composite_element_val.rb', line 43

def composite?
  true
end

#copy(changes = {}) ⇒ CompositeElementVal

Returns:



30
31
32
33
34
# File 'lib/stupidedi/values/composite_element_val.rb', line 30

def copy(changes = {})
  CompositeElementVal.new \
    changes.fetch(:children, @children),
    changes.fetch(:usage, @usage)
end

#element(n) ⇒ SimpleElementVal

Returns:



48
49
50
51
52
53
54
55
# File 'lib/stupidedi/values/composite_element_val.rb', line 48

def element(n)
  unless n > 0
    raise ArgumentError,
      "n must be positive"
  end

  @children.at(n - 1)
end

#leaf?Boolean

Returns false

Returns:

  • (Boolean)

    false



37
38
39
# File 'lib/stupidedi/values/composite_element_val.rb', line 37

def leaf?
  false
end

#pretty_print(q)

This method returns an undefined value.



58
59
60
61
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
# File 'lib/stupidedi/values/composite_element_val.rb', line 58

def pretty_print(q)
  id = definition.bind do |d|
    "[#{d.id}: #{d.name}]".bind do |s|
      if usage.forbidden?
        ansi.forbidden(s)
      elsif usage.required?
        ansi.required(s)
      else
        ansi.optional(s)
      end
    end
  end

  q.text(ansi.composite("CompositeElementVal#{id}"))

  if empty?
    q.text(ansi.composite(".empty"))
  else
    q.group(2, "(", ")") do
      q.breakable ""
      @children.each do |e|
        unless q.current_group.first?
          q.text ", "
          q.breakable
        end
        q.pp e
      end
    end
  end
end