static_resource 1.0.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/MIT-LICENSE +20 -0
- data/README +41 -0
- data/Rakefile +30 -0
- data/VERSION +1 -0
- data/init.rb +2 -0
- data/lib/static_resource.rb +68 -0
- data/static_resource.gemspec +64 -0
- data/test/all_tests.rb +1 -0
- data/test/country.xml +20 -0
- data/test/os.xml +11 -0
- data/test/states.xml +18 -0
- data/test/static_resource_test.rb +74 -0
- data/test/test_helper.rb +7 -0
- metadata +91 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Nick Zalabak
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
= static_resource
|
2
|
+
|
3
|
+
== Description
|
4
|
+
|
5
|
+
A plugin that leverages the slick XML parsing into objects of ActiveResource without
|
6
|
+
making any network calls by reading XML documents on your local filesystem.
|
7
|
+
The XML documents must conform to the same ActiveResource "schema" rules in order to be
|
8
|
+
read and digested into objects.
|
9
|
+
|
10
|
+
This is great for those who don't want to store data structures that aren't necessarily
|
11
|
+
large enough to be worth storing in the database, but not small enough to "hard code"
|
12
|
+
elsewhere.
|
13
|
+
|
14
|
+
== Installation
|
15
|
+
|
16
|
+
ruby script/plugin install git://github.com/techwhizbang/static_resource.git
|
17
|
+
|
18
|
+
== Usage
|
19
|
+
|
20
|
+
#Define an XML file somewhere within your stack, and reference it in a subclass of StaticResource
|
21
|
+
#like this:
|
22
|
+
|
23
|
+
class Country < StaticResource::Base
|
24
|
+
self.static_resource = File.expand_path(File.dirname(__FILE__) + "/country.xml")
|
25
|
+
end
|
26
|
+
|
27
|
+
#Use the find method to read the XML file
|
28
|
+
country = Country.find(:one)
|
29
|
+
|
30
|
+
#Use the object just like you would with attributes on an ActiveResource
|
31
|
+
states = country.states
|
32
|
+
state = states.first
|
33
|
+
|
34
|
+
#Or don't subclass at all just use StaticResource::Base in the raw
|
35
|
+
|
36
|
+
static_resource = StaticResource::Base.find(:one, :from => Rails.root + "/lib/whatever.xml")
|
37
|
+
|
38
|
+
=== Other
|
39
|
+
|
40
|
+
Problems, comments, and suggestions all welcome. nick@controlaltcreate.com or visit my blog
|
41
|
+
http://techwhizbang.com
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |s|
|
7
|
+
s.name = "static_resource"
|
8
|
+
s.summary = "A gem that leverages the XML parsing of ActiveResource into objects without making any network calls"
|
9
|
+
s.description = "Leverages the XML parsing of ActiveResource into objects without making any network calls by reading XML documents on your local filesystem"
|
10
|
+
s.email = "techwhizbang@gmail.com"
|
11
|
+
s.homepage = "http://github.com/techwhizbang/static_resource"
|
12
|
+
s.rubyforge_project = 'static_resource'
|
13
|
+
s.authors = ["Nick Zalabak"]
|
14
|
+
s.add_development_dependency "mocha"
|
15
|
+
s.files = FileList["[A-Za-z]*", "{lib,test}/**/*"]
|
16
|
+
s.test_files = FileList["test/**/*"]
|
17
|
+
s.add_dependency "activeresource"
|
18
|
+
end
|
19
|
+
|
20
|
+
rescue LoadError
|
21
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
22
|
+
end
|
23
|
+
|
24
|
+
Rake::TestTask.new do |t|
|
25
|
+
t.libs << 'lib'
|
26
|
+
t.pattern = 'test/**/*_test.rb'
|
27
|
+
t.verbose = false
|
28
|
+
end
|
29
|
+
|
30
|
+
task :default => :test
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/init.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
module StaticResource
|
2
|
+
class Base < ::ActiveResource::Base
|
3
|
+
|
4
|
+
self.site = ""
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def static_resource
|
9
|
+
if defined?(@static_resource)
|
10
|
+
@static_resource
|
11
|
+
elsif superclass != Object && superclass.static_resource
|
12
|
+
superclass.static_resource.dup.freeze
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def static_resource=(static_resource)
|
17
|
+
if static_resource.nil?
|
18
|
+
@static_resource = nil
|
19
|
+
else
|
20
|
+
@static_resource = static_resource
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def find(*arguments)
|
25
|
+
scope = arguments.slice!(0)
|
26
|
+
options = arguments.slice!(0) || {}
|
27
|
+
options = { :from => @static_resource }.merge(options)
|
28
|
+
if scope == :all
|
29
|
+
resource = find_every(options)
|
30
|
+
if options[:id] && options[:key]
|
31
|
+
resource = resource.detect { |r| r.send(options[:key].to_sym) == options[:id] }
|
32
|
+
end
|
33
|
+
elsif scope == :first
|
34
|
+
resource = find_every(options).first
|
35
|
+
elsif scope == :last
|
36
|
+
resource= find_every(options).last
|
37
|
+
elsif scope == :one
|
38
|
+
resource = find_one(options)
|
39
|
+
else
|
40
|
+
resource = find_single(scope, options)
|
41
|
+
end
|
42
|
+
|
43
|
+
resource = resource.attributes[options[:id]] if scope != :all && options[:id]
|
44
|
+
resource
|
45
|
+
end
|
46
|
+
|
47
|
+
def connection(refresh = false)
|
48
|
+
Connection.new(ActiveResource::Formats[:xml])
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class Connection < ::ActiveResource::Connection
|
54
|
+
def initialize(format = ActiveResource::Formats[:xml])
|
55
|
+
self.format = format
|
56
|
+
end
|
57
|
+
|
58
|
+
def get(path, headers = {})
|
59
|
+
format.decode(request(:get, path, nil))
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def request(method, path, *arguments)
|
65
|
+
File.read( path )
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{static_resource}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Nick Zalabak"]
|
12
|
+
s.date = %q{2010-03-06}
|
13
|
+
s.description = %q{Leverages the XML parsing of ActiveResource into objects without making any network calls by reading XML documents on your local filesystem}
|
14
|
+
s.email = %q{techwhizbang@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
"MIT-LICENSE",
|
20
|
+
"README",
|
21
|
+
"Rakefile",
|
22
|
+
"VERSION",
|
23
|
+
"init.rb",
|
24
|
+
"lib/static_resource.rb",
|
25
|
+
"static_resource.gemspec",
|
26
|
+
"test/all_tests.rb",
|
27
|
+
"test/country.xml",
|
28
|
+
"test/os.xml",
|
29
|
+
"test/states.xml",
|
30
|
+
"test/static_resource_test.rb",
|
31
|
+
"test/test_helper.rb"
|
32
|
+
]
|
33
|
+
s.homepage = %q{http://github.com/techwhizbang/static_resource}
|
34
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
35
|
+
s.require_paths = ["lib"]
|
36
|
+
s.rubyforge_project = %q{static_resource}
|
37
|
+
s.rubygems_version = %q{1.3.5}
|
38
|
+
s.summary = %q{A gem that leverages the XML parsing of ActiveResource into objects without making any network calls}
|
39
|
+
s.test_files = [
|
40
|
+
"test/all_tests.rb",
|
41
|
+
"test/country.xml",
|
42
|
+
"test/os.xml",
|
43
|
+
"test/states.xml",
|
44
|
+
"test/static_resource_test.rb",
|
45
|
+
"test/test_helper.rb"
|
46
|
+
]
|
47
|
+
|
48
|
+
if s.respond_to? :specification_version then
|
49
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
50
|
+
s.specification_version = 3
|
51
|
+
|
52
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
53
|
+
s.add_development_dependency(%q<mocha>, [">= 0"])
|
54
|
+
s.add_runtime_dependency(%q<activeresource>, [">= 0"])
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
57
|
+
s.add_dependency(%q<activeresource>, [">= 0"])
|
58
|
+
end
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
61
|
+
s.add_dependency(%q<activeresource>, [">= 0"])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
data/test/all_tests.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Dir['**/*_test.rb'].each { |test_case| require test_case }
|
data/test/country.xml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
<country>
|
2
|
+
<states type="array">
|
3
|
+
<state>
|
4
|
+
<id type="integer">1</id>
|
5
|
+
<name type="string">Illinois</name>
|
6
|
+
<founded type="datetime">1879-11-06 06:15:00</founded>
|
7
|
+
<latitude type="float">50.2345</latitude>
|
8
|
+
<longitude type="float">123.234</longitude>
|
9
|
+
<population type="integer">100000000</population>
|
10
|
+
</state>
|
11
|
+
<state>
|
12
|
+
<id type="integer">2</id>
|
13
|
+
<name type="string">California</name>
|
14
|
+
<founded type="datetime">1862-01-02 06:15:00</founded>
|
15
|
+
<latitude type="float">70.2345</latitude>
|
16
|
+
<longitude type="float">14.234</longitude>
|
17
|
+
<population type="integer">400000000</population>
|
18
|
+
</state>
|
19
|
+
</states>
|
20
|
+
</country>
|
data/test/os.xml
ADDED
data/test/states.xml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
<states type="array">
|
2
|
+
<state>
|
3
|
+
<id type="integer">1</id>
|
4
|
+
<name type="string">Illinois</name>
|
5
|
+
<founded type="datetime">1879-11-06 06:15:00</founded>
|
6
|
+
<latitude type="float">50.2345</latitude>
|
7
|
+
<longitude type="float">123.234</longitude>
|
8
|
+
<population type="integer">100000000</population>
|
9
|
+
</state>
|
10
|
+
<state>
|
11
|
+
<id type="integer">2</id>
|
12
|
+
<name type="string">California</name>
|
13
|
+
<founded type="datetime">1862-01-02 06:15:00</founded>
|
14
|
+
<latitude type="float">70.2345</latitude>
|
15
|
+
<longitude type="float">14.234</longitude>
|
16
|
+
<population type="integer">400000000</population>
|
17
|
+
</state>
|
18
|
+
</states>
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
|
2
|
+
|
3
|
+
class Country < StaticResource::Base
|
4
|
+
self.static_resource = File.expand_path(File.dirname(__FILE__) + "/country.xml")
|
5
|
+
end
|
6
|
+
|
7
|
+
class StaticResourceTest < Test::Unit::TestCase
|
8
|
+
|
9
|
+
def test_static_resource_assignment
|
10
|
+
assert_equal(File.expand_path(File.dirname(__FILE__) + "/country.xml"), Country.static_resource)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_connection_type
|
14
|
+
assert_instance_of(StaticResource::Connection, Country.connection)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_find_without_xml_file
|
18
|
+
assert_not_nil(Country.find(:one))
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_find_with_xml_file
|
22
|
+
assert_not_nil(StaticResource::Base.find(:one, :from => File.expand_path(File.dirname(__FILE__) + "/os.xml")))
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_find_expects_find_one
|
26
|
+
file = File.expand_path(File.dirname(__FILE__) + "/os.xml")
|
27
|
+
StaticResource::Base.expects(:find_one).with({:from => file})
|
28
|
+
StaticResource::Base.find(:one, :from => file)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_find_expects_find_all
|
32
|
+
file = File.expand_path(File.dirname(__FILE__) + "/states.xml")
|
33
|
+
StaticResource::Base.expects(:find_every).with({:from => file})
|
34
|
+
StaticResource::Base.find(:all, :from => file)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_find_expects_find_last
|
38
|
+
file = File.expand_path(File.dirname(__FILE__) + "/states.xml")
|
39
|
+
resource = StaticResource::Base.find(:last, :from => file)
|
40
|
+
assert_equal(resource.attributes["name"], "California")
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_find_expects_find_first
|
44
|
+
file = File.expand_path(File.dirname(__FILE__) + "/states.xml")
|
45
|
+
resource = StaticResource::Base.find(:first, :from => file)
|
46
|
+
assert_equal(resource.attributes["name"], "Illinois")
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_connection_get_method
|
50
|
+
file = File.expand_path(File.dirname(__FILE__) + "/os.xml")
|
51
|
+
connection = StaticResource::Connection.new(ActiveResource::Formats[:xml])
|
52
|
+
connection.format.expects(:decode)
|
53
|
+
connection.get(file, nil)
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_connection_request_expects_file_read
|
57
|
+
file = File.expand_path(File.dirname(__FILE__) + "/os.xml")
|
58
|
+
connection = StaticResource::Connection.new(ActiveResource::Formats[:xml])
|
59
|
+
File.expects(:read).with(file)
|
60
|
+
connection.send(:request, nil, file, nil)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_find_with_id_lookup
|
64
|
+
file = File.expand_path(File.dirname(__FILE__) + "/os.xml")
|
65
|
+
resource = StaticResource::Base.find(:one, :id => "linux", :from => file)
|
66
|
+
assert_equal(resource.description, "love it")
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_find_all_with_id_and_key_lookup
|
70
|
+
file = File.expand_path(File.dirname(__FILE__) + "/states.xml")
|
71
|
+
resource = StaticResource::Base.find(:all, :id => "Illinois", :key => "name", :from => file)
|
72
|
+
assert_equal(resource.name, "Illinois")
|
73
|
+
end
|
74
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: static_resource
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nick Zalabak
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-03-06 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mocha
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activeresource
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: Leverages the XML parsing of ActiveResource into objects without making any network calls by reading XML documents on your local filesystem
|
36
|
+
email: techwhizbang@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README
|
43
|
+
files:
|
44
|
+
- MIT-LICENSE
|
45
|
+
- README
|
46
|
+
- Rakefile
|
47
|
+
- VERSION
|
48
|
+
- init.rb
|
49
|
+
- lib/static_resource.rb
|
50
|
+
- static_resource.gemspec
|
51
|
+
- test/all_tests.rb
|
52
|
+
- test/country.xml
|
53
|
+
- test/os.xml
|
54
|
+
- test/states.xml
|
55
|
+
- test/static_resource_test.rb
|
56
|
+
- test/test_helper.rb
|
57
|
+
has_rdoc: true
|
58
|
+
homepage: http://github.com/techwhizbang/static_resource
|
59
|
+
licenses: []
|
60
|
+
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options:
|
63
|
+
- --charset=UTF-8
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: "0"
|
77
|
+
version:
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project: static_resource
|
81
|
+
rubygems_version: 1.3.5
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: A gem that leverages the XML parsing of ActiveResource into objects without making any network calls
|
85
|
+
test_files:
|
86
|
+
- test/all_tests.rb
|
87
|
+
- test/country.xml
|
88
|
+
- test/os.xml
|
89
|
+
- test/states.xml
|
90
|
+
- test/static_resource_test.rb
|
91
|
+
- test/test_helper.rb
|