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, #descriptor, #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.



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

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

Instance Attribute Details

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

Returns:



11
12
13
# File 'lib/stupidedi/values/composite_element_val.rb', line 11

def children
  @children
end

#usageCompositeElementUse (readonly)

Returns:

  • (CompositeElementUse)


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

def usage
  @usage
end

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/stupidedi/values/composite_element_val.rb', line 95

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

#composite?Boolean

Returns:

  • (Boolean)

    true



48
49
50
# File 'lib/stupidedi/values/composite_element_val.rb', line 48

def composite?
  true
end

#copy(changes = {}) ⇒ CompositeElementVal

Returns:



26
27
28
29
30
# File 'lib/stupidedi/values/composite_element_val.rb', line 26

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

#element(n) ⇒ SimpleElementVal

Returns:



53
54
55
56
57
58
59
60
# File 'lib/stupidedi/values/composite_element_val.rb', line 53

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



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

def leaf?
  false
end

#position



32
33
34
35
36
37
38
39
# File 'lib/stupidedi/values/composite_element_val.rb', line 32

def position
  if @children.present?
    @children.head.position
  else
    # GH-194
    "<position unknown>"
  end
end

#pretty_print(q)

This method returns an undefined value.



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

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