paul-datapathy 0.1.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/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +7 -0
- data/Rakefile +52 -0
- data/VERSION +1 -0
- data/lib/datapathy.rb +21 -0
- data/lib/datapathy/adapters/abstract_adapter.rb +29 -0
- data/lib/datapathy/adapters/memory_adapter.rb +34 -0
- data/lib/datapathy/model.rb +99 -0
- data/lib/datapathy/query.rb +83 -0
- data/spec/datapathy_spec.rb +50 -0
- data/spec/spec_helper.rb +12 -0
- metadata +86 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Paul Sadauskas
|
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.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "datapathy"
|
8
|
+
gem.summary = %Q{TODO}
|
9
|
+
gem.email = "psadauskas@gmail.com"
|
10
|
+
gem.homepage = "http://github.com/paul/datapathy"
|
11
|
+
gem.authors = ["Paul Sadauskas"]
|
12
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
13
|
+
|
14
|
+
gem.add_dependency "activesupport", "~> 3.0"
|
15
|
+
gem.add_dependency "uuidtools", "~> 2.0"
|
16
|
+
end
|
17
|
+
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
spec.spec_opts << ['--options', 'spec/spec.opts']
|
27
|
+
end
|
28
|
+
|
29
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
30
|
+
spec.libs << 'lib' << 'spec'
|
31
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
32
|
+
spec.rcov = true
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
if File.exist?('VERSION.yml')
|
41
|
+
config = YAML.load(File.read('VERSION.yml'))
|
42
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
43
|
+
else
|
44
|
+
version = ""
|
45
|
+
end
|
46
|
+
|
47
|
+
rdoc.rdoc_dir = 'rdoc'
|
48
|
+
rdoc.title = "datapathy #{version}"
|
49
|
+
rdoc.rdoc_files.include('README*')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
52
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/datapathy.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
require 'uuidtools'
|
3
|
+
|
4
|
+
# only require the parts of activesupport we want
|
5
|
+
require 'active_support/core_ext/object/returning'
|
6
|
+
require 'active_support/inflector'
|
7
|
+
require 'active_support/core_ext/string/inflections'
|
8
|
+
|
9
|
+
module Datapathy
|
10
|
+
|
11
|
+
def self.default_adapter
|
12
|
+
@adapter ||= Datapathy::Adapters::MemoryAdapter.new
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
require File.join(File.dirname(__FILE__), 'datapathy/model')
|
18
|
+
require File.join(File.dirname(__FILE__), 'datapathy/query')
|
19
|
+
require File.join(File.dirname(__FILE__), 'datapathy/adapters/abstract_adapter')
|
20
|
+
require File.join(File.dirname(__FILE__), 'datapathy/adapters/memory_adapter')
|
21
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Datapathy::Adapters
|
2
|
+
|
3
|
+
class AbstractAdapter
|
4
|
+
|
5
|
+
def initialize(options = {})
|
6
|
+
@options = options
|
7
|
+
|
8
|
+
end
|
9
|
+
|
10
|
+
def create(resources)
|
11
|
+
raise NotImplementedError, "#{self.class}#create not implemented"
|
12
|
+
end
|
13
|
+
|
14
|
+
def read(key_or_query)
|
15
|
+
raise NotImplementedError, "#{self.class}#read not implemented"
|
16
|
+
end
|
17
|
+
|
18
|
+
def update(attributes, collection)
|
19
|
+
raise NotImplementedError, "#{self.class}#update not implemented"
|
20
|
+
end
|
21
|
+
|
22
|
+
def delete(collection)
|
23
|
+
raise NotImplementedError, "#{self.class}#delete not implemented"
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Datapathy::Adapters
|
2
|
+
|
3
|
+
class MemoryAdapter < AbstractAdapter
|
4
|
+
|
5
|
+
def initialize(options = {})
|
6
|
+
super
|
7
|
+
|
8
|
+
@store = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def create(resources)
|
12
|
+
resources.each do |resource|
|
13
|
+
records_for(resource)[resource.key] = resource.persisted_attributes
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def read(query)
|
18
|
+
if query.key_lookup?
|
19
|
+
records_for(query.model)[query.key]
|
20
|
+
else
|
21
|
+
query.filter_records(records_for(query.model).values)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
26
|
+
|
27
|
+
def records_for(resource)
|
28
|
+
@store[resource.model_name] ||= {}
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
@@ -0,0 +1,99 @@
|
|
1
|
+
|
2
|
+
module Datapathy::Model
|
3
|
+
|
4
|
+
def self.included(klass)
|
5
|
+
klass.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize(attributes = {})
|
9
|
+
attributes.each do |name, value|
|
10
|
+
self.send(:"#{name}=", value)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def persisted_attributes
|
15
|
+
returning({}) do |attrs|
|
16
|
+
self.class.persisted_attributes.each do |name|
|
17
|
+
attrs[name] = self.send(:"#{name}")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def key
|
23
|
+
self.id
|
24
|
+
end
|
25
|
+
|
26
|
+
def model_name
|
27
|
+
self.class.model_name
|
28
|
+
end
|
29
|
+
|
30
|
+
def ==(other)
|
31
|
+
self.key == other.key
|
32
|
+
end
|
33
|
+
|
34
|
+
module ClassMethods
|
35
|
+
|
36
|
+
def persists(*args)
|
37
|
+
args.each do |atr|
|
38
|
+
persisted_attributes << atr
|
39
|
+
|
40
|
+
define_method(atr) do
|
41
|
+
instance_variable_get("@#{atr}")
|
42
|
+
end
|
43
|
+
|
44
|
+
define_method("#{atr}=") do |val|
|
45
|
+
instance_variable_set("@#{atr}",val)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def persisted_attributes
|
51
|
+
@persisted_attributes ||= []
|
52
|
+
end
|
53
|
+
|
54
|
+
def create(*attributes)
|
55
|
+
resources = attributes.map do |attrs|
|
56
|
+
me = new(attrs)
|
57
|
+
adapter.create([me])
|
58
|
+
me
|
59
|
+
end
|
60
|
+
attributes.size == 1 ? resources.first : resources
|
61
|
+
end
|
62
|
+
|
63
|
+
def adapter
|
64
|
+
@adapter || Datapathy.default_adapter
|
65
|
+
end
|
66
|
+
|
67
|
+
def key
|
68
|
+
:id
|
69
|
+
end
|
70
|
+
|
71
|
+
def [](key)
|
72
|
+
query = Datapathy::Query.new(self)
|
73
|
+
query.add_condition(self.key, :eql, key)
|
74
|
+
new(adapter.read(query))
|
75
|
+
end
|
76
|
+
|
77
|
+
def all
|
78
|
+
query = Datapathy::Query.new(self)
|
79
|
+
adapter.read(query).map do |r|
|
80
|
+
new(r)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def detect(&blk)
|
85
|
+
query = Datapathy::Query.new(self, &blk)
|
86
|
+
|
87
|
+
adapter.read(query).map do |r|
|
88
|
+
new(r)
|
89
|
+
end.first
|
90
|
+
end
|
91
|
+
|
92
|
+
def model_name
|
93
|
+
self.to_s.underscore
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
@@ -0,0 +1,83 @@
|
|
1
|
+
|
2
|
+
class Datapathy::Query
|
3
|
+
|
4
|
+
attr_reader :model, :conditions
|
5
|
+
|
6
|
+
def initialize(model, &blk)
|
7
|
+
@model = model
|
8
|
+
@conditions = []
|
9
|
+
yield self if block_given?
|
10
|
+
end
|
11
|
+
|
12
|
+
def add_condition(*args)
|
13
|
+
@conditions << Condition.new(*args)
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
def key_lookup?
|
18
|
+
@conditions.size == 1 &&
|
19
|
+
@conditions.first.attribute == model.key &&
|
20
|
+
@conditions.first.operator == :eql
|
21
|
+
end
|
22
|
+
|
23
|
+
def key
|
24
|
+
@conditions.first.operand
|
25
|
+
end
|
26
|
+
|
27
|
+
def filter_records(records)
|
28
|
+
records.select do |record|
|
29
|
+
@conditions.all? do |condition|
|
30
|
+
condition.matches?(record)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def method_missing(method_name, *args)
|
36
|
+
if model.respond_to?(method_name)
|
37
|
+
returning Condition.new(method_name) do |condition|
|
38
|
+
@conditions << condition
|
39
|
+
end
|
40
|
+
|
41
|
+
else
|
42
|
+
raise "Model #{model.model_name} does not have a #{method_name} method to search on"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class Condition
|
47
|
+
attr_reader :attribute, :operator, :operand
|
48
|
+
|
49
|
+
def initialize(attribute = nil, operator = nil, operand = nil)
|
50
|
+
@attribute, @operator, @operand = attribute, operator, operand
|
51
|
+
end
|
52
|
+
|
53
|
+
[ :==, :===, :eql?, :equal?, :=~, :<=>, :<=, :<, :>, :>= ].each do |op|
|
54
|
+
define_method(op) do |val|
|
55
|
+
@operator = op
|
56
|
+
@operand = val
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def contains?(val)
|
61
|
+
@operator = :contains
|
62
|
+
@operand = val
|
63
|
+
self
|
64
|
+
end
|
65
|
+
|
66
|
+
def nil?
|
67
|
+
@operator = :==
|
68
|
+
@operand = nil
|
69
|
+
end
|
70
|
+
|
71
|
+
def matches?(record)
|
72
|
+
value = if record.is_a?(Hash)
|
73
|
+
record[attribute]
|
74
|
+
else
|
75
|
+
record.send(attribute)
|
76
|
+
end
|
77
|
+
|
78
|
+
value.send(operator, operand)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
class MyModel
|
4
|
+
include Datapathy::Model
|
5
|
+
|
6
|
+
persists :id
|
7
|
+
persists :name
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "Datapathy" do
|
11
|
+
|
12
|
+
def uuid
|
13
|
+
UUIDTools::UUID.random_create.to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should have attributes' do
|
17
|
+
MyModel.persisted_attributes.should == [:id, :name]
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should be able to store and retrieve a record' do
|
21
|
+
my_model = MyModel.create(:id => uuid,
|
22
|
+
:name => "Paul")
|
23
|
+
|
24
|
+
other = MyModel[my_model.id]
|
25
|
+
other.should == my_model
|
26
|
+
other.id.should == my_model.id
|
27
|
+
other.name.should == my_model.name
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should be able to retrive all things' do
|
31
|
+
a = MyModel.create(:id => uuid,
|
32
|
+
:name => "George")
|
33
|
+
b = MyModel.create(:id => uuid,
|
34
|
+
:name => "John")
|
35
|
+
|
36
|
+
results = MyModel.all
|
37
|
+
|
38
|
+
results.should include(a)
|
39
|
+
results.should include(b)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should be able to find things' do
|
43
|
+
my_model = MyModel.create(:id => UUIDTools::UUID.random_create.to_s,
|
44
|
+
:name => "Ringo")
|
45
|
+
|
46
|
+
other = MyModel.detect { |r| r.name == "Ringo" }
|
47
|
+
other.should == my_model
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: paul-datapathy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Paul Sadauskas
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-09-11 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activesupport
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "3.0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: uuidtools
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "2.0"
|
34
|
+
version:
|
35
|
+
description:
|
36
|
+
email: psadauskas@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- lib/datapathy.rb
|
52
|
+
- lib/datapathy/adapters/abstract_adapter.rb
|
53
|
+
- lib/datapathy/adapters/memory_adapter.rb
|
54
|
+
- lib/datapathy/model.rb
|
55
|
+
- lib/datapathy/query.rb
|
56
|
+
- spec/datapathy_spec.rb
|
57
|
+
- spec/spec_helper.rb
|
58
|
+
has_rdoc: false
|
59
|
+
homepage: http://github.com/paul/datapathy
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options:
|
62
|
+
- --charset=UTF-8
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: "0"
|
70
|
+
version:
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: "0"
|
76
|
+
version:
|
77
|
+
requirements: []
|
78
|
+
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 1.2.0
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: TODO
|
84
|
+
test_files:
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
- spec/datapathy_spec.rb
|