shortcode 0.4.1 → 0.4.2
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.
- data/Gemfile.lock +1 -1
- data/lib/shortcode.rb +2 -6
- data/lib/shortcode/presenter.rb +23 -2
- data/lib/shortcode/version.rb +1 -1
- data/spec/presenter_spec.rb +42 -39
- data/spec/spec_helper.rb +1 -1
- data/spec/support/presenters/missing_attributes_presenter.rb +12 -0
- data/spec/support/presenters/missing_content_presenter.rb +12 -0
- data/spec/support/presenters/missing_for_presenter.rb +12 -0
- data/spec/support/presenters/missing_initialize_presenter.rb +12 -0
- data/spec/support/presenters/multiple_presenter.rb +19 -0
- data/spec/support/presenters/my_presenter.rb +19 -0
- metadata +14 -2
data/Gemfile.lock
CHANGED
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
|
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
|
-
|
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
|
data/lib/shortcode/presenter.rb
CHANGED
@@ -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
|
data/lib/shortcode/version.rb
CHANGED
data/spec/presenter_spec.rb
CHANGED
@@ -2,45 +2,12 @@ require 'spec_helper'
|
|
2
2
|
require 'parslet/rig/rspec'
|
3
3
|
require 'pp'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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,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.
|
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-
|
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
|