element_factory 0.1.1 → 0.1.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.
@@ -10,5 +10,6 @@ module ElementFactory
10
10
 
11
11
  module Elements
12
12
  autoload :TextElement, "element_factory/elements/text_element"
13
+ autoload :InnerHtmlElement, "element_factory/elements/inner_html_element"
13
14
  end
14
15
  end
@@ -2,12 +2,17 @@ module ElementFactory
2
2
  class Element
3
3
  attr_accessor :name, :attributes
4
4
 
5
+ CONTENT_ATTRIBUTES = {text: Elements::TextElement, inner_html: Elements::InnerHtmlElement}
6
+
5
7
  def initialize(name, attributes_or_string=nil)
6
8
  @name = name.to_s
7
9
  @attributes = coerce_attributes(attributes_or_string)
8
10
 
9
- if attributes[:text].is_a?(String)
10
- add_child Elements::TextElement.new(attributes[:text])
11
+ CONTENT_ATTRIBUTES.each do |attribute, klass|
12
+ if value = attributes[attribute]
13
+ add_child klass.new(value)
14
+ attributes.delete(attribute)
15
+ end
11
16
  end
12
17
  end
13
18
 
@@ -50,10 +55,8 @@ module ElementFactory
50
55
 
51
56
  def coerce_attributes(attributes)
52
57
  case attributes
53
- when String
54
- {text: attributes}
55
- when Hash
56
- attributes
58
+ when String then {text: attributes}
59
+ when Hash then attributes
57
60
  else
58
61
  {}
59
62
  end
@@ -0,0 +1,15 @@
1
+ module ElementFactory
2
+ module Elements
3
+ class InnerHtmlElement
4
+ attr_accessor :inner_html
5
+
6
+ def initialize(inner_html)
7
+ @inner_html = inner_html
8
+ end
9
+
10
+ def to_html
11
+ inner_html
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module ElementFactory
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -19,13 +19,18 @@ describe ElementFactory::Element do
19
19
 
20
20
  specify "with a string" do
21
21
  tag = subject.new(:span, "Some text")
22
- expect(tag.attributes[:text]).to eq("Some text")
22
+ expect(tag.children.first).to be_kind_of ElementFactory::Elements::TextElement
23
23
  end
24
24
 
25
25
  specify "with a string adds a child" do
26
26
  tag = subject.new(:span, "Some text")
27
27
  expect(tag).to have(1).children
28
28
  end
29
+
30
+ it "accepts inner html" do
31
+ tag = subject.new(:span, inner_html: "<p>I'm inside and safe!</p>".html_safe)
32
+ expect(tag).to have_tag "p"
33
+ end
29
34
  end
30
35
 
31
36
  context "tag pieces" do
@@ -91,6 +96,14 @@ describe ElementFactory::Element do
91
96
  expect(element.text).to eq("Text")
92
97
  end
93
98
  end
99
+
100
+ context "omitting attributes" do
101
+ subject { described_class.new(:span, inner_html: "<p>Whut</p>") }
102
+
103
+ it "does not include an inner_html attribute" do
104
+ expect(subject).to_not have_xpath "//span[@inner_html]"
105
+ end
106
+ end
94
107
  end
95
108
 
96
109
  context "children" do
@@ -0,0 +1,15 @@
1
+ require "nokogiri"
2
+
3
+ RSpec::Matchers.define :have_tag do |tag|
4
+ match do |string|
5
+ document = Nokogiri::HTML(string.to_s)
6
+ !!document.at_css(tag)
7
+ end
8
+ end
9
+
10
+ RSpec::Matchers.define :have_xpath do |tag|
11
+ match do |string|
12
+ document = Nokogiri::HTML(string.to_s)
13
+ !!document.at_xpath(tag)
14
+ end
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: element_factory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -178,6 +178,7 @@ files:
178
178
  - element_factory.gemspec
179
179
  - lib/element_factory.rb
180
180
  - lib/element_factory/element.rb
181
+ - lib/element_factory/elements/inner_html_element.rb
181
182
  - lib/element_factory/elements/text_element.rb
182
183
  - lib/element_factory/html_attributes.rb
183
184
  - lib/element_factory/version.rb
@@ -186,6 +187,7 @@ files:
186
187
  - spec/lib/html_attributes_spec.rb
187
188
  - spec/spec_helper.rb
188
189
  - spec/support/element_helpers.rb
190
+ - spec/support/matchers/elements.rb
189
191
  homepage: ''
190
192
  licenses: []
191
193
  post_install_message:
@@ -216,4 +218,5 @@ test_files:
216
218
  - spec/lib/html_attributes_spec.rb
217
219
  - spec/spec_helper.rb
218
220
  - spec/support/element_helpers.rb
221
+ - spec/support/matchers/elements.rb
219
222
  has_rdoc: