dpickett-resource_factory 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +3 -0
- data/VERSION.yml +4 -0
- data/lib/resource_factory/resource_collection.rb +16 -0
- data/lib/resource_factory/resource_factory.rb +63 -0
- data/lib/resource_factory.rb +5 -0
- data/test/lib/resource_collection_test.rb +24 -0
- data/test/lib/resource_factory_test.rb +30 -0
- data/test/test_helper.rb +9 -0
- metadata +62 -0
data/README.textile
ADDED
data/VERSION.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
class ResourceCollection < Array
|
2
|
+
class << self
|
3
|
+
def build(collection)
|
4
|
+
new(collection)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_xml
|
9
|
+
if self.size > 0
|
10
|
+
resource_name = "#{self[0].factory_name.pluralize.dasherize}"
|
11
|
+
"<#{resource_name} type=\"array\">" +
|
12
|
+
self.collect{|i| i.to_xml}.join("\r\n") +
|
13
|
+
"</#{resource_name}>"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
class ResourceFactory
|
2
|
+
class << self
|
3
|
+
attr_accessor :definitions
|
4
|
+
|
5
|
+
def define(factory_name)
|
6
|
+
default_instance = ResourceFactory.new(factory_name.to_sym)
|
7
|
+
yield(default_instance)
|
8
|
+
self.definitions ||= {}
|
9
|
+
self.definitions[factory_name.to_sym] = default_instance
|
10
|
+
end
|
11
|
+
|
12
|
+
def build(factory_name, attrs = {})
|
13
|
+
ResourceFactory.new(factory_name, attrs)
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
attr_accessor :attributes
|
20
|
+
attr_accessor :factory_name
|
21
|
+
|
22
|
+
self.definitions = {}
|
23
|
+
|
24
|
+
def initialize(factory_name, attributes = {})
|
25
|
+
self.attributes = {}
|
26
|
+
self.factory_name = factory_name.to_s
|
27
|
+
|
28
|
+
if self.class.definitions.has_key?(factory_name)
|
29
|
+
self.attributes =
|
30
|
+
self.class.definitions[factory_name].attributes.merge(attributes)
|
31
|
+
else
|
32
|
+
self.attributes = attributes
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def method_missing(possible_name, *args, &block)
|
37
|
+
if args.size == 1 && !block_given?
|
38
|
+
self.attributes.merge!({possible_name => args[0]})
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_h
|
43
|
+
self.attributes
|
44
|
+
end
|
45
|
+
|
46
|
+
def to_xml
|
47
|
+
element_name = self.factory_name.dasherize
|
48
|
+
"<#{element_name}>" +
|
49
|
+
self.attributes.keys.collect{|a| get_attribute_xml(a)}.join("\r\n") +
|
50
|
+
"</#{element_name}>"
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
def get_attribute_xml(attribute)
|
55
|
+
if self.attributes[attribute]
|
56
|
+
element_name = attribute.to_s.dasherize
|
57
|
+
"<#{element_name}>" + self.attributes[attribute] + "</#{element_name}>"
|
58
|
+
else
|
59
|
+
""
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
|
+
|
3
|
+
class ResourceCollectionTest < Test::Unit::TestCase
|
4
|
+
context "collections" do
|
5
|
+
setup do
|
6
|
+
@books = [
|
7
|
+
ResourceFactory.build(:nonfiction_book),
|
8
|
+
ResourceFactory.build(:nonfiction_book)
|
9
|
+
]
|
10
|
+
|
11
|
+
@collection = ResourceCollection.build(@books)
|
12
|
+
end
|
13
|
+
|
14
|
+
should "have a header that reflects the pluralized resource type" do
|
15
|
+
assert_match(/<nonfiction-books>/, @collection.to_xml)
|
16
|
+
end
|
17
|
+
|
18
|
+
should "contain each items' xml" do
|
19
|
+
@books.each do |b|
|
20
|
+
assert_match(/#{b.to_xml}/, @collection.to_xml)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "..", "test_helper")
|
2
|
+
|
3
|
+
class ResourceFactoryTest < Test::Unit::TestCase
|
4
|
+
class PetResource < ActiveResource::Base
|
5
|
+
|
6
|
+
end
|
7
|
+
|
8
|
+
ResourceFactory.define(:nonfiction_book) do |p|
|
9
|
+
p.name "The Pragmatic Programmer"
|
10
|
+
p.author "David Thomas"
|
11
|
+
end
|
12
|
+
|
13
|
+
context "building with resource factory" do
|
14
|
+
should "have default attributes" do
|
15
|
+
book = ResourceFactory.build(:nonfiction_book)
|
16
|
+
assert_not_nil book.attributes[:name]
|
17
|
+
end
|
18
|
+
|
19
|
+
should "have a header that reflects the resource type" do
|
20
|
+
book = ResourceFactory.build(:nonfiction_book)
|
21
|
+
assert_match /<nonfiction-book>/, book.to_xml
|
22
|
+
end
|
23
|
+
|
24
|
+
should "contain the attributes inside the root" do
|
25
|
+
book = ResourceFactory.build(:nonfiction_book)
|
26
|
+
assert_match /<name>#{book.attributes[:name]}<\/name>/, book.to_xml
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dpickett-resource_factory
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dan Pickett
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-01-21 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: ""
|
17
|
+
email: josh@technicalpickles.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- README.textile
|
26
|
+
- VERSION.yml
|
27
|
+
- lib/resource_factory
|
28
|
+
- lib/resource_factory/resource_collection.rb
|
29
|
+
- lib/resource_factory/resource_factory.rb
|
30
|
+
- lib/resource_factory.rb
|
31
|
+
- test/lib
|
32
|
+
- test/lib/resource_collection_test.rb
|
33
|
+
- test/lib/resource_factory_test.rb
|
34
|
+
- test/test_helper.rb
|
35
|
+
has_rdoc: false
|
36
|
+
homepage: http://github.com/dpickett/resource_factory
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
requirements: []
|
55
|
+
|
56
|
+
rubyforge_project:
|
57
|
+
rubygems_version: 1.2.0
|
58
|
+
signing_key:
|
59
|
+
specification_version: 2
|
60
|
+
summary: Easily create ActiveResource XML responses
|
61
|
+
test_files: []
|
62
|
+
|