minimapper 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ![Build status](https://secure.travis-ci.org/joakimk/minimapper.png) | [builds](https://travis-ci.org/#!/joakimk/minimapper/builds)
1
+ [![Build Status](https://secure.travis-ci.org/joakimk/minimapper.png)](http://travis-ci.org/joakimk/minimapper)
2
2
 
3
3
  # Minimapper
4
4
 
@@ -6,31 +6,28 @@
6
6
 
7
7
  ### Introduction
8
8
 
9
- A minimalistic way of separating your models from ORMs like ActiveRecord that allows you to swap out your persistence layer for an in-memory implementation in tests or use different persistence for different models.
9
+ Minimapper is a minimalistic way of separating models from ORMs like ActiveRecord. It enables you to test your models (and code using your models) within a [sub-second unit test suite](https://github.com/joakimk/fast_unit_tests_example) and makes it simpler to have a modular design as described in [Matt Wynne's Hexagonal Rails posts](http://blog.mattwynne.net/2012/04/09/hexagonal-rails-introduction/).
10
10
 
11
- If you're following good style you're probably already pushing all knowledge of your ORM down into your models or model-layer classes. This takes it a step further and let's you work with your models without depending on heavy frameworks like rails or needing a database.
11
+ Minimapper comes with an in-memory implementation of common CRUD operations. You can use this in tests to not hit the database where it isn't nessesary to do so. You can also develop new features without having to think about migrations until you need to persist data.
12
12
 
13
- Minimapper is a partial [repository-pattern](http://martinfowler.com/eaaCatalog/repository.html) implementation (it implements repositories and data mappers but not criteria builders).
13
+ Minimapper is not an ORM, instead it's a tool to make it simpler to handle persistence in existing applications using ORMs like ActiveRecord. It may also be an attractive alternative to using DataMapper 2 (when it's done) for new apps if you already know ActiveRecord well (most of the rails developers I know have many years of experience with ActiveRecord).
14
14
 
15
- ### Only the most basic API
16
-
17
- This library only implements the most basic persistence API (mostly just CRUD). Any significant additions will be made into separate gems (with names like "minimapper-FOO").
18
-
19
- The reasons for this are:
15
+ ### Early days
20
16
 
21
- * You should be able to depend on the API
22
- * It should be possible to learn all it does in a short time
23
- * It should be simple to add an adapter for a new database
24
- * It should be simple to maintain minimapper
17
+ The API may not be entirely stable yet and there are probably edge cases that aren't covered. However... it's most likely better to use this than to roll your own project specific solution. We need good tools for this kind of thing in the rails community, but to make that possible we need to gather around a few of them and make them good.
25
18
 
26
19
  ### Compatibility
27
20
 
28
21
  This gem is tested against all major rubies in both 1.8 and 1.9, see [.travis.yml](https://github.com/joakimk/minimapper/blob/master/.travis.yml). For each ruby version, the SQL mappers are tested against SQLite3, PostgreSQL and MySQL.
29
22
 
30
- ### Early days
23
+ ### Only the most basic API
31
24
 
32
- This is not quite intended for production use yet. It's a very small API that is well tested so you could use it in production apps, but there are still some problem areas that hasn't been well explored (for example, how to handle associations). It is probably better to use this than to roll your own project specific solution though.
25
+ This library only implements the most basic persistence API (mostly just CRUD). Any significant additions will be made into separate gems. The reasons for this are:
33
26
 
27
+ * It should have a stable API
28
+ * It should be possible to learn all it does in a short time
29
+ * It should be simple to add an adapter for a new database
30
+ * It should be simple to maintain minimapper
34
31
 
35
32
  ## Installation
36
33
 
@@ -176,10 +173,9 @@ It gets simpler to maintain if you use shared tests to test both implementations
176
173
 
177
174
  ### Typed attributes
178
175
 
179
- ``` ruby
180
- # Supported types: Integer, DateTime
181
- # TODO: Will probably not support more types, but instead provide a way to add custom conversions.
176
+ If you specify type, minimapper will attempt to convert into that type. Supported types: Integer and DateTime.
182
177
 
178
+ ``` ruby
183
179
  class User
184
180
  include Minimapper::Entity
185
181
  attributes [ :profile_id, Integer ]
@@ -191,9 +187,32 @@ User.new(:profile_id => " ").profile_id # => nil
191
187
  User.new(:profile_id => "foobar").profile_id # => nil
192
188
  ```
193
189
 
190
+ You can add your own type conversions like this:
191
+
192
+ ``` ruby
193
+ require "date"
194
+
195
+ class ToDate
196
+ def convert(value)
197
+ Date.parse(value) rescue nil
198
+ end
199
+ end
200
+
201
+ Minimapper::Entity::Convert.register_converter(Date, ToDate.new)
202
+
203
+ class User
204
+ include Minimapper::Entity
205
+ attributes [ :profile_id, Date ]
206
+ end
207
+
208
+ User.new(:reminder_on => "2012-01-01").reminder # => #<Date: 2012-01-01 ...>
209
+ ```
210
+
211
+ Minimapper only calls #convert on non-empty strings. When the value is blank or nil, the attribute is set to nil.
212
+
194
213
  ### Custom entity class
195
214
 
196
- You don't need to use Minimapper::Entity. The core API is implemented in [Minimapper::Entity::Core](https://github.com/joakimk/minimapper/blob/master/lib/minimapper/entity/core.rb). Either include that module or implement your own version.
215
+ [Minimapper::Entity](https://github.com/joakimk/minimapper/blob/master/lib/minimapper/entity.rb) adds some convenience methods for when a model is used within a rails application. If you don't need that you can just include the core API from the [Minimapper::Entity::Core](https://github.com/joakimk/minimapper/blob/master/lib/minimapper/entity/core.rb) module (or implement your own version that behaves like [Minimapper::Entity::Core](https://github.com/joakimk/minimapper/blob/master/lib/minimapper/entity/core.rb)).
197
216
 
198
217
  ### Adding a new mapper
199
218
 
@@ -0,0 +1,11 @@
1
+ module Minimapper
2
+ module Entity
3
+ class Convert
4
+ class ToDateTime
5
+ def convert(value)
6
+ DateTime.parse(value) rescue nil
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module Minimapper
2
+ module Entity
3
+ class Convert
4
+ class ToInteger
5
+ def convert(value)
6
+ if value =~ /[0-9]/
7
+ value.to_i
8
+ else
9
+ nil
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,6 @@
1
+ require 'minimapper/entity/convert/to_integer'
2
+ require 'minimapper/entity/convert/to_date_time'
3
+
1
4
  module Minimapper
2
5
  module Entity
3
6
  class Convert
@@ -5,32 +8,31 @@ module Minimapper
5
8
  @value = value
6
9
  end
7
10
 
11
+ def self.register_converter(type, converter)
12
+ @@converters ||= {}
13
+ @@converters[type.to_s] = converter
14
+ end
15
+
8
16
  def to(type)
9
- return if value.blank?
17
+ return nil if value.blank?
10
18
  return value unless value.is_a?(String)
11
19
 
12
- case type.to_s
13
- when "Integer"
14
- to_integer
15
- when "DateTime"
16
- to_date_time
17
- else
18
- value
19
- end
20
+ converter_for(type).convert(value)
20
21
  end
21
22
 
23
+ register_converter Integer, ToInteger.new
24
+ register_converter DateTime, ToDateTime.new
25
+
22
26
  private
23
27
 
24
- def to_integer
25
- if value =~ /[0-9]/
26
- value.to_i
27
- else
28
- nil
29
- end
28
+ def converter_for(type)
29
+ @@converters.fetch(type.to_s, NoOpConverter.new)
30
30
  end
31
31
 
32
- def to_date_time
33
- DateTime.parse(value) rescue nil
32
+ class NoOpConverter
33
+ def convert(value)
34
+ value
35
+ end
34
36
  end
35
37
 
36
38
  attr_reader :value
@@ -1,3 +1,3 @@
1
1
  module Minimapper
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/minimapper.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |gem|
8
8
  gem.version = Minimapper::VERSION
9
9
  gem.authors = ["Joakim Kolsjö"]
10
10
  gem.email = ["joakim.kolsjo@gmail.com"]
11
- gem.description = %q{A minimalistic way of separating your models from ORMs like ActiveRecord (by implementing the repository pattern)}
12
- gem.summary = %q{A minimalistic way of separating your models from ORMs like ActiveRecord (by implementing the repository pattern)}
11
+ gem.description = %q{A minimalistic way of separating your models from ORMs like ActiveRecord.}
12
+ gem.summary = %q{A minimalistic way of separating your models from ORMs like ActiveRecord.}
13
13
  gem.homepage = ""
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minimapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
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-11-05 00:00:00.000000000 Z
12
+ date: 2012-11-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70233951931080 !ruby/object:Gem::Requirement
16
+ requirement: &70178508749460 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,9 +21,8 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70233951931080
25
- description: A minimalistic way of separating your models from ORMs like ActiveRecord
26
- (by implementing the repository pattern)
24
+ version_requirements: *70178508749460
25
+ description: A minimalistic way of separating your models from ORMs like ActiveRecord.
27
26
  email:
28
27
  - joakim.kolsjo@gmail.com
29
28
  executables: []
@@ -43,6 +42,8 @@ files:
43
42
  - lib/minimapper/entity.rb
44
43
  - lib/minimapper/entity/attributes.rb
45
44
  - lib/minimapper/entity/convert.rb
45
+ - lib/minimapper/entity/convert/to_date_time.rb
46
+ - lib/minimapper/entity/convert/to_integer.rb
46
47
  - lib/minimapper/entity/core.rb
47
48
  - lib/minimapper/entity/rails.rb
48
49
  - lib/minimapper/memory.rb
@@ -74,19 +75,24 @@ required_ruby_version: !ruby/object:Gem::Requirement
74
75
  - - ! '>='
75
76
  - !ruby/object:Gem::Version
76
77
  version: '0'
78
+ segments:
79
+ - 0
80
+ hash: 3127199441283205218
77
81
  required_rubygems_version: !ruby/object:Gem::Requirement
78
82
  none: false
79
83
  requirements:
80
84
  - - ! '>='
81
85
  - !ruby/object:Gem::Version
82
86
  version: '0'
87
+ segments:
88
+ - 0
89
+ hash: 3127199441283205218
83
90
  requirements: []
84
91
  rubyforge_project:
85
92
  rubygems_version: 1.8.5
86
93
  signing_key:
87
94
  specification_version: 3
88
- summary: A minimalistic way of separating your models from ORMs like ActiveRecord
89
- (by implementing the repository pattern)
95
+ summary: A minimalistic way of separating your models from ORMs like ActiveRecord.
90
96
  test_files:
91
97
  - spec/ar_spec.rb
92
98
  - spec/spec_helper.rb