graft 0.1.1 → 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/Rakefile +1 -0
- data/lib/graft/core_ext/hash.rb +9 -0
- data/lib/graft/json.rb +14 -0
- data/lib/graft/json/attribute.rb +18 -0
- data/lib/graft/json/model.rb +28 -0
- data/lib/graft/model.rb +13 -44
- data/lib/graft/version.rb +2 -2
- data/lib/graft/xml.rb +19 -0
- data/lib/graft/xml/attribute.rb +52 -0
- data/lib/graft/xml/model.rb +49 -0
- data/lib/graft/xml/type.rb +91 -0
- data/test/test_helper.rb +4 -3
- data/test/unit/core_ext/hash_test.rb +29 -0
- data/test/unit/json/attribute_test.rb +51 -0
- data/test/unit/json/model_test.rb +86 -0
- data/test/unit/xml/attribute_test.rb +161 -0
- data/test/unit/{model_test.rb → xml/model_test.rb} +65 -47
- data/test/unit/xml/type_test.rb +65 -0
- metadata +26 -9
- data/lib/graft.rb +0 -13
- data/lib/graft/attribute.rb +0 -50
- data/lib/graft/type.rb +0 -89
- data/test/unit/attribute_test.rb +0 -159
- data/test/unit/source_test.rb +0 -16
- data/test/unit/type_test.rb +0 -63
@@ -0,0 +1,65 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../../test_helper'
|
2
|
+
|
3
|
+
module Graft
|
4
|
+
module Xml
|
5
|
+
|
6
|
+
class StringTest < Test::Unit::TestCase
|
7
|
+
context "An instance of the Graft::Xml::Type::String class" do
|
8
|
+
|
9
|
+
should_convert 'a string', :to => 'a string'
|
10
|
+
should_convert '', :to => nil
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class BooleanTest < Test::Unit::TestCase
|
16
|
+
context "An instance of the Graft::Xml::Type::Boolean class" do
|
17
|
+
|
18
|
+
should_convert 'true', :to => true
|
19
|
+
should_convert 'false', :to => false
|
20
|
+
should_convert '0', :to => false
|
21
|
+
should_convert '1', :to => true
|
22
|
+
should_convert '', :to => nil
|
23
|
+
|
24
|
+
should_fail_when_converting 'foo'
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class IntegerTest < Test::Unit::TestCase
|
30
|
+
context "An instance of the Graft::Xml::Type::Integer class" do
|
31
|
+
|
32
|
+
should_convert '1', :to => 1
|
33
|
+
should_convert '', :to => nil
|
34
|
+
|
35
|
+
should_fail_when_converting 'foo'
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class DateTest < Test::Unit::TestCase
|
41
|
+
|
42
|
+
context "An instance of the Graft::Xml::Type::Date class" do
|
43
|
+
|
44
|
+
should_convert '2008-08-01', :to => Date.parse('2008-08-01')
|
45
|
+
should_convert '', :to => nil
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
class TimeTest < Test::Unit::TestCase
|
52
|
+
|
53
|
+
context "An instance of the Graft::Xml::Type::Time class" do
|
54
|
+
|
55
|
+
should_convert '2008-07-28T16:57:10Z', :to => Time.parse('2008-07-28T16:57:10Z')
|
56
|
+
should_convert '2008-12-25 18:26:55', :to => Time.parse('2008-12-25 18:26:55')
|
57
|
+
should_convert '1230274722', :to => Time.at(1230274722)
|
58
|
+
should_convert '', :to => nil
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graft
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrick Reagan
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-07
|
12
|
+
date: 2009-10-07 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -52,6 +52,16 @@ dependencies:
|
|
52
52
|
- !ruby/object:Gem::Version
|
53
53
|
version: "2.0"
|
54
54
|
version:
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: json
|
57
|
+
type: :runtime
|
58
|
+
version_requirement:
|
59
|
+
version_requirements: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 1.1.7
|
64
|
+
version:
|
55
65
|
description:
|
56
66
|
email: reaganpr@gmail.com
|
57
67
|
executables: []
|
@@ -63,16 +73,23 @@ extra_rdoc_files:
|
|
63
73
|
files:
|
64
74
|
- README.rdoc
|
65
75
|
- Rakefile
|
66
|
-
- lib/graft/
|
76
|
+
- lib/graft/core_ext/hash.rb
|
77
|
+
- lib/graft/json/attribute.rb
|
78
|
+
- lib/graft/json/model.rb
|
79
|
+
- lib/graft/json.rb
|
67
80
|
- lib/graft/model.rb
|
68
|
-
- lib/graft/type.rb
|
69
81
|
- lib/graft/version.rb
|
70
|
-
- lib/graft.rb
|
82
|
+
- lib/graft/xml/attribute.rb
|
83
|
+
- lib/graft/xml/model.rb
|
84
|
+
- lib/graft/xml/type.rb
|
85
|
+
- lib/graft/xml.rb
|
71
86
|
- test/test_helper.rb
|
72
|
-
- test/unit/
|
73
|
-
- test/unit/
|
74
|
-
- test/unit/
|
75
|
-
- test/unit/
|
87
|
+
- test/unit/core_ext/hash_test.rb
|
88
|
+
- test/unit/json/attribute_test.rb
|
89
|
+
- test/unit/json/model_test.rb
|
90
|
+
- test/unit/xml/attribute_test.rb
|
91
|
+
- test/unit/xml/model_test.rb
|
92
|
+
- test/unit/xml/type_test.rb
|
76
93
|
has_rdoc: true
|
77
94
|
homepage: http://sneaq.net/
|
78
95
|
licenses: []
|
data/lib/graft.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
$:.unshift File.dirname(__FILE__)
|
2
|
-
|
3
|
-
require 'hpricot'
|
4
|
-
require 'builder'
|
5
|
-
require 'tzinfo'
|
6
|
-
require 'active_support/core_ext/blank'
|
7
|
-
require 'active_support/time_with_zone'
|
8
|
-
require 'active_support/inflector'
|
9
|
-
|
10
|
-
|
11
|
-
require 'graft/attribute'
|
12
|
-
require 'graft/model'
|
13
|
-
require 'graft/type'
|
data/lib/graft/attribute.rb
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
module Graft
|
2
|
-
class Attribute
|
3
|
-
|
4
|
-
# TODO: Refactor the location / attribute logic into a Source class
|
5
|
-
|
6
|
-
attr_reader :name, :sources
|
7
|
-
|
8
|
-
def initialize(name, type = :string, sources = nil)
|
9
|
-
@name = name.to_sym
|
10
|
-
@type = type
|
11
|
-
|
12
|
-
@sources = Array(sources)
|
13
|
-
@sources << @name.to_s if @sources.empty?
|
14
|
-
end
|
15
|
-
|
16
|
-
def type_class
|
17
|
-
"Graft::Type::#{@type.to_s.camelize}".constantize
|
18
|
-
end
|
19
|
-
|
20
|
-
def split(source)
|
21
|
-
location, attribute = source.split('@')
|
22
|
-
location = self.name.to_s if location.blank?
|
23
|
-
|
24
|
-
[location, attribute]
|
25
|
-
end
|
26
|
-
|
27
|
-
def node_for(document, source)
|
28
|
-
document.at(location(source)) || document.search("//[@#{attribute(source)}]").first
|
29
|
-
end
|
30
|
-
|
31
|
-
def attribute(source)
|
32
|
-
location, attribute = source.split('@')
|
33
|
-
attribute || location
|
34
|
-
end
|
35
|
-
|
36
|
-
def location(source)
|
37
|
-
split(source).first
|
38
|
-
end
|
39
|
-
|
40
|
-
def value_from(document)
|
41
|
-
values = sources.map do |source|
|
42
|
-
node = node_for(document, source)
|
43
|
-
(node.attributes[attribute(source)] || node.inner_text) unless node.nil?
|
44
|
-
end
|
45
|
-
|
46
|
-
type_class.new(values.compact.first).value
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
50
|
-
end
|
data/lib/graft/type.rb
DELETED
@@ -1,89 +0,0 @@
|
|
1
|
-
module Graft
|
2
|
-
|
3
|
-
# = Type
|
4
|
-
#
|
5
|
-
class Type
|
6
|
-
|
7
|
-
class ConversionError < StandardError; end
|
8
|
-
|
9
|
-
def initialize(source)
|
10
|
-
@source = source
|
11
|
-
end
|
12
|
-
|
13
|
-
def convertible?
|
14
|
-
true
|
15
|
-
end
|
16
|
-
|
17
|
-
def value
|
18
|
-
raise ConversionError unless (@source.blank? || convertible?)
|
19
|
-
@source.blank? ? nil : convert
|
20
|
-
end
|
21
|
-
|
22
|
-
# = String
|
23
|
-
#
|
24
|
-
class String < Type
|
25
|
-
|
26
|
-
def convert
|
27
|
-
@source
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
|
-
# = Boolean
|
33
|
-
#
|
34
|
-
class Boolean < Type
|
35
|
-
def true_values
|
36
|
-
['true', '1']
|
37
|
-
end
|
38
|
-
|
39
|
-
def false_values
|
40
|
-
['false', '0']
|
41
|
-
end
|
42
|
-
|
43
|
-
def convertible?
|
44
|
-
(true_values + false_values).include?(@source)
|
45
|
-
end
|
46
|
-
|
47
|
-
def convert
|
48
|
-
true_values.include?(@source) ? true : false
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
# = Integer
|
53
|
-
#
|
54
|
-
class Integer < Type
|
55
|
-
def convertible?
|
56
|
-
!@source.match(/\d+/).nil?
|
57
|
-
end
|
58
|
-
|
59
|
-
def convert
|
60
|
-
@source.to_i
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
# = Time
|
65
|
-
#
|
66
|
-
class Time < Type
|
67
|
-
|
68
|
-
def timestamp?
|
69
|
-
!@source.match(/^\d+$/).nil?
|
70
|
-
end
|
71
|
-
|
72
|
-
def convert
|
73
|
-
timestamp? ? ::Time.at(@source.to_i) : ::Time.parse(@source)
|
74
|
-
end
|
75
|
-
|
76
|
-
end
|
77
|
-
|
78
|
-
# = Date
|
79
|
-
#
|
80
|
-
class Date < Type
|
81
|
-
|
82
|
-
def convert
|
83
|
-
::Date.parse(@source)
|
84
|
-
end
|
85
|
-
|
86
|
-
end
|
87
|
-
|
88
|
-
end
|
89
|
-
end
|
data/test/unit/attribute_test.rb
DELETED
@@ -1,159 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
-
|
3
|
-
module Graft
|
4
|
-
class AttributeTest < Test::Unit::TestCase
|
5
|
-
context "An instance of the Attribute class" do
|
6
|
-
|
7
|
-
should "know the name of the attribute" do
|
8
|
-
attr = Attribute.new('foo')
|
9
|
-
attr.name.should == :foo
|
10
|
-
end
|
11
|
-
|
12
|
-
should "have a default type class" do
|
13
|
-
attr = Attribute.new('foo')
|
14
|
-
attr.type_class.should == Graft::Type::String
|
15
|
-
end
|
16
|
-
|
17
|
-
should "have a default source" do
|
18
|
-
attr = Attribute.new(:foo)
|
19
|
-
attr.sources.should == ['foo']
|
20
|
-
end
|
21
|
-
|
22
|
-
should "be able to assign multiple sources" do
|
23
|
-
attr = Attribute.new(:foo, :string, ['foo1', 'foo2'])
|
24
|
-
attr.sources.should == ['foo1', 'foo2']
|
25
|
-
end
|
26
|
-
|
27
|
-
should "pull the location from the source" do
|
28
|
-
attr = Attribute.new('foo')
|
29
|
-
attr.location('foo').should == 'foo'
|
30
|
-
end
|
31
|
-
|
32
|
-
should "return the location when splitting" do
|
33
|
-
attr = Attribute.new('foo')
|
34
|
-
attr.split('foo').should == ['foo', nil]
|
35
|
-
end
|
36
|
-
|
37
|
-
should "return the name for the location when splitting if the location isn't specified" do
|
38
|
-
attr = Attribute.new('foo')
|
39
|
-
attr.split('@bar').should == ['foo', 'bar']
|
40
|
-
end
|
41
|
-
|
42
|
-
should "allow the setting of the location information" do
|
43
|
-
attr = Attribute.new('foo', :string, 'bar')
|
44
|
-
attr.sources.should == ['bar']
|
45
|
-
end
|
46
|
-
|
47
|
-
should "allow the setting of the attribute value" do
|
48
|
-
attr = Attribute.new('foo')
|
49
|
-
attr.attribute('@bogon').should == 'bogon'
|
50
|
-
end
|
51
|
-
|
52
|
-
should "use the location as the attribute" do
|
53
|
-
attr = Attribute.new('foo')
|
54
|
-
attr.attribute('foo').should == 'foo'
|
55
|
-
end
|
56
|
-
|
57
|
-
should "use the attribute for the attribute if specified" do
|
58
|
-
attr = Attribute.new(:id, :string, '@nsid')
|
59
|
-
attr.attribute('@nsid').should == 'nsid'
|
60
|
-
end
|
61
|
-
|
62
|
-
should "be able to retrieve the node from the path" do
|
63
|
-
document = Hpricot.XML('<name>Bassdrive</name>')
|
64
|
-
expected = document.at('name')
|
65
|
-
|
66
|
-
attr = Attribute.new(:name)
|
67
|
-
attr.node_for(document, 'name').should == expected
|
68
|
-
end
|
69
|
-
|
70
|
-
should "be able to retrieve the node that contains the specified attribute" do
|
71
|
-
document = Hpricot.XML('<user id="1337" />')
|
72
|
-
expected = document.at('user')
|
73
|
-
|
74
|
-
attr = Attribute.new(:id)
|
75
|
-
attr.node_for(document, '@id').should == expected
|
76
|
-
end
|
77
|
-
|
78
|
-
should "be able to retrieve the node for the specified attribute" do
|
79
|
-
document = Hpricot.XML('<user nsid="1337" />')
|
80
|
-
expected = document.at('user')
|
81
|
-
|
82
|
-
attr = Attribute.new(:id, :string, '@nsid')
|
83
|
-
attr.node_for(document, '@nsid').should == expected
|
84
|
-
end
|
85
|
-
|
86
|
-
should "be able to pull simple values from an XML document" do
|
87
|
-
document = Hpricot.XML('<name>Bassdrive</name>')
|
88
|
-
attr = Attribute.new(:name)
|
89
|
-
attr.value_from(document).should == 'Bassdrive'
|
90
|
-
end
|
91
|
-
|
92
|
-
should "be able to pull an attribute value from the current XML node" do
|
93
|
-
document = Hpricot.XML('<user id="1337" />')
|
94
|
-
attr = Attribute.new(:id)
|
95
|
-
attr.value_from(document).should == '1337'
|
96
|
-
end
|
97
|
-
|
98
|
-
should "be able to pull a specific attribute value from the current XML node" do
|
99
|
-
document = Hpricot.XML('<user nsid="1337" />')
|
100
|
-
attr = Attribute.new(:id, :string, '@nsid')
|
101
|
-
attr.value_from(document).should == '1337'
|
102
|
-
end
|
103
|
-
|
104
|
-
should "be able to pull an attribute value for a node and attribute" do
|
105
|
-
document = Hpricot.XML('<station><genre slug="dnb">Drum & Bass</genre></station>')
|
106
|
-
attr = Attribute.new(:slug, :string, 'station/genre@slug')
|
107
|
-
attr.value_from(document).should == 'dnb'
|
108
|
-
end
|
109
|
-
|
110
|
-
should "be able to pull a value from a nested XML node" do
|
111
|
-
document = Hpricot.XML('<rsp><user>blip</user></rsp>')
|
112
|
-
attr = Attribute.new(:user)
|
113
|
-
attr.value_from(document).should == 'blip'
|
114
|
-
end
|
115
|
-
|
116
|
-
should "return nil if it cannot find the specified node" do
|
117
|
-
document = Hpricot.XML('<user id="1" />')
|
118
|
-
attr = Attribute.new(:photoset, :string, '@nsid')
|
119
|
-
attr.value_from(document).should be(nil)
|
120
|
-
end
|
121
|
-
|
122
|
-
should "be able to try a series of nodes to find a value" do
|
123
|
-
document = Hpricot.XML('<photoid>123</photoid>')
|
124
|
-
|
125
|
-
attr = Attribute.new(:id, :string, ['photo@nsid', 'photoid'])
|
126
|
-
attr.value_from(document).should == '123'
|
127
|
-
end
|
128
|
-
|
129
|
-
should "be able to convert an integer value" do
|
130
|
-
document = Hpricot.XML('<id>1</id>')
|
131
|
-
|
132
|
-
attr = Attribute.new(:id, :integer)
|
133
|
-
attr.value_from(document).should == 1
|
134
|
-
end
|
135
|
-
|
136
|
-
should "be able to convert a boolean value" do
|
137
|
-
document = Hpricot.XML('<active>true</active>')
|
138
|
-
|
139
|
-
attr = Attribute.new(:active, :boolean)
|
140
|
-
attr.value_from(document).should == true
|
141
|
-
end
|
142
|
-
|
143
|
-
should "be able to convert a date value" do
|
144
|
-
document = Hpricot.XML('<due_on>2009-08-01</due_on>')
|
145
|
-
|
146
|
-
attr = Attribute.new(:due_on, :date)
|
147
|
-
attr.value_from(document).should == Date.parse('2009-08-01')
|
148
|
-
end
|
149
|
-
|
150
|
-
should "be able to convert a time value" do
|
151
|
-
document = Hpricot.XML('<created_at>2009-08-01 00:00:00</created_at>')
|
152
|
-
|
153
|
-
attr = Attribute.new(:created_at, :time)
|
154
|
-
attr.value_from(document).should == Time.parse('2009-08-01 00:00:00')
|
155
|
-
end
|
156
|
-
|
157
|
-
end
|
158
|
-
end
|
159
|
-
end
|
data/test/unit/source_test.rb
DELETED