ltdtemplate 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,4 +1,21 @@
1
+ 2013-08-04 Version 0.2.3
2
+
3
+ Documented LtdTemplate constructor options, including the new
4
+ :missing_method option to supply a callback for missing method
5
+ calls (an alternative to subclassing LtdTemplate::Value classes).
6
+ Fixed implementation of class and instance set_classes methods
7
+ (which can now also accept a loaded class constant as an
8
+ alternative to a class name string).
9
+
10
+ LtdTemplate value classes now have a "type" method that returns
11
+ an associated symbol (e.g., :nil, :number, :string) for use by
12
+ the :missing_method callback.
13
+
14
+ Added test cases for $.use, the :missing_method callback, and
15
+ class and instance set_classes methods.
16
+
1
17
  2013-07-29 Version 0.2.2
18
+
2
19
  Added string methods capcase, downcase, join, regexp,
3
20
  rep/replace, rep1/replace1, split, and upcase.
4
21
 
@@ -9,6 +26,7 @@
9
26
  also renamed RESOURCES.txt to RESOURCES.
10
27
 
11
28
  2013-07-28 Version 0.2.1
29
+
12
30
  Added array.{each,each_rnd,each_seq} methods (executing the code
13
31
  block supplied as the first parameter with parameters key and
14
32
  value).
@@ -20,23 +38,29 @@
20
38
  an array, hash, or Sarah as required for the data.
21
39
 
22
40
  2013-07-24 Version 0.1.5
41
+
23
42
  Added RESOURCES.txt to .yardopts. OOPS!
24
43
 
25
44
  2013-07-24 Version 0.1.4
45
+
26
46
  Added String.html (HTML encoding) and String.pcte (percent encoding)
27
47
  methods. Added RESOURCES.txt describing resource usage/limits.
28
48
 
29
49
  2013-07-13 Version 0.1.3
50
+
30
51
  Fix broken links and otherwise add some polish to TEMPLATE_MANUAL.html.
31
52
 
32
53
  2013-07-13 Version 0.1.2
54
+
33
55
  Added .yardopts to gemspec. Still trying to get the hang of Ruby
34
56
  packaging.
35
57
 
36
58
  2013-07-12 Version 0.1.1
59
+
37
60
  Added Gemfile
38
61
 
39
62
  2013-07-12 Version 0.1.0
63
+
40
64
  Fixed class names in unit tests.
41
65
  String.* now handles multipliers <= 0.
42
66
  Added number.abs (absolute value) method.
@@ -50,4 +74,5 @@
50
74
  Added the template manual (TEMPLATE_MANUAL.html).
51
75
 
52
76
  2013-07-08 Version 0.0.1
77
+
53
78
  First release
data/lib/ltdtemplate.rb CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  class LtdTemplate
10
10
 
11
- VERSION = '0.2.2'
11
+ VERSION = '0.2.3'
12
12
 
13
13
  TOKEN_MAP = {
14
14
  ?. => :dot, # method separator
@@ -95,14 +95,16 @@ class LtdTemplate
95
95
  @@classes.merge! classes
96
96
  end
97
97
 
98
+ # Constructor
99
+ #
100
+ # @param options [Hash] Options hash
101
+ # @option options [Proc] :loader Callback for $.use method
102
+ # @option options [Proc] :missing_method Callback for missing methods
98
103
  def initialize (options = {})
99
- @classes = @@classes
100
- @code = nil
101
- @factory_singletons = {}
102
- @limits = {}
103
- @namespace = nil
104
+ @classes, @factory_singletons = {}, {}
105
+ @code, @namespace = nil, nil
106
+ @limits, @usage = {}, {}
104
107
  @options = options
105
- @usage = {}
106
108
  @used = {}
107
109
  end
108
110
 
@@ -143,7 +145,7 @@ class LtdTemplate
143
145
  self
144
146
  end
145
147
 
146
- # Change default factory classes for this template.
148
+ # Override default factory classes for this template instance.
147
149
  #
148
150
  # @param classes [Hash] A hash of factory symbols and corresponding
149
151
  # classes to be instantiated.
@@ -161,12 +163,19 @@ class LtdTemplate
161
163
  # @return Returns the new code/value object.
162
164
  def factory (type, *args)
163
165
  use :factory
164
- type = @classes[type]
165
- file = type.downcase.gsub '::', '/'
166
- require file
167
- eval(type).instance(self, *args)
166
+ type = @classes[type] || @@classes[type]
167
+ if type.is_a? String
168
+ file = type.downcase.gsub '::', File::SEPARATOR
169
+ require file
170
+ eval(type).instance(self, *args)
171
+ else
172
+ type.instance(self, *args)
173
+ end
168
174
  end
169
175
 
176
+ # Shortcut for frequently used template factory :nil
177
+ def nil; @factory_singletons[:nil] || factory(:nil); end
178
+
170
179
  # Render the template.
171
180
  #
172
181
  # The options hash may include :parameters, which may be an array or
@@ -1,11 +1,11 @@
1
- class LtdTemplate; end
2
-
3
1
  # LtdTemplate::Code - Base class for LtdTemplate code/value objects
4
2
  #
5
3
  # @author Brian Katzung <briank@kappacs.com>, Kappa Computer Solutions, LLC
6
4
  # @copyright 2013 Brian Katzung and Kappa Computer Solutions, LLC
7
5
  # @license MIT License
8
6
 
7
+ class LtdTemplate; end
8
+
9
9
  class LtdTemplate::Code
10
10
 
11
11
  # @!attribute [r] tpl_methods
@@ -38,8 +38,7 @@ class LtdTemplate::Code
38
38
  # @param key [String] The (native) string for the method.
39
39
  # @return [LtdTemplate::Value::Code_Block]
40
40
  def get_item (key)
41
- (@tpl_methods.has_key? key) ? @tpl_methods[key] :
42
- @template.factory(:nil)
41
+ (@tpl_methods.has_key? key) ? @tpl_methods[key] : @template.nil
43
42
  end
44
43
 
45
44
  # Set a non-array value's method code block.
@@ -72,7 +71,7 @@ class LtdTemplate::Code
72
71
  if params = opts[:parameters]
73
72
  set_value(params.scalar? ? params.positional[0] : params)
74
73
  end
75
- @template.factory :nil
74
+ @template.nil
76
75
  end
77
76
 
78
77
  # Try to execute code-block methods bound to the object or object
@@ -91,8 +90,10 @@ class LtdTemplate::Code
91
90
  if method
92
91
  opts[:target] = self
93
92
  method.get_value opts
93
+ elsif mmproc = @template.options[:missing_method]
94
+ mmproc.call(@template, self, opts) || @template.nil
94
95
  else
95
- @template.factory :nil
96
+ @template.nil
96
97
  end
97
98
  end
98
99
 
@@ -21,7 +21,7 @@ class LtdTemplate::Code::Code_Block < LtdTemplate::Code
21
21
  def get_value (opts = {})
22
22
  values = @code.map { |part| part.get_value }.flatten
23
23
  case values.size
24
- when 0 then @template.factory :nil
24
+ when 0 then @template.nil
25
25
  when 1 then values[0]
26
26
  else @template.factory(:array).set_value(values)
27
27
  end
@@ -67,7 +67,7 @@ class LtdTemplate::Code::Variable < LtdTemplate::Code
67
67
  case opts[:method]
68
68
  when '=' then do_set opts # see LtdTemplate::Code
69
69
  when '?='
70
- if is_set? then @template.factory :nil
70
+ if is_set? then @template.nil
71
71
  else do_set opts
72
72
  end
73
73
  else target.get_value opts
@@ -37,7 +37,7 @@ class LtdTemplate::Value::Array < LtdTemplate::Code
37
37
  #
38
38
  def has_item? (key); @sarah.has_key? key; end
39
39
  def get_item (key)
40
- @sarah.has_key?(key) ? @sarah[key] : @template.factory(:nil)
40
+ @sarah.has_key?(key) ? @sarah[key] : @template.nil
41
41
  end
42
42
  def set_item (key, value)
43
43
  @sarah[key] = value
@@ -75,6 +75,9 @@ class LtdTemplate::Value::Array < LtdTemplate::Code
75
75
  end
76
76
  end
77
77
 
78
+ # Type (for :missing_method callback)
79
+ def type; :array; end
80
+
78
81
  #
79
82
  # Scalar assignment is used instead of array assignment if the
80
83
  # parameter list contains exactly one positional parameter and
@@ -175,21 +178,21 @@ class LtdTemplate::Value::Array < LtdTemplate::Code
175
178
  end
176
179
 
177
180
  def do_pop (opts)
178
- @sarah.pop || @template.factory(:nil)
181
+ @sarah.pop || @template.nil
179
182
  end
180
183
 
181
184
  def do_push (opts)
182
185
  if params = opts[:parameters] then @sarah.append! params.sarah end
183
- @template.factory :nil
186
+ @template.nil
184
187
  end
185
188
 
186
189
  def do_shift (opts)
187
- @sarah.shift || @template.factory(:nil)
190
+ @sarah.shift || @template.nil
188
191
  end
189
192
 
190
193
  def do_unshift (opts)
191
194
  if params = opts[:parameters] then @sarah.insert! params.sarah end
192
- @template.factory :nil
195
+ @template.nil
193
196
  end
194
197
 
195
198
  protected
@@ -201,7 +204,7 @@ class LtdTemplate::Value::Array < LtdTemplate::Code
201
204
  value.is_a? Array
202
205
  return @template.factory(:array).set_from_hash value if
203
206
  value.is_a? Hash
204
- @template.factory :nil
207
+ @template.nil
205
208
  end
206
209
 
207
210
  end
@@ -37,6 +37,9 @@ class LtdTemplate::Value::Boolean < LtdTemplate::Code
37
37
  end
38
38
  end
39
39
 
40
+ # Type (for :missing_method callback)
41
+ def type; :boolean; end
42
+
40
43
  # Implement +/| (or):
41
44
  # bool|(bool1, ..., boolN)
42
45
  # True if ANY boolean is true. Evaluates {} blocks until true.
@@ -37,4 +37,7 @@ class LtdTemplate::Value::Code_Block < LtdTemplate::Code
37
37
  end
38
38
  end
39
39
 
40
+ # Type (for :missing_method callback)
41
+ def type; :code_block; end
42
+
40
43
  end
@@ -72,8 +72,8 @@ class LtdTemplate::Value::Namespace < LtdTemplate::Value::Array
72
72
  when 'if' then do_if opts
73
73
  when 'loop' then do_loop opts
74
74
  when 'method' then method_string
75
- when 'nil' then @template.factory :nil
76
- when 'target' then @target || @template.factory(:nil)
75
+ when 'nil' then @template.nil
76
+ when 'target' then @target || @template.nil
77
77
  when 'true' then @template.factory :boolean, true
78
78
  when 'use' then do_use opts
79
79
  when 'var' then do_add_names opts
@@ -81,18 +81,21 @@ class LtdTemplate::Value::Namespace < LtdTemplate::Value::Array
81
81
  end
82
82
  end
83
83
 
84
+ # Type (for :missing_method callback)
85
+ def type; :namespace; end
86
+
84
87
  # Add new namespace names with nil or specific values
85
88
  #
86
89
  def do_add_names (opts)
87
90
  params = opts[:parameters]
88
91
  if params.positional.size
89
- tnil = @template.factory :nil
92
+ tnil = @template.nil
90
93
  params.positional.each { |item| set_item(item.to_native, tnil) }
91
94
  end
92
95
  if params.named.size
93
96
  params.named.each { |item, val| set_item(item, val) }
94
97
  end
95
- @template.factory :nil
98
+ @template.nil
96
99
  end
97
100
 
98
101
  # Implement conditionals
@@ -111,7 +114,7 @@ class LtdTemplate::Value::Namespace < LtdTemplate::Value::Array
111
114
  return e2.get_value(:method => 'call') if e1.to_boolean
112
115
  end
113
116
  end
114
- @template.factory :nil
117
+ @template.nil
115
118
  end
116
119
 
117
120
  def do_loop (opts)
@@ -141,7 +144,7 @@ class LtdTemplate::Value::Namespace < LtdTemplate::Value::Array
141
144
  result.kind_of? String
142
145
  end
143
146
  end
144
- tpl.factory :nil
147
+ tpl.nil
145
148
  end
146
149
 
147
150
  end
@@ -20,6 +20,9 @@ class LtdTemplate::Value::Nil < LtdTemplate::Code
20
20
  end
21
21
  end
22
22
 
23
+ # Type (for :missing_method callback)
24
+ def type; :nil; end
25
+
23
26
  def to_boolean; false; end
24
27
  def to_native; ''; end
25
28
  def to_text; ''; end
@@ -45,6 +45,9 @@ class LtdTemplate::Value::Number < LtdTemplate::Code
45
45
  end
46
46
  end
47
47
 
48
+ # Type (for :missing_method callback)
49
+ def type; :number; end
50
+
48
51
  # Implement sequential operations (+, *, /, %, &, |, ^)
49
52
  def do_sequential (opts = {}, &block)
50
53
  if params = opts[:parameters]
@@ -48,6 +48,9 @@ class LtdTemplate::Value::String < LtdTemplate::Code
48
48
  end
49
49
  end
50
50
 
51
+ # Type (for :missing_method callback)
52
+ def type; :string; end
53
+
51
54
  def do_add (opts)
52
55
  combined = @value
53
56
  if params = opts[:parameters]
data/ltdtemplate.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "ltdtemplate"
3
- s.version = "0.2.2"
4
- s.date = "2013-07-29"
3
+ s.version = "0.2.3"
4
+ s.date = "2013-08-04"
5
5
  s.authors = ["Brian Katzung"]
6
6
  s.email = ["briank@kappacs.com"]
7
7
  s.homepage = "http://rubygems.org/gems/ltdtemplate"
data/test/09use.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'minitest/autorun'
2
+ require 'ltdtemplate'
3
+
4
+ class TestLtdTemplate_09 < MiniTest::Unit::TestCase
5
+
6
+ def test_use1
7
+ loader = Proc.new { |tpl, name| "@#{name}=(0+(@#{name},1))" }
8
+ tpl = LtdTemplate.new :loader => loader
9
+ tpl.parse "<<$.use('ab)$.use('cd)$.use('ab)\",\".join(@ab,@cd)>>"
10
+ assert_equal "1,1", tpl.render, "use ab, cd, ab counts => 1, 1"
11
+ end
12
+
13
+ end
14
+
15
+ # END
@@ -0,0 +1,18 @@
1
+ require 'minitest/autorun'
2
+ require 'ltdtemplate'
3
+
4
+ class TestLtdTemplate_10 < MiniTest::Unit::TestCase
5
+
6
+ def test_missing_meth1
7
+ miss_meth = Proc.new do |tpl, obj, opt|
8
+ tpl.factory :string, "#{obj.type}.#{opt[:method]};"
9
+ end
10
+ tpl = LtdTemplate.new :missing_method => miss_meth
11
+ tpl.parse "<<$.*.abc 1.def '.ghi>>"
12
+ assert_equal "array.abc;number.def;string.ghi;", tpl.render,
13
+ "missing method ary.abc, num.def, str.def"
14
+ end
15
+
16
+ end
17
+
18
+ # END
data/test/11classes.rb ADDED
@@ -0,0 +1,47 @@
1
+ require 'minitest/autorun'
2
+ require 'ltdtemplate'
3
+ require 'ltdtemplate/value/nil'
4
+
5
+ class TestLtdTemplate_11 < MiniTest::Unit::TestCase
6
+
7
+ def test_classes1
8
+ LtdTemplate.set_classes :nil => Nil1
9
+ tpl = LtdTemplate.new
10
+ tpl.parse '<<$.nil.type>>'
11
+ assert_equal 'nil1', tpl.render, 'nil type after class class change'
12
+
13
+ tpl = LtdTemplate.new
14
+ tpl.set_classes :nil => Nil2
15
+ tpl.parse '<<$.nil.type>>'
16
+ assert_equal 'nil2', tpl.render, 'nil type after instance class change'
17
+
18
+ tpl = LtdTemplate.new
19
+ tpl.parse '<<$.nil.type>>'
20
+ assert_equal 'nil1', tpl.render, 'recheck nil type after class change'
21
+ end
22
+
23
+ end
24
+
25
+ class Nil1 < LtdTemplate::Value::Nil
26
+
27
+ def get_value (opts = {})
28
+ case opts[:method]
29
+ when 'type' then @template.factory :string, 'nil1'
30
+ else super
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ class Nil2 < LtdTemplate::Value::Nil
37
+
38
+ def get_value (opts = {})
39
+ case opts[:method]
40
+ when 'type' then @template.factory :string, 'nil2'
41
+ else super
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+ # END
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 2
9
- version: 0.2.2
8
+ - 3
9
+ version: 0.2.3
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: 2013-07-29 00:00:00 -05:00
17
+ date: 2013-08-04 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
@@ -49,6 +49,7 @@ files:
49
49
  - CHANGELOG
50
50
  - RESOURCES
51
51
  - TEMPLATE_MANUAL.html
52
+ - test/10missing_meth.rb
52
53
  - test/00class.rb
53
54
  - test/04number.rb
54
55
  - test/05string.rb
@@ -58,6 +59,8 @@ files:
58
59
  - test/08interpolate.rb
59
60
  - test/01instance.rb
60
61
  - test/02tpl_literal.rb
62
+ - test/09use.rb
63
+ - test/11classes.rb
61
64
  has_rdoc: true
62
65
  homepage: http://rubygems.org/gems/ltdtemplate
63
66
  licenses:
@@ -91,6 +94,7 @@ signing_key:
91
94
  specification_version: 3
92
95
  summary: A resource-limitable, textual templating system
93
96
  test_files:
97
+ - test/10missing_meth.rb
94
98
  - test/00class.rb
95
99
  - test/04number.rb
96
100
  - test/05string.rb
@@ -100,3 +104,5 @@ test_files:
100
104
  - test/08interpolate.rb
101
105
  - test/01instance.rb
102
106
  - test/02tpl_literal.rb
107
+ - test/09use.rb
108
+ - test/11classes.rb