ltdtemplate 0.2.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/CHANGELOG +10 -1
  2. data/Gemfile +2 -1
  3. data/RESOURCES +18 -32
  4. data/TEMPLATE_MANUAL.html +126 -47
  5. data/lib/ltdtemplate.rb +352 -242
  6. data/lib/ltdtemplate/code.rb +14 -87
  7. data/lib/ltdtemplate/code/call.rb +20 -16
  8. data/lib/ltdtemplate/code/parameters.rb +28 -31
  9. data/lib/ltdtemplate/code/sequence.rb +39 -0
  10. data/lib/ltdtemplate/code/subscript.rb +57 -50
  11. data/lib/ltdtemplate/code/variable.rb +22 -39
  12. data/lib/ltdtemplate/proxy.rb +26 -0
  13. data/lib/ltdtemplate/proxy/array.rb +258 -0
  14. data/lib/ltdtemplate/proxy/boolean.rb +74 -0
  15. data/lib/ltdtemplate/proxy/match.rb +40 -0
  16. data/lib/ltdtemplate/proxy/nil.rb +27 -0
  17. data/lib/ltdtemplate/proxy/number.rb +77 -0
  18. data/lib/ltdtemplate/proxy/regexp.rb +74 -0
  19. data/lib/ltdtemplate/proxy/string.rb +196 -0
  20. data/lib/ltdtemplate/value.rb +94 -0
  21. data/lib/ltdtemplate/value/array_splat.rb +34 -0
  22. data/lib/ltdtemplate/value/code_block.rb +21 -17
  23. data/lib/ltdtemplate/value/namespace.rb +77 -79
  24. data/ltdtemplate.gemspec +2 -2
  25. data/test/04number.rb +0 -7
  26. data/test/05string.rb +0 -7
  27. data/test/06array.rb +0 -9
  28. data/test/07each.rb +3 -3
  29. data/test/08interpolate.rb +1 -1
  30. data/test/10missing_meth.rb +1 -1
  31. data/test/11classes.rb +9 -9
  32. metadata +15 -13
  33. data/lib/ltdtemplate/code/code_block.rb +0 -30
  34. data/lib/ltdtemplate/value/array.rb +0 -210
  35. data/lib/ltdtemplate/value/boolean.rb +0 -82
  36. data/lib/ltdtemplate/value/nil.rb +0 -30
  37. data/lib/ltdtemplate/value/number.rb +0 -96
  38. data/lib/ltdtemplate/value/string.rb +0 -215
  39. data/lib/test.rb +0 -10
  40. data/test/03tpl_singletons.rb +0 -48
metadata CHANGED
@@ -3,10 +3,10 @@ name: ltdtemplate
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
+ - 1
6
7
  - 0
7
- - 2
8
- - 4
9
- version: 0.2.4
8
+ - 0
9
+ version: 1.0.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Brian Katzung
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2014-03-15 00:00:00 -05:00
17
+ date: 2014-05-20 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -28,17 +28,21 @@ extensions: []
28
28
  extra_rdoc_files: []
29
29
 
30
30
  files:
31
- - lib/test.rb
32
31
  - lib/ltdtemplate/value/code_block.rb
33
- - lib/ltdtemplate/value/array.rb
34
- - lib/ltdtemplate/value/nil.rb
35
- - lib/ltdtemplate/value/boolean.rb
36
- - lib/ltdtemplate/value/number.rb
37
32
  - lib/ltdtemplate/value/namespace.rb
38
- - lib/ltdtemplate/value/string.rb
33
+ - lib/ltdtemplate/value/array_splat.rb
34
+ - lib/ltdtemplate/proxy.rb
39
35
  - lib/ltdtemplate/code.rb
36
+ - lib/ltdtemplate/value.rb
37
+ - lib/ltdtemplate/proxy/match.rb
38
+ - lib/ltdtemplate/proxy/regexp.rb
39
+ - lib/ltdtemplate/proxy/array.rb
40
+ - lib/ltdtemplate/proxy/nil.rb
41
+ - lib/ltdtemplate/proxy/boolean.rb
42
+ - lib/ltdtemplate/proxy/number.rb
43
+ - lib/ltdtemplate/proxy/string.rb
44
+ - lib/ltdtemplate/code/sequence.rb
40
45
  - lib/ltdtemplate/code/subscript.rb
41
- - lib/ltdtemplate/code/code_block.rb
42
46
  - lib/ltdtemplate/code/call.rb
43
47
  - lib/ltdtemplate/code/parameters.rb
44
48
  - lib/ltdtemplate/code/variable.rb
@@ -55,7 +59,6 @@ files:
55
59
  - test/05string.rb
56
60
  - test/06array.rb
57
61
  - test/07each.rb
58
- - test/03tpl_singletons.rb
59
62
  - test/08interpolate.rb
60
63
  - test/01instance.rb
61
64
  - test/02tpl_literal.rb
@@ -100,7 +103,6 @@ test_files:
100
103
  - test/05string.rb
101
104
  - test/06array.rb
102
105
  - test/07each.rb
103
- - test/03tpl_singletons.rb
104
106
  - test/08interpolate.rb
105
107
  - test/01instance.rb
106
108
  - test/02tpl_literal.rb
@@ -1,30 +0,0 @@
1
- # LtdTemplate::Code::Code_Block - Represents a code block (a list of
2
- # code steps) in an LtdTemplate
3
- #
4
- # Implied code blocks do not accept parameters or generate new namespaces.
5
- # They are used for things like call parameters and subscript expressions.
6
- # See also: LtdTemplate::Value::Code_Block.
7
- #
8
- # Author:: Brian Katzung <briank@kappacs.com>, Kappa Computer Solutions, LLC
9
- # Copyright:: 2013 Brian Katzung and Kappa Computer Solutions, LLC
10
- # License:: MIT License
11
-
12
- require 'ltdtemplate/code'
13
-
14
- class LtdTemplate::Code::Code_Block < LtdTemplate::Code
15
-
16
- def initialize (template, code)
17
- super template
18
- @code = code
19
- end
20
-
21
- def get_value (opts = {})
22
- values = @code.map { |part| part.get_value }.flatten
23
- case values.size
24
- when 0 then @template.nil
25
- when 1 then values[0]
26
- else @template.factory(:array).set_value(values)
27
- end
28
- end
29
-
30
- end
@@ -1,210 +0,0 @@
1
- # LtdTemplate::Value::Array - Represents a combination array/hash value
2
- # in an LtdTemplate
3
- #
4
- # @author Brian Katzung <briank@kappacs.com>, Kappa Computer Solutions, LLC
5
- # @copyright 2013 Brian Katzung and Kappa Computer Solutions, LLC
6
- # @license MIT License
7
-
8
- require 'ltdtemplate/code'
9
- require 'sarah'
10
-
11
- class LtdTemplate::Value::Array < LtdTemplate::Code
12
-
13
- attr_reader :sarah
14
-
15
- def initialize (template)
16
- super template
17
- @sarah = Sarah.new
18
- @scalar = false
19
- @template.use :arrays
20
- end
21
-
22
- #
23
- # Access positional (sequential) or named (random-access)
24
- # parts of the array
25
- #
26
- def positional; @sarah.seq; end
27
- def named; @sarah.rnd; end
28
-
29
- #
30
- # Implement the subscripting interface. Note that the most recent
31
- # value is always used. (1 .. 0, 2, 0, 3)[0] is 3.
32
- # Items set within (or at the end of) the positional range at the
33
- # time will be positional. Otherwise, they will be named.
34
- #
35
- # Keys must be Ruby-native values; values must be template code
36
- # or values.
37
- #
38
- def has_item? (key); @sarah.has_key? key; end
39
- def get_item (key)
40
- @sarah.has_key?(key) ? @sarah[key] : @template.nil
41
- end
42
- def set_item (key, value)
43
- @sarah[key] = value
44
- @template.using :array_size, @sarah.size
45
- end
46
-
47
- def to_boolean; true; end
48
- def to_native
49
- if @sarah.rnd_size == 0 then native = []
50
- elsif @sarah.seq_size == 0 then native = {}
51
- else native = Sarah.new
52
- end
53
- @sarah.each { |key, val| native[key] = val.to_native }
54
- native
55
- end
56
- def to_text; @sarah.seq.map { |val| val.to_text }.join ''; end
57
-
58
- def get_value (opts = {})
59
- case opts[:method]
60
- when nil, 'call' then self
61
- when 'class' then @template.factory :string, 'Array'
62
- when 'each', 'each_rnd', 'each_seq' then do_each opts
63
- when 'join' then do_join opts
64
- when 'pop', '->' then do_pop opts
65
- when 'push', '+>' then do_push opts
66
- when 'rnd_size' then @template.factory :number, @sarah.rnd_size
67
- when 'seq_size' then @template.factory :number, @sarah.seq_size
68
- when 'shift', '<-' then do_shift opts
69
- when 'size' then @template.factory :number, @sarah.size
70
- when 'type' then @template.factory :string, 'array'
71
- when 'unshift', '<+' then do_unshift opts
72
- when '/' then @template.factory :parameters, @sarah.seq, @sarah.rnd
73
- when '%' then @template.factory :parameters, [], @sarah.seq
74
- else do_method opts, 'Array'
75
- end
76
- end
77
-
78
- # Type (for :missing_method callback)
79
- def type; :array; end
80
-
81
- #
82
- # Scalar assignment is used instead of array assignment if the
83
- # parameter list contains exactly one positional parameter and
84
- # the ".." operator was not used.
85
- #
86
- def scalar?; @scalar and @sarah.seq_size == 1; end
87
-
88
- #
89
- # Clear all current positional and named values
90
- #
91
- def clear
92
- @sarah.clear
93
- @scalar = false
94
- self
95
- end
96
-
97
- #
98
- # Set positional and possibly named values. Keys must be ruby-native
99
- # values; values must be template code or values.
100
- #
101
- def set_value (positional, named = {}, scalar = false)
102
- clear
103
- @sarah.merge! positional, named
104
- @scalar = scalar
105
- self
106
- end
107
-
108
- #
109
- # Set (recursively) from a native array.
110
- #
111
- def set_from_array (data)
112
- clear
113
- data.each_index { |i| set_item i, map_native_value(data[i]) }
114
- self
115
- end
116
-
117
- #
118
- # Set (recursively) from a native hash.
119
- #
120
- def set_from_hash (data)
121
- clear
122
- data.each { |key, val| set_item key, map_native_value(val) }
123
- self
124
- end
125
-
126
- #
127
- # Loop over each key, value
128
- #
129
- def do_each (opts)
130
- results = @template.factory :array
131
- if params = opts[:parameters] and params.positional.size > 0
132
- body = params.positional[0]
133
- if opts[:method] != 'each_rnd'
134
- @sarah.seq.each_index do |idx|
135
- @template.use :iterations
136
- body_params = @template.factory :parameters,
137
- [@template.factory(:number, idx), @sarah.seq[idx]]
138
- results.sarah.push body.get_value(:method => 'each_seq',
139
- :parameters => body_params)
140
- end
141
- end
142
- if opts[:method] != 'each_seq'
143
- @sarah.rnd.each do |key, val|
144
- @template.use :iterations
145
- body_params = @template.factory :parameters,
146
- [map_native_value(key), val]
147
- results.sarah.push body.get_value(:method => 'each_rnd',
148
- :parameters => body_params)
149
- end
150
- end
151
- end
152
- results
153
- end
154
-
155
- #
156
- # Combine array element values into a string
157
- #
158
- def do_join (opts)
159
- two = first = middle = last = ''
160
- if params = opts[:parameters]
161
- params = params.positional
162
- if params.size > 3
163
- two, first, middle, last =
164
- params[0..3].map { |val| val.get_value.to_text }
165
- elsif params.size > 0
166
- two = first = middle = last = params[0].get_value.to_text
167
- end
168
- end
169
-
170
- text = @sarah.seq.map { |val| val.get_value.to_text }
171
- @template.factory :string, case text.size
172
- when 0 then ''
173
- when 1 then text[0]
174
- when 2 then "#{text[0]}#{two}#{text[1]}"
175
- else "#{text[0]}#{first}" + text[1..-2].join(middle) +
176
- "#{last}#{text[-1]}"
177
- end
178
- end
179
-
180
- def do_pop (opts)
181
- @sarah.pop || @template.nil
182
- end
183
-
184
- def do_push (opts)
185
- if params = opts[:parameters] then @sarah.append! params.sarah end
186
- @template.nil
187
- end
188
-
189
- def do_shift (opts)
190
- @sarah.shift || @template.nil
191
- end
192
-
193
- def do_unshift (opts)
194
- if params = opts[:parameters] then @sarah.insert! params.sarah end
195
- @template.nil
196
- end
197
-
198
- protected
199
-
200
- def map_native_value (value)
201
- return @template.factory :number, value if value.is_a? Numeric
202
- return @template.factory :string, value if value.is_a? String
203
- return @template.factory(:array).set_from_array value if
204
- value.is_a? Array
205
- return @template.factory(:array).set_from_hash value if
206
- value.is_a? Hash
207
- @template.nil
208
- end
209
-
210
- end
@@ -1,82 +0,0 @@
1
- # LtdTemplate::Value::Boolean - Represents true/false in an LtdTemplate
2
- #
3
- # @author Brian Katzung <briank@kappacs.com>, Kappa Computer Solutions, LLC
4
- # @copyright 2013 Brian Katzung and Kappa Computer Solutions, LLC
5
- # @license MIT License
6
-
7
- require 'ltdtemplate/code'
8
-
9
- class LtdTemplate::Value::Boolean < LtdTemplate::Code
10
-
11
- # Use one shared true value and one shared false value per template.
12
- def self.instance (template, bool)
13
- template.factory_singletons[bool ? :bool_true : :bool_false] ||=
14
- self.new(template, bool)
15
- end
16
-
17
- def initialize (template, bool)
18
- super template
19
- @bool = bool
20
- end
21
-
22
- def to_boolean; @bool; end
23
- def to_native; @bool; end
24
- def to_text; ''; end
25
-
26
- def get_value (opts = {})
27
- case opts[:method]
28
- when nil, 'call' then self
29
- when 'class' then @template.factory :string, 'Boolean'
30
- when 'str', 'string' then @template.factory :string,
31
- (@bool ? 'true' : 'false')
32
- when 'type' then @template.factory :string, 'boolean'
33
- when '+', '|', 'or' then do_or opts
34
- when '*', '&', 'and' then do_and opts
35
- when '!', 'not' then do_not opts
36
- else do_method opts, 'Boolean'
37
- end
38
- end
39
-
40
- # Type (for :missing_method callback)
41
- def type; :boolean; end
42
-
43
- # Implement +/| (or):
44
- # bool|(bool1, ..., boolN)
45
- # True if ANY boolean is true. Evaluates {} blocks until true.
46
- def do_or (opts)
47
- if not @bool and params = opts[:parameters]
48
- params.positional.each do |tval|
49
- return @template.factory :boolean, true if
50
- tval.get_value(:method => 'call').to_boolean
51
- end
52
- end
53
- self
54
- end
55
-
56
- # Implement */& (and):
57
- # bool&(bool1, ..., boolN)
58
- # True if ALL booleans are true. Evaluates {} blocks until false.
59
- def do_and (opts)
60
- if @bool and params = opts[:parameters]
61
- params.positional.each do |tval|
62
- return @template.factory :boolean, false unless
63
- tval.get_value(:method => 'call').to_boolean
64
- end
65
- end
66
- self
67
- end
68
-
69
- # Implement ! (not):
70
- # bool!(bool1, ..., boolN)
71
- # True if ALL booleans are false. Evaluates {} blocks until true.
72
- def do_not (opts)
73
- if !@bool and params = opts[:parameters]
74
- params.positional.each do |tval|
75
- return @template.factory :boolean, false if
76
- tval.get_value(:method => 'call').to_boolean
77
- end
78
- end
79
- @template.factory :boolean, !@bool
80
- end
81
-
82
- end
@@ -1,30 +0,0 @@
1
- # LtdTemplate::Value::Nil - Represents nil in an LtdTemplate
2
- #
3
- # @author Brian Katzung <briank@kappacs.com>, Kappa Computer Solutions, LLC
4
- # @copyright 2013 Brian Katzung and Kappa Computer Solutions, LLC
5
- # @license MIT License
6
-
7
- require 'ltdtemplate/code'
8
-
9
- class LtdTemplate::Value::Nil < LtdTemplate::Code
10
-
11
- # Use one shared instance per template.
12
- def self.instance (template, *args)
13
- template.factory_singletons[:nil] ||= self.new(template, *args)
14
- end
15
-
16
- def get_value (opts = {})
17
- case opts[:method]
18
- when 'type' then @template.factory :string, 'nil'
19
- else do_method opts
20
- end
21
- end
22
-
23
- # Type (for :missing_method callback)
24
- def type; :nil; end
25
-
26
- def to_boolean; false; end
27
- def to_native; ''; end
28
- def to_text; ''; end
29
-
30
- end
@@ -1,96 +0,0 @@
1
- # LtdTemplate::Value::Number - Represents a number in an LtdTemplate
2
- #
3
- # @author Brian Katzung <briank@kappacs.com>, Kappa Computer Solutions, LLC
4
- # @copyright 2013 Brian Katzung and Kappa Computer Solutions, LLC
5
- # @license MIT License
6
-
7
- require 'ltdtemplate/code'
8
-
9
- class LtdTemplate::Value::Number < LtdTemplate::Code
10
-
11
- def initialize (template, value = 0)
12
- super template
13
- case value
14
- when Numeric then @value = value
15
- when String then @value = (value =~ /\./) ? value.to_f : value.to_i
16
- end
17
- end
18
-
19
- def to_boolean; true; end
20
- def to_native; @value; end
21
- def to_text; @value.to_s; end
22
-
23
- def get_value (opts = {})
24
- case opts[:method]
25
- when nil, 'call' then self
26
- when 'abs' then (@value >= 0) ? self :
27
- @template.factory(:number, -@value)
28
- when 'ceil' then @template.factory :number, @value.ceil
29
- when 'class' then @template.factory :string, 'Number'
30
- when 'floor' then @template.factory :number, @value.floor
31
- when 'flt', 'float' then @template.factory :number, @value.to_f
32
- when 'int' then @template.factory :number, @value.to_i
33
- when 'str', 'string' then @template.factory :number, @value.to_s
34
- when 'type' then @template.factory :string, 'number'
35
- when '+' then do_sequential(opts) { |a, b| a + b }
36
- when '-' then do_subtract opts
37
- when '*' then do_sequential(opts) { |a, b| a * b }
38
- when '/' then do_sequential(opts) { |a, b| a / b }
39
- when '%' then do_sequential(opts) { |a, b| a % b }
40
- when '&' then do_sequential(opts) { |a, b| a & b }
41
- when '|' then do_sequential(opts) { |a, b| a | b }
42
- when '^' then do_sequential(opts) { |a, b| a ^ b }
43
- when '<', '<=', '==', '!=', '>=', '>' then do_compare opts
44
- else do_method opts, 'Number'
45
- end
46
- end
47
-
48
- # Type (for :missing_method callback)
49
- def type; :number; end
50
-
51
- # Implement sequential operations (+, *, /, %, &, |, ^)
52
- def do_sequential (opts = {}, &block)
53
- if params = opts[:parameters]
54
- @template.factory(:number,
55
- params.positional.map { |tval| tval.to_native }.
56
- select { |nval| nval.is_a? Numeric }.
57
- inject(@value, &block))
58
- else
59
- @value
60
- end
61
- end
62
-
63
- # Implement "-" method (subtraction/negation)
64
- def do_subtract (opts)
65
- sum = @value
66
- params = params.positional if params = opts[:parameters]
67
- if !params or params.size == 0
68
- sum = -sum
69
- else
70
- params.each do |tval|
71
- nval = tval.to_native
72
- sum -= nval if nval.is_a? Numeric
73
- end
74
- end
75
- @template.factory :number, sum
76
- end
77
-
78
- # Implement numeric comparison operators
79
- def do_compare (opts)
80
- diff = 0
81
- if params = opts[:parameters] and params.positional.size > 0
82
- diff = params.positional[0].to_native
83
- diff = 0 unless diff.is_a? Numeric
84
- end
85
- diff = @value - diff
86
- @template.factory :boolean, case opts[:method]
87
- when '<' then diff < 0
88
- when '<=' then diff <= 0
89
- when '==' then diff == 0
90
- when '!=' then diff != 0
91
- when '>=' then diff >= 0
92
- when '>' then diff > 0
93
- end
94
- end
95
-
96
- end