hom 0.1.0 → 0.2.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.
data/hom.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'hom'
3
- s.version = '0.1.0'
3
+ s.version = '0.2.0'
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.authors = ['Tim Craft']
6
6
  s.email = ['mail@timcraft.com']
@@ -1,18 +1,28 @@
1
1
  module HOM
2
- class AttributeList < Array
2
+ class AttributeList
3
+ def initialize
4
+ @index = {}
5
+ end
6
+
7
+ def length
8
+ @index.size
9
+ end
10
+
3
11
  def html
4
- map(&:html).join
12
+ @index.values.map(&:html).join
5
13
  end
6
14
 
7
15
  def lookup(name)
8
- detect { |object| object.name.to_s == name.to_s }
16
+ @index[name.to_s]
9
17
  end
10
18
 
19
+ alias :[] :lookup
20
+
11
21
  def set(name, value = nil)
12
22
  attribute = lookup(name)
13
23
 
14
24
  if attribute.nil?
15
- self << Attribute.new(name, value)
25
+ @index[name.to_s] = Attribute.new(name, value)
16
26
  else
17
27
  attribute.value = value
18
28
  end
data/lib/hom/element.rb CHANGED
@@ -2,50 +2,48 @@ require 'cgi'
2
2
 
3
3
  module HOM
4
4
  class Element
5
- attr_reader :tag_name, :attributes
6
-
7
5
  def initialize(tag_name, attributes = nil, content = nil)
8
6
  @tag_name, @attributes, @content = tag_name, AttributeList.new.update(attributes), content
9
7
  end
10
8
 
11
9
  def html
12
- start_tag = "<#{@tag_name}#{@attributes.html}>"
13
-
14
- @content.nil? ? start_tag : "#{start_tag}#{escape(@content)}</#{@tag_name}>"
10
+ @content.nil? ? start_tag : "#{start_tag}#{encode(@content)}</#{@tag_name}>"
15
11
  end
16
12
 
17
13
  def to_s
18
14
  html_safe(html)
19
15
  end
20
16
 
21
- def method_missing(name, *args, &block)
22
- if args.empty? && block.nil?
23
- attribute = attributes.lookup(name)
24
-
25
- unless attribute.nil? || attribute.value.nil?
26
- return attribute.value
27
- end
28
- end
29
-
30
- super name, *args, &block
17
+ def lookup(name)
18
+ @attributes.lookup(name)
31
19
  end
32
20
 
21
+ alias :[] :lookup
22
+
33
23
  private
34
24
 
35
25
  def html_safe(string)
36
26
  string.respond_to?(:html_safe) ? string.html_safe : string
37
27
  end
38
28
 
39
- def escape(object)
29
+ def encode(object)
40
30
  if object.is_a?(Array)
41
- object.map { |item| escape(item) }.join
31
+ object.map { |item| encode(item) }.join
42
32
  elsif object.respond_to?(:html)
43
33
  object.html
44
34
  elsif object.respond_to?(:html_safe?) && object.html_safe?
45
35
  object.to_s
46
36
  else
47
- CGI.escapeHTML(object.to_s)
37
+ escape(object)
48
38
  end
49
39
  end
40
+
41
+ def start_tag
42
+ "<#{@tag_name}#{@attributes.html}>"
43
+ end
44
+
45
+ def escape(object)
46
+ CGI.escapeHTML(object.to_s)
47
+ end
50
48
  end
51
49
  end
@@ -19,35 +19,35 @@ class HOM::AttributeList::TestCase < MiniTest::Unit::TestCase
19
19
  @list.update(:disabled)
20
20
 
21
21
  assert_equal 1, @list.length
22
- assert_equal :disabled, @list[0].name
23
- assert_equal nil, @list[0].value
22
+ assert_equal :disabled, @list[:disabled].name
23
+ assert_equal nil, @list[:disabled].value
24
24
  end
25
25
 
26
26
  def test_update_with_hash
27
27
  @list.update({type: :text, size: 30})
28
28
 
29
29
  assert_equal 2, @list.length
30
- assert_equal :type, @list[0].name
31
- assert_equal :text, @list[0].value
32
- assert_equal :size, @list[1].name
33
- assert_equal 30, @list[1].value
30
+ assert_equal :type, @list[:type].name
31
+ assert_equal :text, @list[:type].value
32
+ assert_equal :size, @list[:size].name
33
+ assert_equal 30, @list[:size].value
34
34
  end
35
35
 
36
36
  def test_update_with_array
37
37
  @list.update([{type: :text}, :disabled])
38
38
 
39
39
  assert_equal 2, @list.length
40
- assert_equal :type, @list[0].name
41
- assert_equal :text, @list[0].value
42
- assert_equal :disabled, @list[1].name
43
- assert_equal nil, @list[1].value
40
+ assert_equal :type, @list[:type].name
41
+ assert_equal :text, @list[:type].value
42
+ assert_equal :disabled, @list[:disabled].name
43
+ assert_equal nil, @list[:disabled].value
44
44
  end
45
45
 
46
46
  def test_update_with_duplicate_symbols
47
47
  @list.update([:disabled, :disabled])
48
48
 
49
49
  assert_equal 1, @list.length
50
- assert_equal :disabled, @list[0].name
51
- assert_equal nil, @list[0].value
50
+ assert_equal :disabled, @list[:disabled].name
51
+ assert_equal nil, @list[:disabled].value
52
52
  end
53
53
  end
data/test/hom_element.rb CHANGED
@@ -39,8 +39,8 @@ class HOM::Element::TestCase < MiniTest::Unit::TestCase
39
39
  end
40
40
 
41
41
  def test_attribute_access
42
- assert_equal :text, @i3.type
43
- assert_equal 30, @i3.size
44
- assert_raises(NoMethodError) { @i3.disabled }
42
+ assert_equal :text, @i3[:type].value
43
+ assert_equal 30, @i3[:size].value
44
+ assert_equal nil, @i3[:disabled].value
45
45
  end
46
46
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-19 00:00:00.000000000 +01:00
13
- default_executable:
12
+ date: 2012-01-20 00:00:00.000000000Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: activesupport
17
- requirement: &3706480 !ruby/object:Gem::Requirement
16
+ requirement: &10256720 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ! '>='
@@ -22,7 +21,7 @@ dependencies:
22
21
  version: 3.0.3
23
22
  type: :development
24
23
  prerelease: false
25
- version_requirements: *3706480
24
+ version_requirements: *10256720
26
25
  description: A straightforward API for generating HTML programmatically
27
26
  email:
28
27
  - mail@timcraft.com
@@ -40,7 +39,6 @@ files:
40
39
  - test/hom_entity.rb
41
40
  - README.txt
42
41
  - hom.gemspec
43
- has_rdoc: true
44
42
  homepage: http://github.com/timcraft/hom
45
43
  licenses: []
46
44
  post_install_message:
@@ -61,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
59
  version: '0'
62
60
  requirements: []
63
61
  rubyforge_project:
64
- rubygems_version: 1.6.2
62
+ rubygems_version: 1.8.10
65
63
  signing_key:
66
64
  specification_version: 3
67
65
  summary: See description