datamapper-shim 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,11 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ datamapper-shim (0.0.1)
5
+ activerecord (~> 3.2.0)
6
+ composite_primary_keys (~> 5.0.0)
7
+ json (~> 1.4)
8
+
1
9
  GEM
2
10
  remote: https://rubygems.org/
3
11
  specs:
@@ -13,8 +21,6 @@ GEM
13
21
  i18n (~> 0.6, >= 0.6.4)
14
22
  multi_json (~> 1.0)
15
23
  arel (3.0.3)
16
- attribute-defaults (0.6.0)
17
- activerecord (>= 3.0.0)
18
24
  builder (3.0.4)
19
25
  coderay (1.1.0)
20
26
  composite_primary_keys (5.0.13)
@@ -36,10 +42,8 @@ PLATFORMS
36
42
  ruby
37
43
 
38
44
  DEPENDENCIES
39
- activerecord (~> 3.2.0)
40
- attribute-defaults (~> 0.6.0)
41
- composite_primary_keys (~> 5.0.0)
42
- json (~> 1.4)
45
+ bundler (~> 1.3)
46
+ datamapper-shim!
43
47
  minitest (~> 5.2.3)
44
- pry
48
+ pry (~> 0.9.12.4)
45
49
  sqlite3 (~> 1.3.8)
@@ -4,11 +4,11 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "datamapper-shim"
7
- spec.version = "0.0.1"
7
+ spec.version = "0.0.2"
8
8
  spec.authors = ["ecin"]
9
9
  spec.email = ["ecin@copypastel.com"]
10
- spec.description = "Dress ActiveRecord models in DataMapper robes."
11
- spec.summary = "Migrate DataMapper models to ActiveRecord."
10
+ spec.description = "Dress ActiveRecord models in DataMapper robes"
11
+ spec.summary = "Migrate DataMapper models to ActiveRecord"
12
12
  spec.homepage = "https://github.com/typekit/datamapper-shim"
13
13
  spec.license = "MIT"
14
14
 
@@ -19,11 +19,9 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_dependency "json", "~> 1.4"
21
21
  spec.add_dependency "activerecord", "~> 3.2.0"
22
- spec.add_dependency "attribute-defaults", "~> 0.6.0"
23
22
  spec.add_dependency "composite_primary_keys", "~> 5.0.0"
24
- spec.add_dependency "json", "~> 1.4"
25
- spec.add_dependency "sqlite3", "~> 1.3.8"
26
23
 
24
+ spec.add_development_dependency "sqlite3", "~> 1.3.8"
27
25
  spec.add_development_dependency "bundler", "~> 1.3"
28
26
  spec.add_development_dependency "minitest", "~> 5.2.3"
29
27
  spec.add_development_dependency "pry", "~> 0.9.12.4"
@@ -1,6 +1,5 @@
1
1
  require "active_record"
2
2
  require "active_support/concern"
3
- require "attribute_defaults"
4
3
  require "composite_primary_keys"
5
4
  require "json"
6
5
 
@@ -101,7 +100,7 @@ module DataMapper
101
100
  atext = "[#{alpha}#{digit}\!\#\$\%\&\'\*+\/\=\?\^\_\`\{\|\}\~\-]"
102
101
  dot_atom_text = "#{atext}+([.]#{atext}*)*"
103
102
  dot_atom = "#{dot_atom_text}"
104
- qtext = '[^\\x0d\\x22\\x5c\\x8 0-\\xff]'
103
+ qtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]'
105
104
  text = "[\\x01-\\x09\\x11\\x12\\x14-\\x7f]"
106
105
  quoted_pair = "(\\x5c#{text})"
107
106
  qcontent = "(?:#{qtext}|#{quoted_pair})"
@@ -262,7 +261,7 @@ module DataMapper
262
261
  # :if => :class guarantees that the default value is always returned
263
262
  attr_default(name, :if => :class, :persisted => false) do |record|
264
263
  if record.new_record? && !record.__send__(:"#{name}_changed?")
265
- default
264
+ default.respond_to?(:call) ? default.call : default
266
265
  else
267
266
  record.instance_eval do
268
267
  read_attribute(name)
@@ -718,6 +717,51 @@ module DataMapper
718
717
  warn message
719
718
  end
720
719
  end
720
+
721
+ # Borrowed from https://github.com/bsm/attribute-defaults/blob/master/lib/attribute_defaults.rb
722
+ # Copyright (C) 2011 Dimitrij Denissenko
723
+
724
+ # Permission is hereby granted, free of charge, to any person obtaining
725
+ # a copy of this software and associated documentation files (the
726
+ # "Software"), to deal in the Software without restriction, including
727
+ # without limitation the rights to use, copy, modify, merge, publish,
728
+ # distribute, sublicense, and/or sell copies of the Software, and to
729
+ # permit persons to whom the Software is furnished to do so, subject to
730
+ # the following conditions:
731
+ #
732
+ # The above copyright notice and this permission notice shall be
733
+ # included in all copies or substantial portions of the Software.
734
+ #
735
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
736
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
737
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
738
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
739
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
740
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
741
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
742
+ def attr_default(sym, *args, &block)
743
+ options = args.extract_options!
744
+ default = options.delete(:default) || args.shift
745
+ raise 'Default value or block required' unless !default.nil? || block
746
+
747
+ evaluator = "__eval_attr_default_for_#{sym}".to_sym
748
+ setter = "__set_attr_default_for_#{sym}".to_sym
749
+ block ||= default.is_a?(Proc) ? default : proc { default }
750
+
751
+ after_initialize setter.to_sym
752
+ define_method(evaluator, &block)
753
+
754
+ module_eval(<<-EVAL, __FILE__, __LINE__ + 1)
755
+ def #{setter}
756
+ #{'return if persisted?' if options[:persisted] == false}
757
+ return unless self.__send__(:#{sym}).__send__(:#{options[:if] || 'nil?'})
758
+ value = #{evaluator}#{'(self)' unless block.arity.zero?}
759
+ self.__send__(:#{sym}=, value.duplicable? ? value.dup : value)
760
+ rescue ActiveModel::MissingAttributeError
761
+ end
762
+ EVAL
763
+ private evaluator, setter
764
+ end
721
765
  end
722
766
  end
723
767
  end
@@ -46,6 +46,7 @@ describe DataMapper::Shim do
46
46
  t.text :body
47
47
  t.references :user
48
48
  t.datetime :deleted_at
49
+ t.boolean :unread
49
50
  t.timestamps
50
51
  end
51
52
 
@@ -135,6 +136,8 @@ describe DataMapper::Shim do
135
136
  include DataMapper::Shim
136
137
 
137
138
  property :deleted_at, ParanoidDateTime, :writer => :protected
139
+ property :updated_at, DateTime, :default => proc { DateTime.now }, :reader => :protected
140
+ property :unread, Boolean, :default => true, :writer => :protected
138
141
  property :body, Text
139
142
 
140
143
  belongs_to :user
@@ -491,7 +494,6 @@ describe DataMapper::Shim do
491
494
  end
492
495
  end
493
496
 
494
-
495
497
  end
496
498
 
497
499
  describe "typecasted properties" do
@@ -640,6 +642,16 @@ describe DataMapper::Shim do
640
642
  Channel.new.state.must_equal :open
641
643
  end
642
644
 
645
+ it "handles default values on properties with protected writers" do
646
+ message = Message.new
647
+ message.unread.must_equal true
648
+ end
649
+
650
+ it "handles default values on properties with protected readers" do
651
+ message = Message.new
652
+ message.__send__(:updated_at).must_be_instance_of DateTime
653
+ end
654
+
643
655
  it "handles default boolean values" do
644
656
  User.new.must_be :active?
645
657
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datamapper-shim
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - ecin
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2014-02-27 00:00:00 Z
18
+ date: 2014-02-28 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: json
@@ -48,26 +48,10 @@ dependencies:
48
48
  version: 3.2.0
49
49
  type: :runtime
50
50
  version_requirements: *id002
51
- - !ruby/object:Gem::Dependency
52
- name: attribute-defaults
53
- prerelease: false
54
- requirement: &id003 !ruby/object:Gem::Requirement
55
- none: false
56
- requirements:
57
- - - ~>
58
- - !ruby/object:Gem::Version
59
- hash: 7
60
- segments:
61
- - 0
62
- - 6
63
- - 0
64
- version: 0.6.0
65
- type: :runtime
66
- version_requirements: *id003
67
51
  - !ruby/object:Gem::Dependency
68
52
  name: composite_primary_keys
69
53
  prerelease: false
70
- requirement: &id004 !ruby/object:Gem::Requirement
54
+ requirement: &id003 !ruby/object:Gem::Requirement
71
55
  none: false
72
56
  requirements:
73
57
  - - ~>
@@ -79,26 +63,11 @@ dependencies:
79
63
  - 0
80
64
  version: 5.0.0
81
65
  type: :runtime
82
- version_requirements: *id004
83
- - !ruby/object:Gem::Dependency
84
- name: json
85
- prerelease: false
86
- requirement: &id005 !ruby/object:Gem::Requirement
87
- none: false
88
- requirements:
89
- - - ~>
90
- - !ruby/object:Gem::Version
91
- hash: 7
92
- segments:
93
- - 1
94
- - 4
95
- version: "1.4"
96
- type: :runtime
97
- version_requirements: *id005
66
+ version_requirements: *id003
98
67
  - !ruby/object:Gem::Dependency
99
68
  name: sqlite3
100
69
  prerelease: false
101
- requirement: &id006 !ruby/object:Gem::Requirement
70
+ requirement: &id004 !ruby/object:Gem::Requirement
102
71
  none: false
103
72
  requirements:
104
73
  - - ~>
@@ -109,12 +78,12 @@ dependencies:
109
78
  - 3
110
79
  - 8
111
80
  version: 1.3.8
112
- type: :runtime
113
- version_requirements: *id006
81
+ type: :development
82
+ version_requirements: *id004
114
83
  - !ruby/object:Gem::Dependency
115
84
  name: bundler
116
85
  prerelease: false
117
- requirement: &id007 !ruby/object:Gem::Requirement
86
+ requirement: &id005 !ruby/object:Gem::Requirement
118
87
  none: false
119
88
  requirements:
120
89
  - - ~>
@@ -125,11 +94,11 @@ dependencies:
125
94
  - 3
126
95
  version: "1.3"
127
96
  type: :development
128
- version_requirements: *id007
97
+ version_requirements: *id005
129
98
  - !ruby/object:Gem::Dependency
130
99
  name: minitest
131
100
  prerelease: false
132
- requirement: &id008 !ruby/object:Gem::Requirement
101
+ requirement: &id006 !ruby/object:Gem::Requirement
133
102
  none: false
134
103
  requirements:
135
104
  - - ~>
@@ -141,11 +110,11 @@ dependencies:
141
110
  - 3
142
111
  version: 5.2.3
143
112
  type: :development
144
- version_requirements: *id008
113
+ version_requirements: *id006
145
114
  - !ruby/object:Gem::Dependency
146
115
  name: pry
147
116
  prerelease: false
148
- requirement: &id009 !ruby/object:Gem::Requirement
117
+ requirement: &id007 !ruby/object:Gem::Requirement
149
118
  none: false
150
119
  requirements:
151
120
  - - ~>
@@ -158,8 +127,8 @@ dependencies:
158
127
  - 4
159
128
  version: 0.9.12.4
160
129
  type: :development
161
- version_requirements: *id009
162
- description: Dress ActiveRecord models in DataMapper robes.
130
+ version_requirements: *id007
131
+ description: Dress ActiveRecord models in DataMapper robes
163
132
  email:
164
133
  - ecin@copypastel.com
165
134
  executables: []
@@ -206,6 +175,6 @@ rubyforge_project:
206
175
  rubygems_version: 1.8.25
207
176
  signing_key:
208
177
  specification_version: 3
209
- summary: Migrate DataMapper models to ActiveRecord.
178
+ summary: Migrate DataMapper models to ActiveRecord
210
179
  test_files:
211
180
  - test/data_mapper/shim_test.rb