trick_bag 0.41.0 → 0.42.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MDVjMzY3ZGQ2ODZhM2MxOTZkMGQ2NmYyMWQ4NGIzMzQzYTFjZmQ2Mw==
4
+ OWFkZGQzZWE1N2RmY2RhNTNiZmMyZTMyODEzNWRlYjkzNTcwNDNmOA==
5
5
  data.tar.gz: !binary |-
6
- YzNiNzBkMWVjMjVlZjI5NWFhZTRkMDQ4ODY0OTY1YjdhMGFhZGU5OQ==
6
+ MWVmNTRlYzRhOTA3MWY1YWZmNWYxN2EzM2FjZmVkZDM4OTNmYjQ5Zg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MjEyNzc0NjBiNGJlODYzY2ZkZGJhMjRiNGU2OWNkMzI0YjBhNjE3NjdjYTIy
10
- ZjQwMjBlYTQwN2FjZDg4NTAyMzVmNWEyODJkOTY0OWZjYmZhYmMyYWJjYjc0
11
- YjIxMzliZTczZjA5NDZlMTNkYjk3YjlkMDU4MTBlM2IzN2M1ZmM=
9
+ MTNjZjZlYzA3N2YyZDA1ZWNjODdiZWIwOThjNjk0MTkxMzljMDA4MGY1NTQ0
10
+ NjBjMWVlNDkwOGU3ZjkzODdhZTE1NTc2NzNmYzIyMGQ4ZWFlNjkxOGU4YmJh
11
+ NDIxZDk4NTUzNTAzZjc0MGEyNjhiOGMwYjJmY2Y5ZWZkMjFjOGY=
12
12
  data.tar.gz: !binary |-
13
- MmI0NWNiMjM4MDkxMmI3MzBmMTdhODYwYTM4OGM5ZTJmYTBkOGVmMDczMDVi
14
- YzJjNjBlYTM3NTViNjBmODFhNzg3NTY0ODg1MWUyYmQ5NTkxYjA5MWE2MzE3
15
- Mjk5ZTVhYzU2MjY3YjUyNGFiMmU2MDlhMTNmNTRhOWIwNjRiZmM=
13
+ MGFhMDBjZWQ2MTVmNDIzYTY1YzY4ODE1ZjRiMzZlMWRmNWM2ZDg2OGQzMjgy
14
+ ZGRhYWUyNDE0MGU2YzYxZTkxZDZiNDAzMGM0NDM2ZjNlNDRkMjIzY2UwNzU0
15
+ OGIxODhmODA1MThkNzRiYjY0YWJkZWMwMmI3NTc3NDVmMDI1YmM=
data/RELEASE_NOTES.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## v0.42.0
2
+
3
+ * Added ErbRenderer class, which functions as a bag of values for ERB rendering.
4
+
5
+
1
6
  ## v0.41.0
2
7
 
3
8
  * Added Timing.benchmark().
@@ -0,0 +1,38 @@
1
+ module TrickBag
2
+
3
+ # ERB requires a binding from which it gets its values for substituting
4
+ # in its generated output. Often the current binding is used,
5
+ # but this often exposes far more to the template than it needs,
6
+ # and can potentially expose the running program to security risks.
7
+ #
8
+ # Please see the unit tests for examples.
9
+ #
10
+ # This class functions as a bag of values that can be passed to ERB
11
+ # for its substitutions. It subclasses OpenStruct,
12
+ # so it can be instantiated with a hash, and arbitrary new
13
+ # setter methods can be called to add new values.
14
+ #
15
+ # In the template, these values can be accessed by their method/attribute names.
16
+ #
17
+ # The 'keys' method is added as a convenience, so that you can see the
18
+ # names of the attributes in the 'bag'. It is unfortunate the OpenStruct
19
+ # doesn't have something like this already...or does it?
20
+ #
21
+ # A 'render' method is provided that calls ERB and passes the instance's binding,
22
+ # which will contain the attributes.
23
+
24
+ # Render strategy taken from
25
+ # http://stackoverflow.com/questions/8954706/render-an-erb-template-with-values-from-a-hash
26
+ class ErbRenderer < OpenStruct
27
+
28
+ def render(template)
29
+ ERB.new(template).result(binding)
30
+ end
31
+
32
+ # Useful for seeing what values are stored in the OpenStruct.
33
+ def keys
34
+ @table.keys.clone
35
+ end
36
+ end
37
+
38
+ end
@@ -52,13 +52,13 @@ module Timing
52
52
  # Returns the passed block's return value.
53
53
  #
54
54
  # e.g. benchmark('time to loop 1,000,000 times') { 1_000_000.times { 42 }; 'hi' }
55
- # outputs to the specified output stream the following string:
55
+ # outputs the following string:
56
56
  # 0.050000 0.000000 0.050000 ( 0.042376): time to loop 1,000,000 times
57
57
  # and returns: 42
58
58
  #
59
59
  # @param caption the text fragment to print after the timing data
60
60
  # @param out_stream object responding to << that will get the output string
61
- # @default $stdout
61
+ # defaults to $stdout
62
62
  # @block the block to execute and benchmark
63
63
  def benchmark(caption, out_stream = $stdout, &block)
64
64
  return_value = nil
@@ -1,3 +1,3 @@
1
1
  module TrickBag
2
- VERSION = "0.41.0"
2
+ VERSION = "0.42.0"
3
3
  end
@@ -0,0 +1,34 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ require 'trick_bag/formatters/erb_renderer'
4
+
5
+ module TrickBag
6
+
7
+ describe ErbRenderer do
8
+
9
+ specify 'it can be instantiated with a hash' do
10
+ expect(TrickBag::ErbRenderer.new(color: 'blue').color).to eq('blue')
11
+ end
12
+
13
+ specify 'it can substitute a simple value' do
14
+ expect(TrickBag::ErbRenderer.new(x: 3).render("[<%= x %>]")).to eq('[3]')
15
+ end
16
+
17
+ specify 'the template cannot see local variables' do
18
+ x = 3
19
+ expect(TrickBag::ErbRenderer.new.render("<%= x %>")).to be_empty
20
+ end
21
+
22
+ specify 'the template cannot see instance variables of other classes' do
23
+ @x = 3
24
+ expect(TrickBag::ErbRenderer.new.render("<%= @x %>")).to be_empty
25
+ end
26
+
27
+ specify 'it works when a value is added after instantiation' do
28
+ renderer = ErbRenderer.new
29
+ renderer.fruit = 'mango'
30
+ expect(renderer.render("<%= fruit %>")).to eq('mango')
31
+ end
32
+
33
+ end
34
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trick_bag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.41.0
4
+ version: 0.42.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Bennett
@@ -116,6 +116,7 @@ files:
116
116
  - lib/trick_bag/enumerables/endless_last_enumerable.rb
117
117
  - lib/trick_bag/enumerables/file_line_reader.rb
118
118
  - lib/trick_bag/enumerables/filtered_enumerable.rb
119
+ - lib/trick_bag/formatters/erb_renderer.rb
119
120
  - lib/trick_bag/formatters/formatters.rb
120
121
  - lib/trick_bag/io/temp_files.rb
121
122
  - lib/trick_bag/io/text_mode_status_updater.rb
@@ -138,6 +139,7 @@ files:
138
139
  - spec/trick_bag/enumerables/endless_last_enumerable_spec.rb
139
140
  - spec/trick_bag/enumerables/file_line_reader_spec.rb
140
141
  - spec/trick_bag/enumerables/filtered_enumerable_spec.rb
142
+ - spec/trick_bag/formatters/erb_renderer_spec.rb
141
143
  - spec/trick_bag/formatters/formatters_spec.rb
142
144
  - spec/trick_bag/io/temp_files_spec.rb
143
145
  - spec/trick_bag/io/text_mode_status_updater_spec.rb
@@ -185,6 +187,7 @@ test_files:
185
187
  - spec/trick_bag/enumerables/endless_last_enumerable_spec.rb
186
188
  - spec/trick_bag/enumerables/file_line_reader_spec.rb
187
189
  - spec/trick_bag/enumerables/filtered_enumerable_spec.rb
190
+ - spec/trick_bag/formatters/erb_renderer_spec.rb
188
191
  - spec/trick_bag/formatters/formatters_spec.rb
189
192
  - spec/trick_bag/io/temp_files_spec.rb
190
193
  - spec/trick_bag/io/text_mode_status_updater_spec.rb