shortcode 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- shortcode (0.4.1)
4
+ shortcode (0.4.2)
5
5
  parslet (= 1.6.0)
6
6
 
7
7
  GEM
data/lib/shortcode.rb CHANGED
@@ -12,7 +12,7 @@ rescue LoadError; end
12
12
  module Shortcode
13
13
 
14
14
  class << self
15
- attr_writer :configuration, :presenters
15
+ attr_writer :configuration
16
16
  end
17
17
 
18
18
  def self.process(string, additional_attributes=nil)
@@ -24,15 +24,11 @@ module Shortcode
24
24
  end
25
25
 
26
26
  def self.register_presenter(presenter)
27
- [*presenter.for].each { |k| presenters[k.to_sym] = presenter }
27
+ Shortcode::Presenter.register presenter
28
28
  end
29
29
 
30
30
  private
31
31
 
32
- def self.presenters
33
- @presenters ||= {}
34
- end
35
-
36
32
  def self.configuration
37
33
  @configuration ||= Configuration.new
38
34
  end
@@ -1,5 +1,26 @@
1
1
  class Shortcode::Presenter
2
2
 
3
+ class << self
4
+ attr_writer :presenters
5
+
6
+ def presenters
7
+ @presenters ||= {}
8
+ end
9
+
10
+ def register(presenter)
11
+ validate presenter
12
+ [*presenter.for].each { |k| presenters[k.to_sym] = presenter }
13
+ end
14
+
15
+ def validate(presenter)
16
+ raise ArgumentError, "The presenter must define the class method #for" unless presenter.respond_to?(:for)
17
+ raise ArgumentError, "The presenter must define an initialize method" unless presenter.private_instance_methods(false).include?(:initialize)
18
+ %w(content attributes).each do |method|
19
+ raise ArgumentError, "The presenter must define the method ##{method}" unless presenter.method_defined?(method.to_sym)
20
+ end
21
+ end
22
+ end
23
+
3
24
  def initialize(name, attributes, content, additional_attributes)
4
25
  @attributes = attributes
5
26
  @content = content
@@ -18,8 +39,8 @@ class Shortcode::Presenter
18
39
  private
19
40
 
20
41
  def initialize_custom_presenter(name)
21
- if Shortcode.presenters.has_key? name.to_sym
22
- presenter = Shortcode.presenters[name.to_sym].new(@attributes, @content, @additional_attributes)
42
+ if Shortcode::Presenter.presenters.has_key? name.to_sym
43
+ presenter = Shortcode::Presenter.presenters[name.to_sym].new(@attributes, @content, @additional_attributes)
23
44
  @attributes = presenter.attributes
24
45
  @content = presenter.content
25
46
  end
@@ -1,3 +1,3 @@
1
1
  module Shortcode
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.2"
3
3
  end
@@ -2,45 +2,12 @@ require 'spec_helper'
2
2
  require 'parslet/rig/rspec'
3
3
  require 'pp'
4
4
 
5
- class MyPresenter
6
-
7
- def self.for
8
- :quote
9
- end
10
-
11
- def initialize(attributes, content, additional_attributes)
12
- @content = content
13
- @additional_attributes = additional_attributes
14
- end
15
-
16
- def content
17
- @content
18
- end
19
-
20
- def attributes
21
- @additional_attributes || { title: "my custom title" }
22
- end
23
- end
24
-
25
- class MultiplePresenter
26
-
27
- def self.for
28
- [:quote, :item]
29
- end
30
-
31
- def initialize(attributes, content, additional_attributes)
32
- @content = content
33
- @additional_attributes = additional_attributes
34
- end
35
-
36
- def content
37
- @content
38
- end
39
-
40
- def attributes
41
- @additional_attributes || { title: "my custom title" }
42
- end
43
- end
5
+ require 'support/presenters/my_presenter'
6
+ require 'support/presenters/multiple_presenter'
7
+ require 'support/presenters/missing_for_presenter'
8
+ require 'support/presenters/missing_initialize_presenter'
9
+ require 'support/presenters/missing_content_presenter'
10
+ require 'support/presenters/missing_attributes_presenter'
44
11
 
45
12
  describe Shortcode::Presenter do
46
13
 
@@ -90,4 +57,40 @@ describe Shortcode::Presenter do
90
57
 
91
58
  end
92
59
 
60
+ context "presenter validation" do
61
+
62
+ describe "missing #for class method" do
63
+
64
+ it "raises an exception" do
65
+ expect { Shortcode.register_presenter MissingForPresenter }.to raise_error(ArgumentError, "The presenter must define the class method #for")
66
+ end
67
+
68
+ end
69
+
70
+ describe "missing #initialize method" do
71
+
72
+ it "raises an exception" do
73
+ expect { Shortcode.register_presenter MissingInitializePresenter }.to raise_error(ArgumentError, "The presenter must define an initialize method")
74
+ end
75
+
76
+ end
77
+
78
+ describe "missing #content method" do
79
+
80
+ it "raises an exception" do
81
+ expect { Shortcode.register_presenter MissingContentPresenter }.to raise_error(ArgumentError, "The presenter must define the method #content")
82
+ end
83
+
84
+ end
85
+
86
+ describe "missing #attributes method" do
87
+
88
+ it "raises an exception" do
89
+ expect { Shortcode.register_presenter MissingAttributesPresenter }.to raise_error(ArgumentError, "The presenter must define the method #attributes")
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+
93
96
  end
data/spec/spec_helper.rb CHANGED
@@ -18,7 +18,7 @@ RSpec.configure do |config|
18
18
  config.order = "random"
19
19
 
20
20
  config.before(:each) do
21
- Shortcode.presenters = {}
21
+ Shortcode::Presenter.presenters = {}
22
22
  Shortcode.setup do |config|
23
23
  config.template_parser = :erb
24
24
  config.template_path = File.join File.dirname(__FILE__), "support/templates/erb"
@@ -0,0 +1,12 @@
1
+ class MissingAttributesPresenter
2
+
3
+ def initialize
4
+ end
5
+
6
+ def self.for
7
+ end
8
+
9
+ def content
10
+ end
11
+
12
+ end
@@ -0,0 +1,12 @@
1
+ class MissingContentPresenter
2
+
3
+ def initialize
4
+ end
5
+
6
+ def self.for
7
+ end
8
+
9
+ def attributes
10
+ end
11
+
12
+ end
@@ -0,0 +1,12 @@
1
+ class MissingForPresenter
2
+
3
+ def initialize
4
+ end
5
+
6
+ def content
7
+ end
8
+
9
+ def attributes
10
+ end
11
+
12
+ end
@@ -0,0 +1,12 @@
1
+ class MissingInitializePresenter
2
+
3
+ def self.for
4
+ end
5
+
6
+ def content
7
+ end
8
+
9
+ def attributes
10
+ end
11
+
12
+ end
@@ -0,0 +1,19 @@
1
+ class MultiplePresenter
2
+
3
+ def self.for
4
+ [:quote, :item]
5
+ end
6
+
7
+ def initialize(attributes, content, additional_attributes)
8
+ @content = content
9
+ @additional_attributes = additional_attributes
10
+ end
11
+
12
+ def content
13
+ @content
14
+ end
15
+
16
+ def attributes
17
+ @additional_attributes || { title: "my custom title" }
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ class MyPresenter
2
+
3
+ def self.for
4
+ :quote
5
+ end
6
+
7
+ def initialize(attributes, content, additional_attributes)
8
+ @content = content
9
+ @additional_attributes = additional_attributes
10
+ end
11
+
12
+ def content
13
+ @content
14
+ end
15
+
16
+ def attributes
17
+ @additional_attributes || { title: "my custom title" }
18
+ end
19
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shortcode
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-06-18 00:00:00.000000000 Z
12
+ date: 2014-07-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: parslet
@@ -188,6 +188,12 @@ files:
188
188
  - spec/support/fixtures/timeline_info_output.html
189
189
  - spec/support/fixtures/timeline_person.txt
190
190
  - spec/support/fixtures/timeline_person_output.html
191
+ - spec/support/presenters/missing_attributes_presenter.rb
192
+ - spec/support/presenters/missing_content_presenter.rb
193
+ - spec/support/presenters/missing_for_presenter.rb
194
+ - spec/support/presenters/missing_initialize_presenter.rb
195
+ - spec/support/presenters/multiple_presenter.rb
196
+ - spec/support/presenters/my_presenter.rb
191
197
  - spec/support/templates/erb/collapsible_list.html.erb
192
198
  - spec/support/templates/erb/item.html.erb
193
199
  - spec/support/templates/erb/quote.html.erb
@@ -261,6 +267,12 @@ test_files:
261
267
  - spec/support/fixtures/timeline_info_output.html
262
268
  - spec/support/fixtures/timeline_person.txt
263
269
  - spec/support/fixtures/timeline_person_output.html
270
+ - spec/support/presenters/missing_attributes_presenter.rb
271
+ - spec/support/presenters/missing_content_presenter.rb
272
+ - spec/support/presenters/missing_for_presenter.rb
273
+ - spec/support/presenters/missing_initialize_presenter.rb
274
+ - spec/support/presenters/multiple_presenter.rb
275
+ - spec/support/presenters/my_presenter.rb
264
276
  - spec/support/templates/erb/collapsible_list.html.erb
265
277
  - spec/support/templates/erb/item.html.erb
266
278
  - spec/support/templates/erb/quote.html.erb