texttube 6.1.0 → 7.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 610f0fe430a90ce6e8de9b697d41e81029cdd82c
4
- data.tar.gz: a334d2477567f2be643d05076866fb4fd6f41e1e
3
+ metadata.gz: 3b2a5ba68a11010904a881a8aa8d05f96cbd4cb4
4
+ data.tar.gz: bb6e54ec1473035c6b0324ad51dc885edc04bea7
5
5
  SHA512:
6
- metadata.gz: eb605ac48e32f2cb5066fddeaab12be996435e5517d205f774f4957e7530b2fd174ec0e7b64e092907f69e2418470762de03a63f0a059ff3b8cd2cbf8009ab1a
7
- data.tar.gz: c9df73120109399f0efd543c46f5c521c76f05a42bb6fde52b75e91321012d2c6cf75c8369dc2ff455abc5066b4a3e332d43fb0045e15292520f532fd805072c
6
+ metadata.gz: 7e4018b23e62df19927781d886e74b6666f5fd2756b05a9f69532fe2be9874f5beef2ca6b2c0760879f682a2731cfcc8a6b5a7eb20b365155330da3d4af528bb
7
+ data.tar.gz: 423f6494980562823c69298a183a1a30c583d41387935bc758cde142c493f6427aa2ca2be142bd1940160506771382bbebc9057f9169f2b51de9800d3b58f1d2
data/.gitignore CHANGED
@@ -22,5 +22,6 @@ vendor/
22
22
  vendor.noindex/
23
23
  coverage/
24
24
  docs/
25
+ doc/
25
26
  .yardoc/
26
27
  .bundle/
@@ -1,10 +1,21 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
3
  - 2.0.0
4
+ - 2.1.0
5
+ - 2.2.2
6
+ - jruby-19mode # JRuby in 1.9 mode
7
+ - rbx
8
+ - ruby-head
9
+ - jruby-head
5
10
 
6
11
  # whitelist
7
12
  branches:
8
13
  only:
9
14
  - master
10
15
  - develop
16
+
17
+ matrix:
18
+ allow_failures:
19
+ - rvm: jruby-head
20
+ - rvm: ruby-head
21
+ - rvm: rbx
data/CHANGES.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # CH CH CH CHANGES! #
2
2
 
3
+ ## Sunday the 14th of June 2015#
4
+
5
+ # v7.0.1 ##
6
+
7
+ * Added fences to code block in example.
8
+
9
+ ----
10
+
11
+ # v7.0.0 #
12
+
13
+ * Added more examples of how to use options.
14
+ * Cleaned up the options a lot, clarified and better tested.
15
+ * Fixed the ever moving target of the Travis-CI settings.
16
+
17
+ ----
18
+
3
19
 
4
20
  ## Friday the 12th of June 2015, v6.1.0 ##
5
21
 
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- # A sample Gemfile
1
+ RUBY_ENGINE = 'ruby' unless defined? RUBY_ENGINE
2
2
  source "https://rubygems.org"
3
3
 
4
4
  gemspec
@@ -7,19 +7,17 @@ gem "rake"
7
7
 
8
8
  group :documentation do
9
9
  gem "yard"
10
- gem "rdiscount"
10
+ gem "kramdown"
11
11
  end
12
12
 
13
13
  group :development do
14
- gem "rdiscount"
15
- gem "wirble"
14
+ unless RUBY_ENGINE == 'jruby' || RUBY_ENGINE == "rbx"
15
+ gem "pry-byebug"
16
+ end
16
17
  end
17
18
 
18
19
  group :test do
19
20
  gem "rspec"
20
21
  gem "simplecov", :require => false
21
- gem "maruku"
22
- gem "rdiscount"
23
- gem "kramdown"
24
22
  gem "rspec-its"
25
23
  end
data/README.md CHANGED
@@ -139,6 +139,115 @@ s.filter
139
139
  # => "Let me introduce <a href="iainbarnett.me.uk" title="My blog">Iain Barnett</a>. He writes Ruby code. ©2013. Let me introduce <a href="iainbarnett.me.uk" title="My blog">Iain Barnett</a>. He writes Ruby code. ©2013. "
140
140
  ```
141
141
 
142
+ ### Options ###
143
+
144
+ There are 3 types of options available:
145
+
146
+ #### Class options ####
147
+
148
+ These options are available for all instances of that class. One was used in the previous example:
149
+
150
+ ```ruby
151
+ MyString.options.merge! :number => {times: 2}
152
+ ```
153
+
154
+ #### Instance options ####
155
+
156
+ Using this type of option will mean that option is used with this instance and no other. It will override class options:
157
+
158
+ ```ruby
159
+ m = NeuS.new "abc", :order=>[:dashes, :double, :spacial]
160
+ m.filter # will use :dashes, :double, :spacial
161
+ ```
162
+
163
+ #### Method options ####
164
+
165
+ These will affect only this call of the `filter` method:
166
+
167
+ ```ruby
168
+ # this does :triple, :dashes, :spacial
169
+ m.filter :order=>[:triple, :dashes, :spacial]
170
+ # this does :dashes, :double, :spacial
171
+ m.filter
172
+ ```
173
+
174
+ Of course, if you just want to effect the order in a one off call then `m.filter :triple, :dashes, :spacial` would work just as well.
175
+
176
+ If you have several filters and you want to pass some options then you need give the name of the filter along with the options. Here's an example:
177
+
178
+ ```ruby
179
+ class FutureString < TextTube::Base
180
+ register do
181
+ filter_with :read_more do |text,options|
182
+ text + " " + options.fetch(:teaser, "READ MORE")
183
+ end
184
+ filter_with :add_title do |text,options|
185
+ title = "#{options.fetch(:title, 'My article')}\n\n"
186
+ title + text
187
+ end
188
+ end
189
+ end
190
+
191
+ future = FutureString.new "It's looking bright!"
192
+ future.filter :read_more =>{teaser: "carry on reading…"}, :add_title =>{title: "Feeling optimistic"}
193
+ ```
194
+
195
+ Which would output:
196
+
197
+ > Feeling optimistic
198
+ >
199
+ > It's looking bright! carry on reading…
200
+
201
+ You can see from this example that you can register more than one filter on the fly within a register block.
202
+
203
+
204
+ #### Overrides ####
205
+
206
+ Keep in mind that class options are overriden by instance options, and instance options (and class options) are overriden by method options.
207
+
208
+ #### Note! ####
209
+
210
+ Options are only applied if you pass a block with the 2nd options argument available. For example, this will apply options:
211
+
212
+ ```ruby
213
+ class Optional < TextTube::Base
214
+ register do
215
+ filter_with :something_with_options do |text,options|
216
+ extras = options.fetch :extra, " "
217
+ text + extras + "This should be ok."
218
+ end
219
+ end
220
+ end
221
+
222
+ This will not:
223
+
224
+ class Optional < TextTube::Base
225
+ register do
226
+ filter_with :something_without_options do |text|
227
+ text + options + "This will throw an error."
228
+ end
229
+ end
230
+ end
231
+ ```
232
+
233
+ If you don't plan to use the options then don't provide them as an argument, but don't refer to them in the block, e.g.
234
+
235
+ ```ruby
236
+ class NotBothered < TextTube::Base
237
+ register do
238
+ filter_with :something_without_options do |text|
239
+ text + "We're just not giving you the option."
240
+ end
241
+ end
242
+ end
243
+ ```
244
+
245
+
246
+ ### All of these examples have been used as specs ###
247
+
248
+ Take a look and run them.
249
+
250
+
142
251
  ### Ready made filters ###
143
252
 
144
253
  They used to come with this library, but sometimes they caused problems with installation (things like Nokogiri can be painful to install at times) so I spun them off into their own library - [TextTubeBaby](https://github.com/yb66/TextTubeBaby)!
data/Rakefile CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  desc "(Re-) generate documentation and place it in the docs/ dir."
4
4
  task :docs => :"docs:yard"
5
- namespace :docs do
5
+ namespace :doc do
6
6
  require 'yard'
7
7
  YARD::Rake::YardocTask.new do |t|
8
8
  t.files = ['lib/**/*.rb']
9
- t.options = ['-odocs/', '--no-private']
9
+ t.options = ['-odoc/', '--no-private']
10
10
  end
11
11
 
12
12
  desc "Docs including private methods."
@@ -0,0 +1,2 @@
1
+ require_relative "texttube/base.rb"
2
+ require_relative "texttube/filterable.rb"
@@ -58,11 +58,6 @@ module TextTube
58
58
 
59
59
  class << self
60
60
 
61
- # Global options. Every descendant will get these.
62
- def options
63
- @options ||= {}
64
- end
65
-
66
61
  # remove all filters
67
62
  # @todo remove methods too.
68
63
  def reset!
@@ -112,6 +107,13 @@ module TextTube
112
107
  # @private
113
108
  def inherited(subclass)
114
109
  subclass.reset!
110
+
111
+ # Per class options
112
+ subclass.instance_eval do
113
+ def options
114
+ @options ||= {}
115
+ end
116
+ end
115
117
  end
116
118
  end
117
119
 
@@ -142,8 +144,9 @@ module TextTube
142
144
  else
143
145
  order,options = order_and_options, {}
144
146
  end
145
- order = order.flatten
146
- order = @options.fetch :order, self.class.filters if order.empty?
147
+ options = self.class.options.merge(@options.merge(options))
148
+ order.flatten!
149
+ order = options.fetch :order, self.class.filters if order.empty?
147
150
  order.inject(self){|current,filter|
148
151
  send filter, current, options
149
152
  }
@@ -35,17 +35,13 @@ module TextTube
35
35
  name = name.to_sym
36
36
  filters << name unless filters.include? name
37
37
  define_method name do |current=self, options=nil|
38
- if current.respond_to? :keys
39
- options=current
40
- current=self
41
- end
42
38
  options = [options, @options, self.class.options].find{|opts|
43
39
  !opts.nil? &&
44
40
  opts.respond_to?(:keys) &&
45
41
  !opts.empty?
46
42
  } || {}
47
43
 
48
- block.call current, options[name]
44
+ block.call current, (options[name] || {})
49
45
  end
50
46
  end
51
47
 
@@ -2,5 +2,5 @@
2
2
 
3
3
  module TextTube
4
4
  # This library's version.
5
- VERSION = "6.1.0"
5
+ VERSION = "7.0.1"
6
6
  end
@@ -15,16 +15,27 @@ describe Base do
15
15
  its(:options) { should be_empty }
16
16
  end
17
17
  context "Class methods and their defaults" do
18
+ shared_examples "any instance from a kind of Base" do
19
+ it { should respond_to :reset! }
20
+ it { should respond_to :filters }
21
+ its(:filters) { should respond_to :empty? }
22
+ its(:filters) { should be_empty }
23
+ it { should respond_to :register }
24
+ it { should respond_to :inherited }
25
+ end
18
26
  subject { Base }
19
- it { should respond_to :options }
20
- its(:options) { should respond_to :empty? }
21
- its(:options) { should be_empty }
22
- it { should respond_to :reset! }
23
- it { should respond_to :filters }
24
- its(:filters) { should respond_to :empty? }
25
- its(:filters) { should be_empty }
26
- it { should respond_to :register }
27
- it { should respond_to :inherited }
27
+ it { should_not respond_to :options }
28
+ it_should_behave_like "any instance from a kind of Base"
29
+ context "when subclassed" do
30
+ before :all do
31
+ SubClassed = Class.new TextTube::Base
32
+ end
33
+ subject { SubClassed }
34
+ it { should respond_to :options }
35
+ its(:options) { should respond_to :empty? }
36
+ its(:options) { should be_empty }
37
+ it_should_behave_like "any instance from a kind of Base"
38
+ end
28
39
  end
29
40
  end
30
41
  end
@@ -5,6 +5,8 @@ require_relative "../lib/texttube/filterable.rb"
5
5
 
6
6
  module TextTube # for convenience
7
7
 
8
+ TimeNow = "2060" # According to Sir Isaac Newton's research of the Bible, the world will end in this year!
9
+
8
10
  describe "Example usage" do
9
11
  before :all do
10
12
  module AFilter
@@ -27,6 +29,18 @@ describe "Example usage" do
27
29
  end
28
30
  end
29
31
 
32
+ module AnotherFilter
33
+ extend TextTube::Filterable
34
+
35
+ filter_with :copyright do |text|
36
+ text << " ©#{TimeNow}. "
37
+ end
38
+
39
+ filter_with :number do |text,options|
40
+ text * options[:times].to_i
41
+ end
42
+ end
43
+
30
44
  class NeuS < TextTube::Base
31
45
  register BFil
32
46
  register AFilter
@@ -36,28 +50,217 @@ describe "Example usage" do
36
50
  end
37
51
  end
38
52
  end
53
+
54
+ class NewerS < TextTube::Base
55
+ register BFil
56
+ register AFilter
57
+ register do
58
+ filter_with :dashes do |text|
59
+ "---#{text}---"
60
+ end
61
+ end
62
+ end
63
+
64
+ class Optional < TextTube::Base
65
+ register do
66
+ filter_with :something_with_options do |text,options|
67
+ extras = options.fetch :extra, " "
68
+ text + extras + "This should be ok."
69
+ end
70
+ end
71
+ register do
72
+ filter_with :something_without_options do |text|
73
+ text + options + "This will throw an error."
74
+ end
75
+ end
76
+ end
77
+
78
+ class FutureString < TextTube::Base
79
+ register do
80
+ filter_with :read_more do |text,options|
81
+ text + " " + options.fetch(:teaser, "READ MORE")
82
+ end
83
+ filter_with :add_title do |text,options|
84
+ title = "#{options.fetch(:title, 'My article')}\n\n"
85
+ title + text
86
+ end
87
+ end
88
+ end
89
+
39
90
  end # before
40
91
 
41
- context "Instantiation" do
92
+ context "Simple" do
42
93
  let(:n) { NeuS.new "abc" }
94
+ let(:the_no_options_filtered_result){ "---a b ca b ca b ca b ca b ca b c---" }
43
95
  subject { n }
44
96
  it { should == "abc" }
45
97
 
46
98
  context "filtering" do
47
99
  context "With no arguments" do
48
100
  subject { n.filter }
49
- it { should == "---a b ca b ca b ca b ca b ca b c---" }
101
+ it { should == the_no_options_filtered_result }
50
102
  end
51
103
  context "Given a specific filter" do
52
104
  subject { n.filter :spacial }
53
105
  it { should == "a b c" }
106
+ it { should_not == the_no_options_filtered_result }
54
107
  end
55
108
  context "Given several specific filters" do
56
109
  subject { n.filter :spacial, :dashes, :spacial }
57
110
  it { should == "- - - a b c - - -" }
111
+ it { should_not == the_no_options_filtered_result }
58
112
  end
59
113
  end
60
114
  end
115
+ context "with options passed to the instance" do
116
+ context "To set the order" do
117
+ let(:n) { NeuS.new "abc", :order=>[:spacial, :dashes, :spacial] }
118
+ let(:the_no_options_filtered_result){ "- - - a b c - - -" }
119
+ subject { n }
120
+ it { should == "abc" }
121
+ context "filtered" do
122
+ subject { n.filter }
123
+ it { should == the_no_options_filtered_result }
124
+ context "and then given method options" do
125
+ subject { n.filter :order=>[:triple, :dashes, :spacial] }
126
+ it { should == "- - - a b c a b c a b c - - -" }
127
+ it { should_not == the_no_options_filtered_result }
128
+ end
129
+ end
130
+ context "with a different instance" do
131
+ let(:m) { NeuS.new "def" }
132
+ subject { m }
133
+ it { should == "def" }
134
+ context "filtered" do
135
+ subject { m.filter }
136
+ it { should_not == "- - - d e f - - -" }
137
+ it { should_not == the_no_options_filtered_result }
138
+ end
139
+ context "and given instance options" do
140
+ let(:k) { NeuS.new "abc", :order=>[:dashes, :double, :spacial] }
141
+ subject { k }
142
+ it { should == "abc" }
143
+ context "filtered" do
144
+ subject { k.filter }
145
+ it { should == "- - - a b c - - - - - - a b c - - -" }
146
+ it { should_not == the_no_options_filtered_result }
147
+
148
+ context "and then given method options" do
149
+ subject { k.filter :order=>[:triple, :dashes, :spacial] }
150
+ it { should == "- - - a b c a b c a b c - - -" }
151
+ it { should_not == the_no_options_filtered_result }
152
+ end
153
+ end
154
+ end
155
+ end
156
+ context "with a different class" do
157
+ let(:n) { NewerS.new "abc" }
158
+ subject { n }
159
+ it { should == "abc" }
160
+ context "filtered" do
161
+ subject { n.filter }
162
+ it { should == "---a b ca b ca b ca b ca b ca b c---" }
163
+ it { should_not == the_no_options_filtered_result }
164
+ end
165
+ end
166
+ end
167
+ end
168
+ context "with class options" do
169
+ context "To set the order" do
170
+ before :all do
171
+ NeuS.options.merge! :order=>[:spacial, :dashes, :spacial]
172
+ end
173
+ let(:n) { NeuS.new "abc" }
174
+ let(:the_no_options_filtered_result){ "- - - a b c - - -" }
175
+ subject { n }
176
+ it { should == "abc" }
177
+ context "filtered" do
178
+ subject { n.filter }
179
+ it { should == the_no_options_filtered_result }
180
+ end
181
+ context "and given method options" do
182
+ subject { n.filter :order=>[:dashes, :double, :spacial] }
183
+ it { should == "- - - a b c - - - - - - a b c - - -" }
184
+ it { should_not == the_no_options_filtered_result }
185
+ end
186
+ context "with a different instance" do
187
+ let(:m) { NeuS.new "def" }
188
+ subject { m }
189
+ it { should == "def" }
190
+ context "filtered" do
191
+ subject { m.filter }
192
+ it { should == "- - - d e f - - -" }
193
+ end
194
+ end
195
+ context "with a different class" do
196
+ let(:n) { NewerS.new "abc" }
197
+ subject { n }
198
+ it { should == "abc" }
199
+ context "filtered" do
200
+ subject { n.filter }
201
+ it { should == "---a b ca b ca b ca b ca b ca b c---" }
202
+ it { should_not == the_no_options_filtered_result }
203
+ end
204
+ end
205
+ context "and given instance options" do
206
+ let(:m) { NeuS.new "abc", :order=>[:dashes, :double, :spacial] }
207
+ subject { m }
208
+ it { should == "abc" }
209
+ context "filtered" do
210
+ subject { m.filter }
211
+ it { should == "- - - a b c - - - - - - a b c - - -" }
212
+ it { should_not == the_no_options_filtered_result }
213
+
214
+ context "and then given method options" do
215
+ subject { m.filter :order=>[:triple, :dashes, :spacial] }
216
+ it { should == "- - - a b c a b c a b c - - -" }
217
+ it { should_not == the_no_options_filtered_result }
218
+ end
219
+ end
220
+ end
221
+ end
222
+ end
223
+
224
+ describe "Naming options" do
225
+ let(:future) { FutureString.new "It's looking bright!" }
226
+ subject{
227
+ future.filter :read_more =>{teaser: "carry on reading…"}, :add_title =>{title: "Feeling optimistic"}
228
+ }
229
+ it { should == "Feeling optimistic\n\nIt's looking bright! carry on reading…" }
230
+ end
231
+
232
+ describe "Block option arguments" do
233
+ let(:optional){ Optional.new "Does this block have an options argument?" }
234
+ context "Given a block" do
235
+ context "with an options argument *and* that uses the options" do
236
+ context "with method options" do
237
+ subject { optional.filter :something_with_options, :something_with_options => {extra: " Yes, I told you so. " } }
238
+ it { should == "Does this block have an options argument? Yes, I told you so. This should be ok." }
239
+ end
240
+ context "without method options" do
241
+ subject { optional.filter :something_with_options }
242
+ it { should == "Does this block have an options argument? This should be ok." }
243
+ end
244
+ end
245
+ context "without an options argument *and* a block that uses the options" do
246
+ context "with method options" do
247
+ it "should raise an error" do
248
+ expect{
249
+ optional.filter :something_without_options, :something_without_options => {extra: " Yes, I told you so. " }
250
+ }.to raise_error(NameError)
251
+ end
252
+ end
253
+ context "without method options" do
254
+ it "should raise an error" do
255
+ expect{
256
+ optional.filter :something_without_options
257
+ }.to raise_error(NameError)
258
+ end
259
+ end
260
+ end
261
+ end
262
+
263
+ end
61
264
  end
62
265
 
63
266
  end # inconvenient
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: texttube
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.1.0
4
+ version: 7.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Iain Barnett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-12 00:00:00.000000000 Z
11
+ date: 2015-06-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Create chainable filters with ease.
14
14
  email: iainspeed @nospam@ gmail.com
@@ -22,6 +22,7 @@ files:
22
22
  - Gemfile
23
23
  - README.md
24
24
  - Rakefile
25
+ - lib/texttube.rb
25
26
  - lib/texttube/base.rb
26
27
  - lib/texttube/filterable.rb
27
28
  - lib/texttube/version.rb