Class: Stupidedi::Values::CompositeElementVal

Inherits:
AbstractElementVal show all
Extended by:
Forwardable
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.



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

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

Instance Attribute Details

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

Returns:



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

def children
  @children
end

#usageCompositeElementUse (readonly)

Returns:

  • (CompositeElementUse)


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

def usage
  @usage
end

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#composite?Boolean

Returns:

  • (Boolean)

    true



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

def composite?
  true
end

#copy(changes = {}) ⇒ CompositeElementVal

Returns:



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

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

#element(n) ⇒ SimpleElementVal

Returns:



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

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



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

def leaf?
  false
end

#pretty_print(q) ⇒ void

This method returns an undefined value.



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

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