graft 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/graft/callback.rb +41 -0
- data/lib/graft/hook.rb +136 -0
- data/lib/graft/hook_point.rb +410 -0
- data/lib/graft/stack.rb +24 -0
- data/lib/graft.rb +5 -0
- metadata +36 -109
- data/README.rdoc +0 -138
- data/Rakefile +0 -43
- data/lib/graft/core_ext/hash.rb +0 -9
- data/lib/graft/json/attribute.rb +0 -18
- data/lib/graft/json/model.rb +0 -28
- data/lib/graft/json.rb +0 -14
- data/lib/graft/model.rb +0 -39
- data/lib/graft/version.rb +0 -13
- data/lib/graft/xml/attribute.rb +0 -55
- data/lib/graft/xml/model.rb +0 -49
- data/lib/graft/xml/type.rb +0 -91
- data/lib/graft/xml.rb +0 -27
- data/test/test_helper.rb +0 -38
- data/test/unit/core_ext/hash_test.rb +0 -29
- data/test/unit/json/attribute_test.rb +0 -51
- data/test/unit/json/model_test.rb +0 -86
- data/test/unit/xml/attribute_test.rb +0 -161
- data/test/unit/xml/model_test.rb +0 -173
- data/test/unit/xml/type_test.rb +0 -65
metadata
CHANGED
@@ -1,123 +1,50 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: graft
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
|
-
authors:
|
7
|
-
-
|
6
|
+
authors:
|
7
|
+
- Loic Nageleisen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
- !ruby/object:Gem::Dependency
|
16
|
-
name: hpricot
|
17
|
-
type: :runtime
|
18
|
-
version_requirement:
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">="
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.6.164
|
24
|
-
version:
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: tzinfo
|
27
|
-
type: :runtime
|
28
|
-
version_requirement:
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: 0.3.12
|
34
|
-
version:
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
name: builder
|
37
|
-
type: :runtime
|
38
|
-
version_requirement:
|
39
|
-
version_requirements: !ruby/object:Gem::Requirement
|
40
|
-
requirements:
|
41
|
-
- - ">="
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: 2.1.2
|
44
|
-
version:
|
45
|
-
- !ruby/object:Gem::Dependency
|
46
|
-
name: activesupport
|
47
|
-
type: :runtime
|
48
|
-
version_requirement:
|
49
|
-
version_requirements: !ruby/object:Gem::Requirement
|
50
|
-
requirements:
|
51
|
-
- - ">="
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: "2.0"
|
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:
|
65
|
-
description:
|
66
|
-
email: reaganpr@gmail.com
|
11
|
+
date: 2024-05-17 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Vendor-independent collaborative hooking library for Ruby
|
14
|
+
email: loic.nageleisen@gmail.com
|
67
15
|
executables: []
|
68
|
-
|
69
16
|
extensions: []
|
70
|
-
|
71
|
-
|
72
|
-
-
|
73
|
-
|
74
|
-
-
|
75
|
-
-
|
76
|
-
- lib/graft/
|
77
|
-
|
78
|
-
|
79
|
-
-
|
80
|
-
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
- lib/graft/xml/type.rb
|
85
|
-
- lib/graft/xml.rb
|
86
|
-
- test/test_helper.rb
|
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
|
93
|
-
has_rdoc: true
|
94
|
-
homepage: http://sneaq.net/
|
95
|
-
licenses: []
|
96
|
-
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/graft.rb
|
20
|
+
- lib/graft/callback.rb
|
21
|
+
- lib/graft/hook.rb
|
22
|
+
- lib/graft/hook_point.rb
|
23
|
+
- lib/graft/stack.rb
|
24
|
+
homepage: https://rubygems.org/gems/graft
|
25
|
+
licenses:
|
26
|
+
- BSD-3-Clause
|
27
|
+
- Apache-2.0
|
28
|
+
metadata:
|
29
|
+
rubygems_mfa_required: 'true'
|
30
|
+
source_code_uri: https://github.com/DataDog/graft-rb
|
97
31
|
post_install_message:
|
98
|
-
rdoc_options:
|
99
|
-
|
100
|
-
- README.rdoc
|
101
|
-
require_paths:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
102
34
|
- lib
|
103
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
-
requirements:
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
105
37
|
- - ">="
|
106
|
-
- !ruby/object:Gem::Version
|
107
|
-
version:
|
108
|
-
|
109
|
-
|
110
|
-
requirements:
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '2.5'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
111
42
|
- - ">="
|
112
|
-
- !ruby/object:Gem::Version
|
113
|
-
version:
|
114
|
-
version:
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
115
45
|
requirements: []
|
116
|
-
|
117
|
-
rubyforge_project:
|
118
|
-
rubygems_version: 1.3.5
|
46
|
+
rubygems_version: 3.4.19
|
119
47
|
signing_key:
|
120
|
-
specification_version:
|
121
|
-
summary:
|
48
|
+
specification_version: 4
|
49
|
+
summary: Standardised hook interface
|
122
50
|
test_files: []
|
123
|
-
|
data/README.rdoc
DELETED
@@ -1,138 +0,0 @@
|
|
1
|
-
= Graft
|
2
|
-
|
3
|
-
== Description
|
4
|
-
|
5
|
-
Graft provides an easy way to map XML and JSON data onto your Ruby classes.
|
6
|
-
|
7
|
-
== Installation
|
8
|
-
|
9
|
-
Stable:
|
10
|
-
|
11
|
-
$ sudo gem install graft
|
12
|
-
|
13
|
-
Bleeding edge:
|
14
|
-
|
15
|
-
$ git clone git://github.com/reagent/graft.git
|
16
|
-
$ cd graft && rake gem
|
17
|
-
$ sudo gem install pkg/graft-<version>.gem
|
18
|
-
|
19
|
-
== Usage
|
20
|
-
|
21
|
-
When interacting with APIs, it's quite often the case that the data returned as
|
22
|
-
part of the response is represented either as XML or JSON. The Graft library makes
|
23
|
-
turning that data into Ruby objects pretty simple. This code was an extraction from
|
24
|
-
my work on both the Fleakr[http://reagent.github.com/fleakr] and
|
25
|
-
Etsy[http://github.com/reagent/etsy] gems.
|
26
|
-
|
27
|
-
=== Mapping XML
|
28
|
-
|
29
|
-
If you want to use Graft in XML mode, you'll need to include the right library:
|
30
|
-
|
31
|
-
require 'rubygems'
|
32
|
-
require 'graft/xml'
|
33
|
-
|
34
|
-
Once that is set up, you can take an XML string like this:
|
35
|
-
|
36
|
-
<rsp>
|
37
|
-
<user nsid="3">reagent</user>
|
38
|
-
<name>Patrick Reagan</name>
|
39
|
-
</rsp>
|
40
|
-
|
41
|
-
And map it onto a Ruby class:
|
42
|
-
|
43
|
-
class User
|
44
|
-
include Graft
|
45
|
-
|
46
|
-
attribute :name
|
47
|
-
attribute :username, :from => 'user'
|
48
|
-
attribute :id, :from => 'user@nsid', :type => :integer
|
49
|
-
end
|
50
|
-
|
51
|
-
There are a couple of ways to pull this data into the +User+ object. The simplest
|
52
|
-
is from the constructor:
|
53
|
-
|
54
|
-
user = User.new(xml)
|
55
|
-
|
56
|
-
There is also a +populate_from+ instance method that will do the same:
|
57
|
-
|
58
|
-
user = User.new
|
59
|
-
user.populate_from(xml)
|
60
|
-
|
61
|
-
This second method is useful if the data you want in your Ruby object comes from 2
|
62
|
-
separate XML files. Accessing it is simple:
|
63
|
-
|
64
|
-
>> user.name # => "Patrick Reagan"
|
65
|
-
>> user.username # => "reagent"
|
66
|
-
>> user.id # => 3
|
67
|
-
|
68
|
-
=== Mapping JSON
|
69
|
-
|
70
|
-
The process of mapping JSON is similar to XML, except:
|
71
|
-
|
72
|
-
* You don't need to declare the type of the attribute
|
73
|
-
* You need to provide a full path to the JSON value
|
74
|
-
|
75
|
-
To get started, include the correct library:
|
76
|
-
|
77
|
-
require 'rubygems'
|
78
|
-
require 'graft/json'
|
79
|
-
|
80
|
-
Then for simple JSON data:
|
81
|
-
|
82
|
-
{
|
83
|
-
"rsp": {
|
84
|
-
"user_id": 3,
|
85
|
-
"username": "reagent",
|
86
|
-
"name": "Patrick Reagan"
|
87
|
-
}
|
88
|
-
}
|
89
|
-
|
90
|
-
You can map it in a similar way:
|
91
|
-
|
92
|
-
class User
|
93
|
-
include Graft
|
94
|
-
|
95
|
-
attribute :name, :from => 'rsp/name'
|
96
|
-
attribute :username, :from => 'rsp/username'
|
97
|
-
attribute :id, :from => 'rsp/user_id'
|
98
|
-
end
|
99
|
-
|
100
|
-
Again, you can initialize the values from both the constructor:
|
101
|
-
|
102
|
-
user = User.new(json)
|
103
|
-
|
104
|
-
Or the +populate_from+ method:
|
105
|
-
|
106
|
-
user = User.new
|
107
|
-
user.populate_from(json)
|
108
|
-
|
109
|
-
The results are the same:
|
110
|
-
|
111
|
-
>> user.name # => "Patrick Reagan"
|
112
|
-
>> user.username # => "reagent"
|
113
|
-
>> user.id # => 3
|
114
|
-
|
115
|
-
== License
|
116
|
-
|
117
|
-
Copyright (c) 2009 Patrick Reagan (reaganpr@gmail.com)
|
118
|
-
|
119
|
-
Permission is hereby granted, free of charge, to any person
|
120
|
-
obtaining a copy of this software and associated documentation
|
121
|
-
files (the "Software"), to deal in the Software without
|
122
|
-
restriction, including without limitation the rights to use,
|
123
|
-
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
124
|
-
copies of the Software, and to permit persons to whom the
|
125
|
-
Software is furnished to do so, subject to the following
|
126
|
-
conditions:
|
127
|
-
|
128
|
-
The above copyright notice and this permission notice shall be
|
129
|
-
included in all copies or substantial portions of the Software.
|
130
|
-
|
131
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
132
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
133
|
-
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
134
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
135
|
-
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
136
|
-
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
137
|
-
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
138
|
-
OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'rake/gempackagetask'
|
3
|
-
require 'rake/testtask'
|
4
|
-
|
5
|
-
require 'lib/graft/version'
|
6
|
-
|
7
|
-
task :default => :test
|
8
|
-
|
9
|
-
spec = Gem::Specification.new do |s|
|
10
|
-
s.name = 'graft'
|
11
|
-
s.version = Graft::Version.to_s
|
12
|
-
s.has_rdoc = true
|
13
|
-
s.extra_rdoc_files = %w(README.rdoc)
|
14
|
-
s.rdoc_options = %w(--main README.rdoc)
|
15
|
-
s.summary = "Graft provides an easy way to map XML and JSON data onto your Ruby classes"
|
16
|
-
s.author = 'Patrick Reagan'
|
17
|
-
s.email = 'reaganpr@gmail.com'
|
18
|
-
s.homepage = 'http://sneaq.net/'
|
19
|
-
s.files = %w(README.rdoc Rakefile) + Dir.glob("{lib,test}/**/*")
|
20
|
-
|
21
|
-
s.add_dependency('hpricot', '>= 0.6.164')
|
22
|
-
s.add_dependency('tzinfo', '>= 0.3.12')
|
23
|
-
s.add_dependency('builder', '>= 2.1.2')
|
24
|
-
s.add_dependency('activesupport', '>= 2.0')
|
25
|
-
s.add_dependency('json', '>= 1.1.7')
|
26
|
-
end
|
27
|
-
|
28
|
-
Rake::GemPackageTask.new(spec) do |pkg|
|
29
|
-
pkg.gem_spec = spec
|
30
|
-
end
|
31
|
-
|
32
|
-
Rake::TestTask.new do |t|
|
33
|
-
t.libs << 'test'
|
34
|
-
t.test_files = FileList["test/**/*_test.rb"]
|
35
|
-
t.verbose = true
|
36
|
-
end
|
37
|
-
|
38
|
-
desc 'Generate the gemspec to serve this Gem from Github'
|
39
|
-
task :github do
|
40
|
-
file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
|
41
|
-
File.open(file, 'w') {|f| f << spec.to_ruby }
|
42
|
-
puts "Created gemspec: #{file}"
|
43
|
-
end
|
data/lib/graft/core_ext/hash.rb
DELETED
data/lib/graft/json/attribute.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
module Graft
|
2
|
-
module Json
|
3
|
-
class Attribute
|
4
|
-
|
5
|
-
attr_reader :name, :source
|
6
|
-
|
7
|
-
def initialize(name, source = nil)
|
8
|
-
@name = name
|
9
|
-
@source = source.nil? ? @name.to_s : source
|
10
|
-
end
|
11
|
-
|
12
|
-
def value_from(data)
|
13
|
-
data/source
|
14
|
-
end
|
15
|
-
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
data/lib/graft/json/model.rb
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
module Graft
|
2
|
-
module Json
|
3
|
-
module Model
|
4
|
-
|
5
|
-
module ClassMethods
|
6
|
-
|
7
|
-
def attribute(name, options = {})
|
8
|
-
source = options[:from]
|
9
|
-
|
10
|
-
self.attributes << Graft::Json::Attribute.new(name, source)
|
11
|
-
class_eval "attr_accessor :#{name}"
|
12
|
-
end
|
13
|
-
|
14
|
-
def data_from(json_or_hash)
|
15
|
-
json_or_hash.is_a?(String) ? JSON.parse(json_or_hash) : json_or_hash
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.included(other)
|
21
|
-
other.send(:extend, Graft::Model::ClassMethods)
|
22
|
-
other.send(:extend, Graft::Json::Model::ClassMethods)
|
23
|
-
other.send(:include, Graft::Model::InstanceMethods)
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
data/lib/graft/json.rb
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
$:.unshift File.dirname(__FILE__) + '/..'
|
2
|
-
|
3
|
-
require 'json'
|
4
|
-
require 'graft/core_ext/hash'
|
5
|
-
|
6
|
-
require 'graft/model'
|
7
|
-
require 'graft/json/attribute'
|
8
|
-
require 'graft/json/model'
|
9
|
-
|
10
|
-
module Graft
|
11
|
-
def self.included(other)
|
12
|
-
other.send(:include, Graft::Json::Model)
|
13
|
-
end
|
14
|
-
end
|
data/lib/graft/model.rb
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
module Graft
|
2
|
-
module Model
|
3
|
-
|
4
|
-
module ClassMethods
|
5
|
-
def attributes
|
6
|
-
@attributes ||= []
|
7
|
-
end
|
8
|
-
|
9
|
-
def collection_from(data_source, node)
|
10
|
-
(data_from(data_source)/node).map {|n| new n }
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
module InstanceMethods
|
15
|
-
|
16
|
-
def initialize(source_data = nil)
|
17
|
-
self.source_data = source_data
|
18
|
-
self.populate_from(self.source_data) unless self.source_data.nil?
|
19
|
-
end
|
20
|
-
|
21
|
-
def source_data=(source_data)
|
22
|
-
@source_data = self.class.data_from(source_data)
|
23
|
-
end
|
24
|
-
|
25
|
-
def source_data
|
26
|
-
@source_data
|
27
|
-
end
|
28
|
-
|
29
|
-
def populate_from(data_source)
|
30
|
-
self.class.attributes.each do |attribute|
|
31
|
-
value = attribute.value_from(self.class.data_from(data_source))
|
32
|
-
self.send("#{attribute.name}=".to_sym, value) unless value.nil?
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
end
|
data/lib/graft/version.rb
DELETED
data/lib/graft/xml/attribute.rb
DELETED
@@ -1,55 +0,0 @@
|
|
1
|
-
module Graft
|
2
|
-
module Xml
|
3
|
-
class Attribute
|
4
|
-
|
5
|
-
# TODO: Refactor the location / attribute logic into a Source class
|
6
|
-
|
7
|
-
attr_reader :name, :sources
|
8
|
-
|
9
|
-
def initialize(name, type = :string, sources = nil)
|
10
|
-
@name = name.to_sym
|
11
|
-
@type = type
|
12
|
-
|
13
|
-
@sources = Array(sources)
|
14
|
-
@sources << @name.to_s if @sources.empty?
|
15
|
-
end
|
16
|
-
|
17
|
-
def type_class
|
18
|
-
"Graft::Xml::Type::#{@type.to_s.camelize}".constantize
|
19
|
-
end
|
20
|
-
|
21
|
-
def split(source)
|
22
|
-
location, attribute = source.split('@')
|
23
|
-
location = self.name.to_s if location.blank?
|
24
|
-
|
25
|
-
[location, attribute]
|
26
|
-
end
|
27
|
-
|
28
|
-
def node_for(document, source)
|
29
|
-
document.at(location(source)) || document.search("//[@#{attribute(source)}]").first
|
30
|
-
end
|
31
|
-
|
32
|
-
def attribute(source)
|
33
|
-
location, attribute = source.split('@')
|
34
|
-
attribute || location
|
35
|
-
end
|
36
|
-
|
37
|
-
def location(source)
|
38
|
-
split(source).first
|
39
|
-
end
|
40
|
-
|
41
|
-
def value_from(document)
|
42
|
-
values = sources.map do |source|
|
43
|
-
node = node_for(document, source)
|
44
|
-
if !node.nil?
|
45
|
-
possible_values = [node.attributes[attribute(source)], node.inner_text]
|
46
|
-
possible_values.detect {|value| !value.blank? }
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
type_class.new(values.compact.first).value
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
data/lib/graft/xml/model.rb
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
module Graft
|
2
|
-
module Xml
|
3
|
-
module Model
|
4
|
-
|
5
|
-
module ClassMethods
|
6
|
-
|
7
|
-
def attribute(name, options = {})
|
8
|
-
source = options[:from]
|
9
|
-
type = options[:type] || :string
|
10
|
-
|
11
|
-
self.attributes << Graft::Xml::Attribute.new(name, type, source)
|
12
|
-
class_eval "attr_accessor :#{name}"
|
13
|
-
end
|
14
|
-
|
15
|
-
def data_from(xml_or_document)
|
16
|
-
xml_or_document.is_a?(String) ? Hpricot.XML(xml_or_document) : xml_or_document
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
module InstanceMethods
|
22
|
-
|
23
|
-
def to_hash
|
24
|
-
self.class.attributes.inject({}) {|h,a| h.merge(a.name.to_s => send(a.name)) }
|
25
|
-
end
|
26
|
-
|
27
|
-
def to_xml(tag_name)
|
28
|
-
xml = Builder::XmlMarkup.new
|
29
|
-
xml.instruct!
|
30
|
-
xml.tag! tag_name do
|
31
|
-
to_hash.each do |attribute, value|
|
32
|
-
xml.tag! attribute, value
|
33
|
-
end
|
34
|
-
end
|
35
|
-
xml.target!
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
|
40
|
-
def self.included(other)
|
41
|
-
other.send(:extend, Graft::Model::ClassMethods)
|
42
|
-
other.send(:extend, Graft::Xml::Model::ClassMethods)
|
43
|
-
other.send(:include, Graft::Model::InstanceMethods)
|
44
|
-
other.send(:include, Graft::Xml::Model::InstanceMethods)
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
data/lib/graft/xml/type.rb
DELETED
@@ -1,91 +0,0 @@
|
|
1
|
-
module Graft
|
2
|
-
module Xml
|
3
|
-
|
4
|
-
# = Type
|
5
|
-
#
|
6
|
-
class Type
|
7
|
-
|
8
|
-
class ConversionError < StandardError; end
|
9
|
-
|
10
|
-
def initialize(source)
|
11
|
-
@source = source
|
12
|
-
end
|
13
|
-
|
14
|
-
def convertible?
|
15
|
-
true
|
16
|
-
end
|
17
|
-
|
18
|
-
def value
|
19
|
-
raise ConversionError unless (@source.blank? || convertible?)
|
20
|
-
@source.blank? ? nil : convert
|
21
|
-
end
|
22
|
-
|
23
|
-
# = String
|
24
|
-
#
|
25
|
-
class String < Type
|
26
|
-
|
27
|
-
def convert
|
28
|
-
@source
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
# = Boolean
|
34
|
-
#
|
35
|
-
class Boolean < Type
|
36
|
-
def true_values
|
37
|
-
['true', '1']
|
38
|
-
end
|
39
|
-
|
40
|
-
def false_values
|
41
|
-
['false', '0']
|
42
|
-
end
|
43
|
-
|
44
|
-
def convertible?
|
45
|
-
(true_values + false_values).include?(@source)
|
46
|
-
end
|
47
|
-
|
48
|
-
def convert
|
49
|
-
true_values.include?(@source) ? true : false
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
# = Integer
|
54
|
-
#
|
55
|
-
class Integer < Type
|
56
|
-
def convertible?
|
57
|
-
!@source.match(/\d+/).nil?
|
58
|
-
end
|
59
|
-
|
60
|
-
def convert
|
61
|
-
@source.to_i
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
# = Time
|
66
|
-
#
|
67
|
-
class Time < Type
|
68
|
-
|
69
|
-
def timestamp?
|
70
|
-
!@source.match(/^\d+$/).nil?
|
71
|
-
end
|
72
|
-
|
73
|
-
def convert
|
74
|
-
timestamp? ? ::Time.at(@source.to_i) : ::Time.parse(@source)
|
75
|
-
end
|
76
|
-
|
77
|
-
end
|
78
|
-
|
79
|
-
# = Date
|
80
|
-
#
|
81
|
-
class Date < Type
|
82
|
-
|
83
|
-
def convert
|
84
|
-
::Date.parse(@source)
|
85
|
-
end
|
86
|
-
|
87
|
-
end
|
88
|
-
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
data/lib/graft/xml.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
$:.unshift File.dirname(__FILE__) + '/..'
|
2
|
-
|
3
|
-
require 'hpricot'
|
4
|
-
require 'builder'
|
5
|
-
require 'tzinfo'
|
6
|
-
|
7
|
-
begin
|
8
|
-
# ActiveSupport < 2.3.5
|
9
|
-
require 'active_support/core_ext/blank'
|
10
|
-
rescue NameError
|
11
|
-
# ActiveSupport >= 2.3.5 will raise a NameError exception
|
12
|
-
require 'active_support/core_ext/object/blank'
|
13
|
-
end
|
14
|
-
|
15
|
-
require 'active_support/time_with_zone'
|
16
|
-
require 'active_support/inflector'
|
17
|
-
|
18
|
-
require 'graft/model'
|
19
|
-
require 'graft/xml/type'
|
20
|
-
require 'graft/xml/attribute'
|
21
|
-
require 'graft/xml/model'
|
22
|
-
|
23
|
-
module Graft
|
24
|
-
def self.included(other)
|
25
|
-
other.send(:include, Graft::Xml::Model)
|
26
|
-
end
|
27
|
-
end
|