ephemeral 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +10 -0
- data/Gemfile.lock +37 -0
- data/LICENSE.txt +20 -0
- data/README.md +63 -0
- data/Rakefile +38 -0
- data/VERSION +1 -0
- data/lib/ephemeral.rb +4 -0
- data/lib/ephemeral/base.rb +38 -0
- data/lib/ephemeral/collection.rb +49 -0
- data/spec/ephemeral_spec.rb +104 -0
- metadata +141 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activesupport (3.2.7)
|
5
|
+
i18n (~> 0.6)
|
6
|
+
multi_json (~> 1.0)
|
7
|
+
diff-lcs (1.1.3)
|
8
|
+
git (1.2.5)
|
9
|
+
i18n (0.6.0)
|
10
|
+
jeweler (1.8.4)
|
11
|
+
bundler (~> 1.0)
|
12
|
+
git (>= 1.2.5)
|
13
|
+
rake
|
14
|
+
rdoc
|
15
|
+
json (1.7.4)
|
16
|
+
multi_json (1.3.6)
|
17
|
+
rake (0.9.2.2)
|
18
|
+
rdoc (3.12)
|
19
|
+
json (~> 1.4)
|
20
|
+
rspec (2.11.0)
|
21
|
+
rspec-core (~> 2.11.0)
|
22
|
+
rspec-expectations (~> 2.11.0)
|
23
|
+
rspec-mocks (~> 2.11.0)
|
24
|
+
rspec-core (2.11.1)
|
25
|
+
rspec-expectations (2.11.2)
|
26
|
+
diff-lcs (~> 1.1.3)
|
27
|
+
rspec-mocks (2.11.2)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
ruby
|
31
|
+
|
32
|
+
DEPENDENCIES
|
33
|
+
activesupport
|
34
|
+
bundler
|
35
|
+
jeweler (~> 1.8.4)
|
36
|
+
rdoc (~> 3.12)
|
37
|
+
rspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Corey Ehmke
|
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.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
ephemeral
|
2
|
+
=========
|
3
|
+
|
4
|
+
Ephemeral was created at Trunk Club to bring ORM-like functionality to non-persisted objects. The anticipated use case is for an application that consumes an API and materializes one or more collections of objects from a JSON response or XML response.
|
5
|
+
|
6
|
+
Please note that Ephemeral is currently in beta and is probably not ready for production use.
|
7
|
+
|
8
|
+
Example the First
|
9
|
+
=================
|
10
|
+
|
11
|
+
Let's say that we have an API server that stores information about the inventions of Nikola Tesla. We have another application that consumes this information. Our initial call to the API retrieves a list of all of his inventions, but we want to display them in categories. Assuming that we materialize each invention into an Invention object, the Ephemeral wiring is simple. Here's our sample Invention class:
|
12
|
+
|
13
|
+
class Invention
|
14
|
+
|
15
|
+
include Ephemeral::Base
|
16
|
+
|
17
|
+
attr_accessor :name, :description, :category, :stolen_by_edison
|
18
|
+
|
19
|
+
scope :telegraphy, {:category => 'Telegraphy'}
|
20
|
+
scope :electrical, {:category => 'Electrical'}
|
21
|
+
scope :mechanical, {:category => 'Mechanical'}
|
22
|
+
scope :stolen {:stolen_by_edison => true }
|
23
|
+
|
24
|
+
def initialize(args={})
|
25
|
+
# Marshal object based on your API
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
We can now easily find all of Tesla's telegraphy inventions that were shamelessly stolen by Thomas Edison:
|
31
|
+
|
32
|
+
Invention.telegraphy.stolen
|
33
|
+
|
34
|
+
Example the Second
|
35
|
+
==================
|
36
|
+
|
37
|
+
Our Tesla site has really taken off, and now we want to introduce another object, each instance of which represents a period in Tesla's life. We want to associate inventions with each of these periods. Again, rather than building complex logic into our API request, we can simply fetch a mass of data and build our relationships on the consumer application side using Ephemeral:
|
38
|
+
|
39
|
+
class Period
|
40
|
+
|
41
|
+
include Ephemeral::Base
|
42
|
+
|
43
|
+
attr_accessor :name, :start_year, :end_year
|
44
|
+
|
45
|
+
collects :inventions
|
46
|
+
|
47
|
+
def initialize(args={})
|
48
|
+
# Marshal object based on your API, including setting up Invention objects
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
Now we can filter inventions on the consumer side:
|
54
|
+
|
55
|
+
period = Period.where(:name => 'Early Years')
|
56
|
+
inventions = period.inventions.mechanical
|
57
|
+
favorite = inventions.where(:name => 'Doomsday Device')
|
58
|
+
|
59
|
+
More Examples?
|
60
|
+
==============
|
61
|
+
|
62
|
+
For a more complete example, please refer to the specs.
|
63
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "ephemeral"
|
18
|
+
gem.homepage = "http://github.com/Bantik/ephemeral"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Ephemeral is an ODM for in-memory collections of objects.}
|
21
|
+
gem.description = %Q{Ephemeral lets you define one-to-many relationships between in-memory objects, with ORM-like support for where clauses and chainable scopes.}
|
22
|
+
gem.email = "corey@idolhands.com"
|
23
|
+
gem.authors = ["Corey Ehmke"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
task :default => :test
|
29
|
+
|
30
|
+
require 'rdoc/task'
|
31
|
+
Rake::RDocTask.new do |rdoc|
|
32
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
33
|
+
|
34
|
+
rdoc.rdoc_dir = 'rdoc'
|
35
|
+
rdoc.title = "ephemeral #{version}"
|
36
|
+
rdoc.rdoc_files.include('README*')
|
37
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
38
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/ephemeral.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
module Ephemeral
|
2
|
+
|
3
|
+
module Base
|
4
|
+
|
5
|
+
require 'active_support/core_ext/object/blank'
|
6
|
+
require 'active_support/core_ext/string/inflections'
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
base.extend ClassMethods
|
10
|
+
base.send(:attr_accessor, :collection)
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
|
15
|
+
def collects(name, args={})
|
16
|
+
class_name = args[:class_name] || name.to_s.classify
|
17
|
+
self.send :define_method, name do
|
18
|
+
self.collection ||= Ephemeral::Collection.new(class_name)
|
19
|
+
end
|
20
|
+
self.send :define_method, "#{name}=" do |objects|
|
21
|
+
self.collection = Ephemeral::Collection.new(class_name, objects)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def scope(name, conditions)
|
26
|
+
self.scopes ||= {}
|
27
|
+
self.scopes[name] = conditions
|
28
|
+
end
|
29
|
+
|
30
|
+
def scopes
|
31
|
+
@@scopes ||= {}
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Ephemeral
|
2
|
+
|
3
|
+
class Collection
|
4
|
+
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
attr_accessor :objects, :klass
|
8
|
+
|
9
|
+
def initialize(klass, objects=[])
|
10
|
+
self.klass = eval(klass)
|
11
|
+
self.klass.scopes.each{ |k, v| define_singleton_method k, lambda { self.execute_scope(k)} }
|
12
|
+
self.objects = self.materialize(objects)
|
13
|
+
self
|
14
|
+
end
|
15
|
+
|
16
|
+
def each(&block)
|
17
|
+
self.objects && self.objects.each(&block)
|
18
|
+
end
|
19
|
+
|
20
|
+
def where(args={})
|
21
|
+
return [] unless self.objects
|
22
|
+
results = args.inject([]){|a,kv| a << self.objects.select{|o| o.send(kv[0]) == kv[1]}}
|
23
|
+
results = results.flatten.select{|r| results.flatten.count(r) == results.count}.uniq
|
24
|
+
end
|
25
|
+
|
26
|
+
def last
|
27
|
+
self.objects.last
|
28
|
+
end
|
29
|
+
|
30
|
+
def materialize(objects_array=[])
|
31
|
+
return [] unless objects_array
|
32
|
+
return objects_array if objects_array.first.class == self.klass
|
33
|
+
objects_array.map{|t| self.klass.new(t) }
|
34
|
+
end
|
35
|
+
|
36
|
+
def execute_scope(method)
|
37
|
+
return Ephemeral::Collection.new(self.klass.name) unless self.objects
|
38
|
+
results = self.klass.scopes[method].inject([]){|a,kv| a << self.objects.select{|o| o.send(kv[0]) == kv[1]}}
|
39
|
+
results = results.flatten.select{|r| results.flatten.count(r) == results.count}.uniq
|
40
|
+
Ephemeral::Collection.new(self.klass.name, results)
|
41
|
+
end
|
42
|
+
|
43
|
+
def << (objekts)
|
44
|
+
self.objects += objekts
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'ephemeral'
|
2
|
+
|
3
|
+
class Collectible
|
4
|
+
include Ephemeral::Base
|
5
|
+
attr_accessor :name, :item_count
|
6
|
+
scope :foos, {:name => 'foo'}
|
7
|
+
scope :paychecks, {:name => 'Paychecks'}
|
8
|
+
scope :threes, {:item_count => 3}
|
9
|
+
def initialize(args={}); args.each{|k,v| self.send("#{k}=", v)}; end
|
10
|
+
end
|
11
|
+
|
12
|
+
class NotCollectible
|
13
|
+
include Ephemeral::Base
|
14
|
+
attr_accessor :name
|
15
|
+
def initialize(args={}); args.each{|k,v| self.send("#{k}=", v)}; end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Collector
|
19
|
+
include Ephemeral::Base
|
20
|
+
attr_accessor :name
|
21
|
+
collects :collectibles
|
22
|
+
collects :junk, :class_name => 'NotCollectible'
|
23
|
+
def initialize(args={}); args.each{|k,v| self.send("#{k}=", v)}; end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe Ephemeral do
|
27
|
+
|
28
|
+
context 'class methods' do
|
29
|
+
|
30
|
+
it 'defines a collection' do
|
31
|
+
Collector.new.collectibles.klass.should == Collectible
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'accepts a class name' do
|
35
|
+
Collector.new.junk.klass.should == NotCollectible
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'creates a setter' do
|
39
|
+
Collector.new.respond_to?(:collectibles=).should be_true
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'creates a getter' do
|
43
|
+
Collector.new.respond_to?(:collectibles).should be_true
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'registers a scope' do
|
47
|
+
Collectible.scopes.include?(:foos).should be_true
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'instance methods' do
|
53
|
+
|
54
|
+
before :each do
|
55
|
+
|
56
|
+
@collectibles = [
|
57
|
+
Collectible.new(:name => 'Paychecks', :item_count => 1),
|
58
|
+
Collectible.new(:name => 'Paychecks', :item_count => 3),
|
59
|
+
Collectible.new(:name => 'Requisitions', :item_count => 2),
|
60
|
+
]
|
61
|
+
|
62
|
+
@collector = Collector.new(:name => 'Hermes')
|
63
|
+
@collector.collectibles = @collectibles
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
describe 'initializes with materialized objects' do
|
68
|
+
|
69
|
+
it 'from a setter' do
|
70
|
+
collector = Collector.new(
|
71
|
+
:name => 'from_api',
|
72
|
+
'collectibles' => [
|
73
|
+
{'name' => 'foo'},
|
74
|
+
{'name' => 'bar'}
|
75
|
+
]
|
76
|
+
)
|
77
|
+
collector.collectibles.count.should == 2
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'performs a where' do
|
83
|
+
@collector.collectibles.where(:name => 'Paychecks').should_not be_blank
|
84
|
+
end
|
85
|
+
|
86
|
+
describe 'scope method' do
|
87
|
+
|
88
|
+
it 'executes a scope' do
|
89
|
+
@collector.collectibles.paychecks.first.should_not be_nil
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'returns a collection' do
|
93
|
+
@collector.collectibles.paychecks.class.name.should == 'Ephemeral::Collection'
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'chains scopes' do
|
97
|
+
@collector.collectibles.paychecks.threes.count.should == 1
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ephemeral
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Corey Ehmke
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-12 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rdoc
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '3.12'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.12'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: bundler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: jeweler
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ~>
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 1.8.4
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 1.8.4
|
94
|
+
description: Ephemeral lets you define one-to-many relationships between in-memory
|
95
|
+
objects, with ORM-like support for where clauses and chainable scopes.
|
96
|
+
email: corey@idolhands.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files:
|
100
|
+
- LICENSE.txt
|
101
|
+
- README.md
|
102
|
+
files:
|
103
|
+
- Gemfile
|
104
|
+
- Gemfile.lock
|
105
|
+
- LICENSE.txt
|
106
|
+
- README.md
|
107
|
+
- Rakefile
|
108
|
+
- VERSION
|
109
|
+
- lib/ephemeral.rb
|
110
|
+
- lib/ephemeral/base.rb
|
111
|
+
- lib/ephemeral/collection.rb
|
112
|
+
- spec/ephemeral_spec.rb
|
113
|
+
homepage: http://github.com/Bantik/ephemeral
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
segments:
|
127
|
+
- 0
|
128
|
+
hash: -1888990476814131903
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
none: false
|
131
|
+
requirements:
|
132
|
+
- - ! '>='
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 1.8.24
|
138
|
+
signing_key:
|
139
|
+
specification_version: 3
|
140
|
+
summary: Ephemeral is an ODM for in-memory collections of objects.
|
141
|
+
test_files: []
|