Class: Eddy::Summary::Loop

Inherits:
Object
  • Object
show all
Defined in:
lib/eddy/summary/loop.rb

Overview

A repeated collection of Segments and/or other Loops. Used in Companion Guides.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializevoid



26
27
28
# File 'lib/eddy/summary/loop.rb', line 26

def initialize()
  self.components = []
end

Instance Attribute Details

#componentsArray<Eddy::Summary::Segment, Eddy::Summary::Loop>

The components that make up the Loop.



23
24
25
# File 'lib/eddy/summary/loop.rb', line 23

def components
  @components
end

#idString

A unique string used to identify the Loop within its Transaction Set.

Returns:

  • (String)


8
9
10
# File 'lib/eddy/summary/loop.rb', line 8

def id
  @id
end

#levelString

Indicates where the Loop is located in the Transaction Set.

Returns:

  • (String)


17
18
19
# File 'lib/eddy/summary/loop.rb', line 17

def level
  @level
end

#notesString

Syntax, Semantic, or Comment notes on a Loop.

Returns:

  • (String)


14
15
16
# File 'lib/eddy/summary/loop.rb', line 14

def notes
  @notes
end

#repeat_limitInteger

Number of times a particular Loop may be repeated.

Returns:

  • (Integer)


11
12
13
# File 'lib/eddy/summary/loop.rb', line 11

def repeat_limit
  @repeat_limit
end

#reqString

Defines if/how the Loop is required.

Returns:

  • (String)


20
21
22
# File 'lib/eddy/summary/loop.rb', line 20

def req
  @req
end

Class Method Details

.create(params = {}) ⇒ self

Parameters:

  • params (Hash) (defaults to: {})

Returns:

  • (self)


32
33
34
35
36
37
38
39
40
41
# File 'lib/eddy/summary/loop.rb', line 32

def self.create(params = {})
  l = new()
  l.id = params[:loop_id]
  l.repeat_limit = params[:repeat]
  l.notes = params[:notes]
  l.level = params[:level]
  l.req = params[:req]
  l.process_components(params[:components])
  return l
end

.from_file(path) ⇒ self

Parameters:

  • path (String)

    Path to a JSON or YAML file containing a valid Loop definition.

Returns:

  • (self)

Raises:



45
46
47
48
49
# File 'lib/eddy/summary/loop.rb', line 45

def self.from_file(path)
  raise Eddy::Errors::Error, "Invalid segment definition" unless Eddy::Summary.valid_loop_data?(path)
  data = Eddy::Util.read_json_or_yaml(path)
  return self.create(data)
end

Instance Method Details

#all_componentsArray<Eddy::Summary::Segment, Eddy::Summary::Loop>

Return all components in a single, flattened array.



89
90
91
92
93
94
95
96
97
# File 'lib/eddy/summary/loop.rb', line 89

def all_components()
  return self.components.map do |comp|
    case comp
    when Eddy::Summary::Loop    then [comp, comp.all_components()]
    when Eddy::Summary::Segment then comp
    else raise Eddy::Errors::Error
    end
  end.flatten
end

#build_as_segment?Boolean

Returns true the loop should be treated as a segment when Generating ruby code. (For use in Build)

Returns:

  • (Boolean)


130
131
132
133
134
# File 'lib/eddy/summary/loop.rb', line 130

def build_as_segment?()
  return false if self.hierarchical?
  return false if self.repeat_limit > 1
  return true
end

#class_nameString

Return id with the prefixes l_ or "hl_" removed.

Returns:

  • (String)


114
115
116
# File 'lib/eddy/summary/loop.rb', line 114

def class_name()
  return self.id.downcase.gsub(/\Ah?l_/i, "")
end

#doc_comment(header: true) ⇒ String

Generate a description to use as a doc comment for a Loop.

Parameters:

  • header (Boolean) (defaults to: true)

    (true)

Returns:

  • (String)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/eddy/summary/loop.rb', line 68

def doc_comment(header: true)
  comps = ""
  self.components.each do |comp|
    case comp
    when Eddy::Summary::Segment then comps << "  - #{comp.id.upcase}\n"
    when Eddy::Summary::Loop    then comps << "  - #{comp.id.upcase} (loop)\n"
    end
  end
  parts = []
  parts << "### Loop Summary:\n" if header
  parts << <<~YARD.strip
    - Repeat: #{self.repeat_limit}
    - Components:
    #{comps}
  YARD
  return parts.compact.join("\n")
end

#hierarchical?Boolean

Returns true if the loop is a Hierarchical Loop. Meant to be used for class or module names.

Returns:

  • (Boolean)


122
123
124
# File 'lib/eddy/summary/loop.rb', line 122

def hierarchical?()
  return self.id =~ /\Ahl_/i
end

#process_components(components) ⇒ void

This method returns an undefined value.

Parameters:

  • components (Array<Hash>)


53
54
55
56
57
58
59
60
61
62
# File 'lib/eddy/summary/loop.rb', line 53

def process_components(components)
  return if components.nil?
  components.each do |comp|
    if comp.key?(:loop_id)
      self.components << Eddy::Summary::Loop.create(comp)
    else
      self.components << Eddy::Summary::Segment.create(comp)
    end
  end
end

#var_nameString

Return id with "l_" slapped on the front if it wasn't already there. Meant to be used for instance variable names.

Returns:

  • (String)


103
104
105
106
107
108
109
# File 'lib/eddy/summary/loop.rb', line 103

def var_name()
  if self.id.start_with?(/\Ah?l_/i)
    return self.id.downcase
  else
    return "l_#{self.id.downcase}"
  end
end