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.



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

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

Instance Attribute Details

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

Returns:



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

def children
  @children
end

#usageCompositeElementUse (readonly)

Returns:

  • (CompositeElementUse)


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

def usage
  @usage
end

Instance Method Details

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
# File 'lib/stupidedi/values/composite_element_val.rb', line 92

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

#composite?Boolean

Returns:

  • (Boolean)

    true



45
46
47
# File 'lib/stupidedi/values/composite_element_val.rb', line 45

def composite?
  true
end

#copy(changes = {}) ⇒ CompositeElementVal

Returns:



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

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

#element(n) ⇒ SimpleElementVal

Returns:



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

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



39
40
41
# File 'lib/stupidedi/values/composite_element_val.rb', line 39

def leaf?
  false
end

#pretty_print(q) ⇒ void

This method returns an undefined value.



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

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