ampere 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,10 +2,7 @@
2
2
 
3
3
  Ampere is an ActiveRecord-style ORM for the Redis key/value data store.
4
4
 
5
- This is under active development right now and not very far along. Stay
6
- tuned for further developments.
7
-
8
- ## A note about version 1.0 (IMPORTANT!!!)
5
+ ## A note about Ampere version >1.0 (IMPORTANT!!!)
9
6
 
10
7
  For the 1.0 release I changed Ampere's API so that instead of subclassing
11
8
  `Ampere::Model` to use Ampere's methods, you include it as a mixin. This
@@ -60,6 +57,9 @@ be slower if one of the keys you are searching by isn't indexed).
60
57
 
61
58
  post = Post.where(:title => "BREAKING: Kitties Are Awesome").first
62
59
 
60
+ Ampere query results implement the `Enumerable` module, so all the Enumerable methods
61
+ you know and love are there.
62
+
63
63
  ### Indexes
64
64
 
65
65
  Indexes work similar to Mongoid. They are non-unique by default.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
data/ampere.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "ampere"
8
- s.version = "1.0.0"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Max Thom Stahl"]
12
- s.date = "2012-04-20"
12
+ s.date = "2012-05-03"
13
13
  s.description = "An ActiveRecord/Mongoid-esque object model for the Redis key/value data store."
14
14
  s.email = "max@villainousindustri.es"
15
15
  s.extra_rdoc_files = [
@@ -41,6 +41,7 @@ Gem::Specification.new do |s|
41
41
  "spec/models/relationships/has_one_spec.rb",
42
42
  "spec/models/updates_spec.rb",
43
43
  "spec/module/ampere_spec.rb",
44
+ "spec/module/collections_ennumerable_spec.rb",
44
45
  "spec/module/collections_spec.rb",
45
46
  "spec/spec_helper.rb",
46
47
  "test/helper.rb",
@@ -49,7 +50,7 @@ Gem::Specification.new do |s|
49
50
  s.homepage = "http://github.com/mstahl/ampere"
50
51
  s.licenses = ["EPL"]
51
52
  s.require_paths = ["lib"]
52
- s.rubygems_version = "1.8.17"
53
+ s.rubygems_version = "1.8.15"
53
54
  s.summary = "A pure Ruby ORM for Redis."
54
55
 
55
56
  if s.respond_to? :specification_version then
@@ -4,6 +4,8 @@ module Ampere
4
4
  # Collections are search results from queries. They can be used like arrays,
5
5
  # but you cannot add anything to them.
6
6
  class Collection
7
+ include Enumerable
8
+
7
9
  attr_reader :raw_array
8
10
  attr_reader :model
9
11
 
@@ -14,6 +16,17 @@ module Ampere
14
16
  @model = model_class
15
17
  end
16
18
 
19
+ def each
20
+ @raw_array.each_with_index do |x, i|
21
+ if x.is_a?(Ampere::Model) then
22
+ yield(x)
23
+ else
24
+ raw_array[i] = @model.find(x)
25
+ yield(raw_array[i])
26
+ end
27
+ end
28
+ end
29
+
17
30
  # Index into the search results. Lazily loads models when they're accessed.
18
31
  def [](idx)
19
32
  if @raw_array[idx].is_a?(Ampere::Model) then
@@ -24,46 +37,16 @@ module Ampere
24
37
  end
25
38
  end
26
39
 
27
- # Delegates to internal array.
28
- def count
29
- @raw_array.count
30
- end
31
-
32
40
  # Delegates to internal array.
33
41
  def empty?
34
42
  @raw_array.empty?
35
43
  end
36
-
37
- # Returns first item or first n elements.
38
- def first(n = 0)
39
- if n == 0 then
40
- self[0]
41
- else
42
- (0..(n - 1)).map{|i| self[i]}
43
- end
44
- end
45
-
44
+
46
45
  # Returns the last item.
47
46
  def last
48
47
  self[-1]
49
48
  end
50
49
 
51
- # Delegates to internal array.
52
- def length
53
- @raw_array.count
54
- end
55
-
56
- # Finds all the un-found items and returns an applicatively-evaluated array.
57
- def to_a
58
- @raw_array.map do |m|
59
- if m.is_a?(Ampere::Model) then
60
- m
61
- else
62
- @model.find(m)
63
- end
64
- end
65
- end
66
-
67
50
  # Class methods ###########################################################
68
51
 
69
52
 
data/lib/ampere/model.rb CHANGED
@@ -149,11 +149,10 @@ module Ampere
149
149
  end
150
150
 
151
151
  ### Class methods
152
-
153
152
  module ClassMethods
154
153
  # Returns an array of all the records that have been stored.
155
154
  def all
156
- Ampere.connection.keys("#{to_s.downcase}.*").map{|m| find m}
155
+ Ampere::Collection.new(self, Ampere.connection.keys("#{to_s.downcase}.*"))
157
156
  end
158
157
 
159
158
  # Declares a belongs_to relationship to another model.
@@ -0,0 +1,70 @@
1
+ require File.join(File.dirname(__FILE__), "..", "spec_helper.rb")
2
+
3
+ describe 'Collections are Ennumerable', :collections => true, :ennumerable => true do
4
+ before :all do
5
+ Redis.new.flushall
6
+ Ampere.connect
7
+
8
+ class President
9
+ include Ampere::Model
10
+
11
+ field :name
12
+ field :party
13
+
14
+ index :party
15
+ end
16
+
17
+ President.create :name => "Millard Fillmore" , :party => "Whig"
18
+ President.create :name => "Ulysses S. Grant" , :party => "Republican"
19
+ President.create :name => "Abraham Lincoln" , :party => "Republican"
20
+ President.create :name => "Franklin D. Roosevelt" , :party => "Democratic"
21
+ President.create :name => "John F. Kennedy" , :party => "Democratic"
22
+ President.create :name => "Jimmy Carter" , :party => "Democratic"
23
+ end
24
+
25
+ it 'should implement the #each method correctly' do
26
+ presidents = []
27
+ President.all.each {|p| presidents << p.name}
28
+ presidents.should == [
29
+ "Millard Fillmore" ,
30
+ "Ulysses S. Grant" ,
31
+ "Abraham Lincoln" ,
32
+ "Franklin D. Roosevelt",
33
+ "John F. Kennedy" ,
34
+ "Jimmy Carter"
35
+ ]
36
+ end
37
+
38
+ # These are just a handful of methods to ensure that the Enumerable module is
39
+ # being included correctly. They can safely be factored out since the #each
40
+ # one above should cover Enumerable if it's being included correctly.
41
+ it 'should implement the #all? method correctly' do
42
+ President.all.all? {|p| p.party != 'Green'}.should be_true
43
+ President.all.all? {|p| p.party == 'Democratic'}.should be_false
44
+ end
45
+
46
+ it 'should implement the #any? method correctly' do
47
+ President.all.any? {|p| p.party == 'Whig'}.should be_true
48
+ President.all.any? {|p| p.party == 'Green'}.should be_false
49
+ end
50
+
51
+ it 'should implement the #map method correctly' do
52
+ President.all.map(&:party).should == %w{Whig Republican Republican Democratic Democratic Democratic}
53
+ end
54
+
55
+ it 'should implement the #inject method correctly' do
56
+ party_counts = President.all.inject({}) do |h, p|
57
+ h[p.party.downcase.to_sym] ||= 0
58
+ h[p.party.downcase.to_sym] += 1
59
+ h
60
+ end
61
+ party_counts.should == {
62
+ whig: 1,
63
+ republican: 2,
64
+ democratic: 3
65
+ }
66
+ end
67
+
68
+
69
+ end
70
+
@@ -23,13 +23,21 @@ describe 'Collections', :collections => true do
23
23
  end
24
24
 
25
25
  ###
26
+
27
+ it 'should be returned by President.all' do
28
+ democrats = President.all
29
+ democrats.class.should == Ampere::Collection
30
+ democrats.model.should == President
31
+ democrats.raw_array.length.should == 6
32
+ democrats.count.should == 6
33
+ end
26
34
 
27
35
  it 'should be returned by where() queries' do
28
36
  democrats = President.where(:party => "Democratic")
29
37
  democrats.class.should == Ampere::Collection
30
38
  democrats.model.should == President
31
39
  democrats.raw_array.length.should == 3
32
- democrats.length.should == 3
40
+ democrats.count.should == 3
33
41
  end
34
42
 
35
43
  it 'should be accessible via [] like an Array' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ampere
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-20 00:00:00.000000000 Z
12
+ date: 2012-05-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
16
- requirement: &70280166250520 !ruby/object:Gem::Requirement
16
+ requirement: &2157094220 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70280166250520
24
+ version_requirements: *2157094220
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: shoulda
27
- requirement: &70280166249600 !ruby/object:Gem::Requirement
27
+ requirement: &2157093700 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70280166249600
35
+ version_requirements: *2157093700
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: cucumber
38
- requirement: &70280166263980 !ruby/object:Gem::Requirement
38
+ requirement: &2157093160 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70280166263980
46
+ version_requirements: *2157093160
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &70280166262680 !ruby/object:Gem::Requirement
49
+ requirement: &2157092600 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.0.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70280166262680
57
+ version_requirements: *2157092600
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: jeweler
60
- requirement: &70280166261440 !ruby/object:Gem::Requirement
60
+ requirement: &2157092100 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 1.6.4
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70280166261440
68
+ version_requirements: *2157092100
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: simplecov
71
- requirement: &70280166260320 !ruby/object:Gem::Requirement
71
+ requirement: &2157000680 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70280166260320
79
+ version_requirements: *2157000680
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
- requirement: &70280166259660 !ruby/object:Gem::Requirement
82
+ requirement: &2157000140 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70280166259660
90
+ version_requirements: *2157000140
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rdoc
93
- requirement: &70280166257860 !ruby/object:Gem::Requirement
93
+ requirement: &2156999620 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,7 +98,7 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *70280166257860
101
+ version_requirements: *2156999620
102
102
  description: An ActiveRecord/Mongoid-esque object model for the Redis key/value data
103
103
  store.
104
104
  email: max@villainousindustri.es
@@ -132,6 +132,7 @@ files:
132
132
  - spec/models/relationships/has_one_spec.rb
133
133
  - spec/models/updates_spec.rb
134
134
  - spec/module/ampere_spec.rb
135
+ - spec/module/collections_ennumerable_spec.rb
135
136
  - spec/module/collections_spec.rb
136
137
  - spec/spec_helper.rb
137
138
  - test/helper.rb
@@ -151,7 +152,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
151
152
  version: '0'
152
153
  segments:
153
154
  - 0
154
- hash: 719359362580818516
155
+ hash: 3615386568657855204
155
156
  required_rubygems_version: !ruby/object:Gem::Requirement
156
157
  none: false
157
158
  requirements:
@@ -160,7 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
161
  version: '0'
161
162
  requirements: []
162
163
  rubyforge_project:
163
- rubygems_version: 1.8.17
164
+ rubygems_version: 1.8.15
164
165
  signing_key:
165
166
  specification_version: 3
166
167
  summary: A pure Ruby ORM for Redis.