Class: Baldr::Segment
- Inherits:
-
Object
show all
- Defined in:
- lib/baldr/segment.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(id) ⇒ Segment
Returns a new instance of Segment.
24
25
26
27
28
|
# File 'lib/baldr/segment.rb', line 24
def initialize(id)
@elements = []
@children = []
@id = id.to_s.upcase
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/baldr/segment.rb', line 77
def method_missing(method, *args, &block)
m = method.to_s
if m.start_with?(@id) && m.length == @id.length + 2
element = m[-2..-1].to_i - 1
@elements[element] = args[0]
elsif m =~ /^[A-Z][A-Z0-9]{1,2}$/
segment = Baldr::Segment.new(m)
get_loop(m).add segment
if block_given?
if block.arity == 0
segment.instance_eval &block
else
block.call(segment)
end
end
else
super
end
end
|
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
3
4
5
|
# File 'lib/baldr/segment.rb', line 3
def children
@children
end
|
#elements ⇒ Object
Returns the value of attribute elements.
3
4
5
|
# File 'lib/baldr/segment.rb', line 3
def elements
@elements
end
|
#id ⇒ Object
Returns the value of attribute id.
3
4
5
|
# File 'lib/baldr/segment.rb', line 3
def id
@id
end
|
Class Method Details
.helpers ⇒ Object
20
21
22
|
# File 'lib/baldr/segment.rb', line 20
def self.helpers
@helpers || []
end
|
.helpers_for_elements(hash) ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/baldr/segment.rb', line 5
def self.helpers_for_elements(hash)
@helpers = hash.values
hash.each do |element, method|
self.class_eval <<-RUBY
def #{method}
self['#{element}']
end
def #{method}=(value)
self['#{element}'] = value
end
RUBY
end
end
|
Instance Method Details
#[](key) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/baldr/segment.rb', line 55
def [](key)
key = key.to_s
if key.start_with?(@id) && key.length == @id.length + 2
element = key[-2..-1].to_i - 1
@elements[element]
elsif !key.start_with?(@id) && key.length > 3 && key =~ /[0-9]{2}$/
loop = @children.select { |l| l.id == key[0..-3] }.first
if loop
if loop.segments.length == 1
loop.segments.first[key]
else
raise Baldr::Error, 'there are more than 1 segment in loop. use it as enum' if loop.segments.length > 1
end
end
elsif key =~ /^[A-z][A-Z0-9]{1,2}$/
loop = @children.select { |l| l.id == key }.first
if loop
loop.segments.to_enum
end
end
end
|
#[]=(key, value) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/baldr/segment.rb', line 34
def []=(key, value)
key = key.to_s
if key.start_with?(@id) && key.length == @id.length + 2
element = key[-2..-1].to_i - 1
@elements[element] = value
elsif !key.start_with?(@id) && key.length > 3 && key =~ /[0-9]{2}$/
loop = @children.select { |l| l.id == key[0..-3] }.first
if loop
if loop.segments.length == 1
loop.segments.first[key] = value
else
raise Baldr::Error, 'there are more than 1 segment in loop. you can\'t assign from here' if loop.segments.length > 1
end
else
raise Baldr::Error, "segment #{key[0..-3]} not found"
end
else
raise Baldr::Error, "unable to assign #{key}"
end
end
|
#add(segment) ⇒ Object
121
122
123
|
# File 'lib/baldr/segment.rb', line 121
def add(segment)
get_loop(segment.id).add segment
end
|
#create(id) ⇒ Object
97
98
99
100
101
102
103
|
# File 'lib/baldr/segment.rb', line 97
def create(id)
if id =~ /^[A-Z][A-Z0-9]{1,2}$/
segment = Baldr::Segment.new(id)
get_loop(id).add segment
segment
end
end
|
#fetch(key, default = nil) ⇒ Object
30
31
32
|
# File 'lib/baldr/segment.rb', line 30
def fetch(key, default = nil)
self[key] || default
end
|
#number_of_segments ⇒ Object
117
118
119
|
# File 'lib/baldr/segment.rb', line 117
def number_of_segments
1 + @children.map(&:number_of_segments).sum
end
|
#prepare! ⇒ Object
113
114
115
|
# File 'lib/baldr/segment.rb', line 113
def prepare!
end
|
#sub_grammar(version) ⇒ Object
109
110
111
|
# File 'lib/baldr/segment.rb', line 109
def sub_grammar(version)
end
|
#sub_version ⇒ Object
105
106
107
|
# File 'lib/baldr/segment.rb', line 105
def sub_version
end
|