ambry 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Changelog.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Ambry Changelog
2
2
 
3
- ## 0.1.0 - NOT RELEASED YET
3
+ ## [0.1.1](https://github.com/norman/ambry/tree/0.1.1) - 2011-08-24 ([diff](https://github.com/norman/ambry/compare/0.1.0...0.1.1))
4
4
 
5
- Initial release.
5
+ ### [Esad Hajdarevic](https://github.com/esad)
6
+
7
+ * Allow attribute keys to be strings
8
+ * Pass key as attribute when loading database. This allows you to avoid specifying the key twice when manually editing a YAML file.
9
+ * Remove use of String#blank? to avoid depending on Active Support
10
+
11
+ ### [Ignacio Carrera](https://github.com/nachokb)
12
+
13
+ * Added #last and #inspect
14
+
15
+
16
+ ## [0.1.0](https://github.com/norman/ambry/tree/0.1.0) - 2011-08-18
17
+
18
+ Initial release.
data/README.md CHANGED
@@ -15,6 +15,9 @@ quick samples.
15
15
 
16
16
  ## A quick tour
17
17
 
18
+ # Initialize a default adapter.
19
+ Ambry::Adapter.new
20
+
18
21
  # Create a model.
19
22
  class Country
20
23
  # Turn any Ruby object into a Ambry model by extending this module.
@@ -32,6 +35,10 @@ quick samples.
32
35
  def in_region(region)
33
36
  find {|c| c.region == region)
34
37
  end
38
+
39
+ def alphabetical
40
+ sort_by {|c| c.name}
41
+ end
35
42
  end
36
43
 
37
44
  # Root filter, can be used to setup relations.
@@ -51,6 +58,9 @@ quick samples.
51
58
  # Do some searches
52
59
  big_asian_countries = Country.big.in_region(:asia)
53
60
  countries_that_start_with_c = Country.find {|c| c.name =~ /^C/}
61
+ # #first and #last only make sense if you run Ruby 1.9 (creation order) or explicitly specified an order
62
+ first_alphabetical = Country.alphabetical.first
63
+ last_alphabetical = Country.alphabetical.last
54
64
 
55
65
 
56
66
  ## Installation
@@ -54,8 +54,8 @@ get "/books/:slug" do |slug|
54
54
  end
55
55
 
56
56
  post "/books" do
57
- Book.delete params[:slug] unless params[:slug].blank?
58
- @book = Book.create params unless params[:title].blank?
57
+ Book.delete params[:slug] if params[:slug]
58
+ @book = Book.create params if params[:title]
59
59
  redirect "/"
60
60
  end
61
61
 
@@ -41,6 +41,12 @@ module Ambry
41
41
  block_given? ? all.detect(&block) : all.first
42
42
  end
43
43
 
44
+ # With no block, returns an instance for the first key. If a block is given,
45
+ # it returns the first instance yielding a true value.
46
+ def last(&block)
47
+ block_given? ? all.reverse.detect(&block) : all.last
48
+ end
49
+
44
50
  # With no block, returns the number of keys. If a block is given, counts the
45
51
  # number of elements yielding a true value.
46
52
  def count(&block)
@@ -99,6 +105,14 @@ module Ambry
99
105
  @callable = callable
100
106
  end
101
107
 
108
+ def reverse
109
+ KeyIterator.new(keys.reverse, &callable)
110
+ end
111
+
112
+ def last
113
+ callable.call keys.last
114
+ end
115
+
102
116
  def each(&block)
103
117
  block_given? ? keys.each {|k| yield callable.call(k)} : to_enum
104
118
  end
@@ -39,7 +39,7 @@ module Ambry
39
39
  end
40
40
 
41
41
  def import_data
42
- data.blank? ? {} : Marshal.load(Zlib::Inflate.inflate(verifier.verify(data)))
42
+ (!data || data.empty?) ? {} : Marshal.load(Zlib::Inflate.inflate(verifier.verify(data)))
43
43
  end
44
44
 
45
45
  def load_database
@@ -14,7 +14,7 @@ module Ambry
14
14
 
15
15
  def load_database
16
16
  @db = import_data
17
- @db.blank? ? @db = {} : @db.map(&:freeze)
17
+ (!@db || @db.empty?) ? @db = {} : @db.map(&:freeze)
18
18
  rescue Errno::ENOENT
19
19
  # @TODO warn via logger when file doesn't exist
20
20
  @db = {}
@@ -8,7 +8,7 @@ module Ambry
8
8
 
9
9
  # Allows accessing a hash attribute as a method.
10
10
  def method_missing(symbol)
11
- hash[symbol] or raise NoMethodError
11
+ hash[symbol] or hash[symbol.to_s] or raise NoMethodError
12
12
  end
13
13
 
14
14
  # Allows accessing a hash attribute as hash key, either a string or symbol.
data/lib/ambry/mapper.rb CHANGED
@@ -8,7 +8,7 @@ module Ambry
8
8
  attr :hash
9
9
  attr_accessor :adapter_name, :klass, :indexes, :options
10
10
  def_delegators :hash, :clear, :delete
11
- def_delegators :key_set, :all, :count, :find, :find_by_key, :first, :keys
11
+ def_delegators :key_set, :all, :count, :find, :find_by_key, :first, :last, :keys
12
12
 
13
13
  def initialize(klass, adapter_name = nil, options = {})
14
14
  @klass = klass
@@ -51,7 +51,7 @@ module Ambry
51
51
 
52
52
  # Get an instance by key
53
53
  def get(key)
54
- klass.send :from_hash, self[key]
54
+ klass.send :from_hash, self[key].merge(klass.id_method => key)
55
55
  end
56
56
 
57
57
  def key_set
data/lib/ambry/model.rb CHANGED
@@ -17,7 +17,7 @@ module Ambry
17
17
  attr_accessor :attribute_names, :id_method, :mapper
18
18
  attr_reader :key_class
19
19
  def_delegators(*[:find, Enumerable.public_instance_methods(false)].flatten)
20
- def_delegators(:mapper, :[], :all, :delete, :first, :get, :count, :find, :find_by_key, :keys)
20
+ def_delegators(:mapper, :[], :all, :delete, :first, :last, :get, :count, :find, :find_by_key, :keys)
21
21
  alias id_field id_method=
22
22
 
23
23
  def field(*names)
@@ -27,7 +27,7 @@ module Ambry
27
27
  attribute_names << name.to_sym
28
28
  class_eval(<<-EOM, __FILE__, __LINE__ + 1)
29
29
  def #{name}
30
- @#{name} or (@attributes[:#{name}] if @attributes)
30
+ @#{name} or (@attributes[:#{name}] || @attributes['#{name}'] if @attributes)
31
31
  end
32
32
 
33
33
  def #{name}=(value)
@@ -89,6 +89,10 @@ module Ambry
89
89
  @mapper ||= Mapper.new(self, name, options)
90
90
  end
91
91
  end
92
+
93
+ def inspect
94
+ "#{name}(#{attribute_names * ', '})"
95
+ end
92
96
  end
93
97
 
94
98
  module InstanceMethods
@@ -159,6 +163,10 @@ module Ambry
159
163
  def delete
160
164
  self.class.delete(self.to_id)
161
165
  end
166
+
167
+ def inspect
168
+ "#<#{self.class.name} #{self.class.attribute_names.map { |attr| "#{attr}: #{self.send(attr).inspect}" } * ', ' }>"
169
+ end
162
170
  end
163
171
  end
164
172
  end
data/lib/ambry/version.rb CHANGED
@@ -2,7 +2,7 @@ module Ambry
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 0
5
+ TINY = 1
6
6
  BUILD = nil
7
7
  STRING = [MAJOR, MINOR, TINY, BUILD].compact.join('.')
8
8
  end
File without changes
data/spec/fixtures.yml CHANGED
@@ -1,17 +1,13 @@
1
1
  ---
2
2
  Person:
3
3
  moe@3stooges.com:
4
- :name: Moe Howard
5
- :email: moe@3stooges.com
4
+ :name: Moe Howard
6
5
  shemp@3stooges.com:
7
- :name: Shemp Howard
8
- :email: shemp@3stooges.com
6
+ :name: Shemp Howard
9
7
  curly@3stooges.com:
10
- :name: Curly Howard
11
- :email: curly@3stooges.com
8
+ :name: Curly Howard
12
9
  larry@3stooges.com:
13
- :name: Larry Fine
14
- :email: larry@3stooges.com
10
+ :name: Larry Fine
15
11
  "MyModule::Animal":
16
12
  Canis Familaris:
17
13
  :species: Canis Familaris
data/spec/key_set_spec.rb CHANGED
@@ -35,11 +35,21 @@ describe Ambry::AbstractKeySet do
35
35
 
36
36
  describe "#first" do
37
37
  it "should return the first matching instance when called with a block" do
38
- assert_equal "Curly Howard", Person.first {|p| p.name =~ /Curly/}.name
38
+ assert_equal "Curly Howard", Person.alphabetical.first {|p| p.name =~ /Curly/}.name
39
39
  end
40
40
 
41
41
  it "should return the first instance when not called with a block" do
42
- assert_kind_of Person, Person.first
42
+ assert_kind_of Person, Person.alphabetical.first
43
+ end
44
+ end
45
+
46
+ describe "#last" do
47
+ it "should return the last matching instance when called with a block" do
48
+ assert_equal "Shemp Howard", Person.alphabetical.last {|p| p.name =~ /Howard/}.name
49
+ end
50
+
51
+ it "should return the last instance when not called with a block" do
52
+ assert_kind_of Person, Person.alphabetical.last
43
53
  end
44
54
  end
45
55
 
data/spec/spec_helper.rb CHANGED
@@ -23,7 +23,11 @@ class Person
23
23
 
24
24
  filters do
25
25
  def non_howards
26
- find {|p| p[:name] !~ /Howard/}
26
+ find {|p| p.name !~ /Howard/}
27
+ end
28
+
29
+ def alphabetical
30
+ sort_by {|p| p.name}
27
31
  end
28
32
  end
29
33
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ambry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-18 00:00:00.000000000 -03:00
13
- default_executable:
12
+ date: 2011-08-24 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: ffaker
17
- requirement: &70280928791740 !ruby/object:Gem::Requirement
16
+ requirement: &70346028569780 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ! '>='
@@ -22,10 +21,10 @@ dependencies:
22
21
  version: '0'
23
22
  type: :development
24
23
  prerelease: false
25
- version_requirements: *70280928791740
24
+ version_requirements: *70346028569780
26
25
  - !ruby/object:Gem::Dependency
27
26
  name: minitest
28
- requirement: &70280928790160 !ruby/object:Gem::Requirement
27
+ requirement: &70346028569280 !ruby/object:Gem::Requirement
29
28
  none: false
30
29
  requirements:
31
30
  - - ~>
@@ -33,10 +32,10 @@ dependencies:
33
32
  version: 2.2.2
34
33
  type: :development
35
34
  prerelease: false
36
- version_requirements: *70280928790160
35
+ version_requirements: *70346028569280
37
36
  - !ruby/object:Gem::Dependency
38
37
  name: mocha
39
- requirement: &70280928788880 !ruby/object:Gem::Requirement
38
+ requirement: &70346028568860 !ruby/object:Gem::Requirement
40
39
  none: false
41
40
  requirements:
42
41
  - - ! '>='
@@ -44,10 +43,10 @@ dependencies:
44
43
  version: '0'
45
44
  type: :development
46
45
  prerelease: false
47
- version_requirements: *70280928788880
46
+ version_requirements: *70346028568860
48
47
  - !ruby/object:Gem::Dependency
49
48
  name: activesupport
50
- requirement: &70280928787220 !ruby/object:Gem::Requirement
49
+ requirement: &70346028568320 !ruby/object:Gem::Requirement
51
50
  none: false
52
51
  requirements:
53
52
  - - ~>
@@ -55,10 +54,10 @@ dependencies:
55
54
  version: '3.0'
56
55
  type: :development
57
56
  prerelease: false
58
- version_requirements: *70280928787220
57
+ version_requirements: *70346028568320
59
58
  - !ruby/object:Gem::Dependency
60
59
  name: activemodel
61
- requirement: &70280928786280 !ruby/object:Gem::Requirement
60
+ requirement: &70346028567820 !ruby/object:Gem::Requirement
62
61
  none: false
63
62
  requirements:
64
63
  - - ~>
@@ -66,10 +65,10 @@ dependencies:
66
65
  version: '3.0'
67
66
  type: :development
68
67
  prerelease: false
69
- version_requirements: *70280928786280
68
+ version_requirements: *70346028567820
70
69
  - !ruby/object:Gem::Dependency
71
70
  name: rake
72
- requirement: &70280928785640 !ruby/object:Gem::Requirement
71
+ requirement: &70346028567440 !ruby/object:Gem::Requirement
73
72
  none: false
74
73
  requirements:
75
74
  - - ! '>='
@@ -77,7 +76,7 @@ dependencies:
77
76
  version: '0'
78
77
  type: :development
79
78
  prerelease: false
80
- version_requirements: *70280928785640
79
+ version_requirements: *70346028567440
81
80
  description: ! " Ambry is not an ORM, man! It's a database and ORM replacement
82
81
  for (mostly)\n static models and small datasets. It provides ActiveModel compatibility,
83
82
  and\n flexible searching and storage.\n"
@@ -107,8 +106,8 @@ files:
107
106
  - lib/ambry/mapper.rb
108
107
  - lib/ambry/model.rb
109
108
  - lib/ambry/version.rb
110
- - lib/generators/norman_generator.rb
111
- - lib/rack/norman.rb
109
+ - lib/generators/ambry_generator.rb
110
+ - lib/rack/ambry.rb
112
111
  - spec/active_model_spec.rb
113
112
  - spec/adapter_spec.rb
114
113
  - spec/cookie_adapter_spec.rb
@@ -118,7 +117,6 @@ files:
118
117
  - spec/mapper_spec.rb
119
118
  - spec/model_spec.rb
120
119
  - spec/spec_helper.rb
121
- has_rdoc: true
122
120
  homepage: http://github.com/norman/ambry
123
121
  licenses: []
124
122
  post_install_message:
@@ -139,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
137
  version: '0'
140
138
  requirements: []
141
139
  rubyforge_project: ! '[none]'
142
- rubygems_version: 1.6.2
140
+ rubygems_version: 1.8.6
143
141
  signing_key:
144
142
  specification_version: 3
145
143
  summary: An ActiveModel-compatible ORM-like library for storing model instances in