attribute-defaults 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +21 -0
- data/lib/attribute_defaults.rb +5 -2
- metadata +29 -33
- data/.gitignore +0 -2
- data/Gemfile +0 -8
- data/Gemfile.lock +0 -39
- data/Rakefile +0 -25
- data/VERSION +0 -1
- data/attribute-defaults.gemspec +0 -57
- data/spec/attribute_defaults_spec.rb +0 -78
- data/spec/spec_helper.rb +0 -59
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
Copyright (C) 2011 Dimitrij Denissenko
|
3
|
+
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
a copy of this software and associated documentation files (the
|
6
|
+
"Software"), to deal in the Software without restriction, including
|
7
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/attribute_defaults.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_support/concern'
|
3
|
+
|
1
4
|
module ActiveRecord
|
2
5
|
module AttributesWithDefaults
|
3
6
|
extend ActiveSupport::Concern
|
@@ -20,7 +23,7 @@ module ActiveRecord
|
|
20
23
|
|
21
24
|
evaluator = "__eval_attr_default_for_#{sym}".to_sym
|
22
25
|
setter = "__set_attr_default_for_#{sym}".to_sym
|
23
|
-
block ||= default.is_a?(Proc) ? default :
|
26
|
+
block ||= default.is_a?(Proc) ? default : proc { default }
|
24
27
|
|
25
28
|
after_initialize setter.to_sym
|
26
29
|
define_method(evaluator, &block)
|
@@ -29,7 +32,7 @@ module ActiveRecord
|
|
29
32
|
def #{setter}
|
30
33
|
#{'return if persisted?' if options[:persisted] == false}
|
31
34
|
return unless self.#{sym}.send(:#{options[:if] || 'nil?'})
|
32
|
-
value = #{evaluator}(self)
|
35
|
+
value = #{evaluator}#{'(self)' unless block.arity.zero?}
|
33
36
|
self.#{sym} = value.duplicable? ? value.dup : value
|
34
37
|
rescue ActiveModel::MissingAttributeError
|
35
38
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribute-defaults
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Dimitrij Denissenko
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-06-13 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -24,7 +24,7 @@ dependencies:
|
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
26
|
requirements:
|
27
|
-
- -
|
27
|
+
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
hash: 7
|
30
30
|
segments:
|
@@ -40,7 +40,7 @@ dependencies:
|
|
40
40
|
requirement: &id002 !ruby/object:Gem::Requirement
|
41
41
|
none: false
|
42
42
|
requirements:
|
43
|
-
- -
|
43
|
+
- - ~>
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
hash: 7
|
46
46
|
segments:
|
@@ -50,33 +50,26 @@ dependencies:
|
|
50
50
|
version: 3.0.0
|
51
51
|
type: :runtime
|
52
52
|
version_requirements: *id002
|
53
|
-
description:
|
53
|
+
description: ActiveRecord plugin that allows to specify default values for attributes
|
54
54
|
email: dimitrij@blacksquaremedia.com
|
55
55
|
executables: []
|
56
56
|
|
57
57
|
extensions: []
|
58
58
|
|
59
|
-
extra_rdoc_files:
|
60
|
-
|
59
|
+
extra_rdoc_files: []
|
60
|
+
|
61
61
|
files:
|
62
|
-
-
|
63
|
-
- Gemfile
|
64
|
-
- Gemfile.lock
|
62
|
+
- LICENSE
|
65
63
|
- README.rdoc
|
66
|
-
- Rakefile
|
67
|
-
- VERSION
|
68
|
-
- attribute-defaults.gemspec
|
69
64
|
- lib/attribute-defaults.rb
|
70
65
|
- lib/attribute_defaults.rb
|
71
|
-
- spec/attribute_defaults_spec.rb
|
72
|
-
- spec/spec_helper.rb
|
73
66
|
has_rdoc: true
|
74
|
-
homepage:
|
67
|
+
homepage: https://github.com/bsm/attribute-defaults
|
75
68
|
licenses: []
|
76
69
|
|
77
70
|
post_install_message:
|
78
|
-
rdoc_options:
|
79
|
-
|
71
|
+
rdoc_options: []
|
72
|
+
|
80
73
|
require_paths:
|
81
74
|
- lib
|
82
75
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -84,26 +77,29 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
84
77
|
requirements:
|
85
78
|
- - ">="
|
86
79
|
- !ruby/object:Gem::Version
|
87
|
-
hash:
|
80
|
+
hash: 57
|
88
81
|
segments:
|
89
|
-
-
|
90
|
-
|
82
|
+
- 1
|
83
|
+
- 8
|
84
|
+
- 7
|
85
|
+
version: 1.8.7
|
91
86
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
87
|
none: false
|
93
88
|
requirements:
|
94
89
|
- - ">="
|
95
90
|
- !ruby/object:Gem::Version
|
96
|
-
hash:
|
91
|
+
hash: 23
|
97
92
|
segments:
|
98
|
-
-
|
99
|
-
|
93
|
+
- 1
|
94
|
+
- 3
|
95
|
+
- 6
|
96
|
+
version: 1.3.6
|
100
97
|
requirements: []
|
101
98
|
|
102
99
|
rubyforge_project:
|
103
|
-
rubygems_version: 1.
|
100
|
+
rubygems_version: 1.6.2
|
104
101
|
signing_key:
|
105
102
|
specification_version: 3
|
106
|
-
summary: Specify default values for attributes
|
107
|
-
test_files:
|
108
|
-
|
109
|
-
- spec/spec_helper.rb
|
103
|
+
summary: Specify default values for ActiveRecord attributes
|
104
|
+
test_files: []
|
105
|
+
|
data/.gitignore
DELETED
data/Gemfile
DELETED
data/Gemfile.lock
DELETED
@@ -1,39 +0,0 @@
|
|
1
|
-
GEM
|
2
|
-
remote: http://rubygems.org/
|
3
|
-
specs:
|
4
|
-
activemodel (3.0.0)
|
5
|
-
activesupport (= 3.0.0)
|
6
|
-
builder (~> 2.1.2)
|
7
|
-
i18n (~> 0.4.1)
|
8
|
-
activerecord (3.0.0)
|
9
|
-
activemodel (= 3.0.0)
|
10
|
-
activesupport (= 3.0.0)
|
11
|
-
arel (~> 1.0.0)
|
12
|
-
tzinfo (~> 0.3.23)
|
13
|
-
activesupport (3.0.0)
|
14
|
-
arel (1.0.1)
|
15
|
-
activesupport (~> 3.0.0)
|
16
|
-
builder (2.1.2)
|
17
|
-
diff-lcs (1.1.2)
|
18
|
-
i18n (0.4.1)
|
19
|
-
rspec (2.0.0.beta.22)
|
20
|
-
rspec-core (= 2.0.0.beta.22)
|
21
|
-
rspec-expectations (= 2.0.0.beta.22)
|
22
|
-
rspec-mocks (= 2.0.0.beta.22)
|
23
|
-
rspec-core (2.0.0.beta.22)
|
24
|
-
rspec-expectations (2.0.0.beta.22)
|
25
|
-
diff-lcs (>= 1.1.2)
|
26
|
-
rspec-mocks (2.0.0.beta.22)
|
27
|
-
rspec-core (= 2.0.0.beta.22)
|
28
|
-
rspec-expectations (= 2.0.0.beta.22)
|
29
|
-
sqlite3-ruby (1.3.1)
|
30
|
-
tzinfo (0.3.23)
|
31
|
-
|
32
|
-
PLATFORMS
|
33
|
-
ruby
|
34
|
-
|
35
|
-
DEPENDENCIES
|
36
|
-
activerecord (= 3.0.0)
|
37
|
-
activesupport (= 3.0.0)
|
38
|
-
rspec (~> 2.0.0.beta)
|
39
|
-
sqlite3-ruby
|
data/Rakefile
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
require 'rake'
|
2
|
-
require 'rspec/mocks/version'
|
3
|
-
require 'rspec/core/rake_task'
|
4
|
-
|
5
|
-
RSpec::Core::RakeTask.new(:spec)
|
6
|
-
|
7
|
-
desc 'Default: run specs.'
|
8
|
-
task :default => :spec
|
9
|
-
|
10
|
-
begin
|
11
|
-
require 'jeweler'
|
12
|
-
Jeweler::Tasks.new do |gemspec|
|
13
|
-
gemspec.name = "attribute-defaults"
|
14
|
-
gemspec.summary = "Specify default values for attributes"
|
15
|
-
gemspec.description = "Simple ActiveRecord plugin that allows to specify default values for attributes"
|
16
|
-
gemspec.email = "dimitrij@blacksquaremedia.com"
|
17
|
-
gemspec.homepage = "http://github.com/bsm/attribute-defaults"
|
18
|
-
gemspec.authors = ["Dimitrij Denissenko"]
|
19
|
-
gemspec.add_runtime_dependency "activerecord", ">= 3.0.0"
|
20
|
-
gemspec.add_runtime_dependency "activesupport", ">= 3.0.0"
|
21
|
-
end
|
22
|
-
Jeweler::GemcutterTasks.new
|
23
|
-
rescue LoadError
|
24
|
-
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
25
|
-
end
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.1.1
|
data/attribute-defaults.gemspec
DELETED
@@ -1,57 +0,0 @@
|
|
1
|
-
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
-
# -*- encoding: utf-8 -*-
|
5
|
-
|
6
|
-
Gem::Specification.new do |s|
|
7
|
-
s.name = %q{attribute-defaults}
|
8
|
-
s.version = "0.1.1"
|
9
|
-
|
10
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Dimitrij Denissenko"]
|
12
|
-
s.date = %q{2010-09-15}
|
13
|
-
s.description = %q{Simple ActiveRecord plugin that allows to specify default values for attributes}
|
14
|
-
s.email = %q{dimitrij@blacksquaremedia.com}
|
15
|
-
s.extra_rdoc_files = [
|
16
|
-
"README.rdoc"
|
17
|
-
]
|
18
|
-
s.files = [
|
19
|
-
".gitignore",
|
20
|
-
"Gemfile",
|
21
|
-
"Gemfile.lock",
|
22
|
-
"README.rdoc",
|
23
|
-
"Rakefile",
|
24
|
-
"VERSION",
|
25
|
-
"attribute-defaults.gemspec",
|
26
|
-
"lib/attribute-defaults.rb",
|
27
|
-
"lib/attribute_defaults.rb",
|
28
|
-
"spec/attribute_defaults_spec.rb",
|
29
|
-
"spec/spec_helper.rb"
|
30
|
-
]
|
31
|
-
s.homepage = %q{http://github.com/bsm/attribute-defaults}
|
32
|
-
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
-
s.require_paths = ["lib"]
|
34
|
-
s.rubygems_version = %q{1.3.7}
|
35
|
-
s.summary = %q{Specify default values for attributes}
|
36
|
-
s.test_files = [
|
37
|
-
"spec/attribute_defaults_spec.rb",
|
38
|
-
"spec/spec_helper.rb"
|
39
|
-
]
|
40
|
-
|
41
|
-
if s.respond_to? :specification_version then
|
42
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
|
-
s.specification_version = 3
|
44
|
-
|
45
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
46
|
-
s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0"])
|
47
|
-
s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0"])
|
48
|
-
else
|
49
|
-
s.add_dependency(%q<activerecord>, [">= 3.0.0"])
|
50
|
-
s.add_dependency(%q<activesupport>, [">= 3.0.0"])
|
51
|
-
end
|
52
|
-
else
|
53
|
-
s.add_dependency(%q<activerecord>, [">= 3.0.0"])
|
54
|
-
s.add_dependency(%q<activesupport>, [">= 3.0.0"])
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
@@ -1,78 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
|
3
|
-
describe ActiveRecord::AttributesWithDefaults do
|
4
|
-
|
5
|
-
def foo(options = {})
|
6
|
-
@foo ||= Foo.new options.reverse_merge(:age => 30)
|
7
|
-
end
|
8
|
-
|
9
|
-
def bar(options = {})
|
10
|
-
@bar ||= Bar.new options.reverse_merge(:age => 30)
|
11
|
-
end
|
12
|
-
|
13
|
-
def baz(options = {})
|
14
|
-
@baz ||= Baz.new
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'should initialize records setting defaults' do
|
18
|
-
foo.description.should == '(no description)'
|
19
|
-
foo.locale.should == 'en'
|
20
|
-
foo.birth_year.should == 30.years.ago.year
|
21
|
-
end
|
22
|
-
|
23
|
-
it 'should allow to set custom value conditions' do
|
24
|
-
foo(:locale => '', :description => '')
|
25
|
-
foo.locale.should == ''
|
26
|
-
foo.description.should == '(no description)'
|
27
|
-
|
28
|
-
Bar.new(:some_arr => nil).some_arr.should == [1, 2, 3]
|
29
|
-
Bar.new(:some_arr => []).some_arr.should == [1, 2, 3]
|
30
|
-
Bar.new(:some_arr => [:A, :B]).some_arr.should == [:A, :B]
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'should not override attributes that were set manually' do
|
34
|
-
foo(:locale => 'en-GB').locale.should == 'en-GB'
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'should respect :persisted => false option' do
|
38
|
-
persisted = Foo.first
|
39
|
-
persisted.description.should == '(no description)'
|
40
|
-
persisted.locale.should be_nil
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'should work with protected attributes' do
|
44
|
-
foo(:description => 'Custom').description.should == '(no description)'
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'should correctly apply defaults in subclasses' do
|
48
|
-
bar.description.should == '(no description)'
|
49
|
-
bar.locale.should == 'en'
|
50
|
-
bar.birth_year.should == 30.years.ago.year
|
51
|
-
end
|
52
|
-
|
53
|
-
it 'should allow mass-assignment' do
|
54
|
-
baz.description.should == 'Please set ...'
|
55
|
-
baz.locale.should == 'en-US'
|
56
|
-
baz.age.should == 18
|
57
|
-
|
58
|
-
persisted = Baz.first
|
59
|
-
persisted.age.should be_nil
|
60
|
-
end
|
61
|
-
|
62
|
-
it 'should allow Hashes as default values' do
|
63
|
-
bar.some_hash.should == {}
|
64
|
-
end
|
65
|
-
|
66
|
-
it 'should ensure default values are duplicated' do
|
67
|
-
bar.some_hash[:a] = 'A'
|
68
|
-
bar.some_hash.should == {:a => 'A'}
|
69
|
-
Bar.new.some_hash.should == {}
|
70
|
-
end
|
71
|
-
|
72
|
-
it 'should handle missing attributes (e.g. in case of Base#exists?)' do
|
73
|
-
lambda { Foo.exists? }.should_not raise_error
|
74
|
-
lambda { Foo.select(:locale).first }.should_not raise_error
|
75
|
-
end
|
76
|
-
|
77
|
-
|
78
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
$: << File.dirname(__FILE__) + '/../lib'
|
2
|
-
require 'rubygems'
|
3
|
-
require 'bundler'
|
4
|
-
Bundler.setup
|
5
|
-
Bundler.require :default, :test
|
6
|
-
|
7
|
-
require 'active_support'
|
8
|
-
require 'active_record'
|
9
|
-
require 'rspec'
|
10
|
-
require 'fileutils'
|
11
|
-
require 'attribute_defaults'
|
12
|
-
|
13
|
-
SPEC_DATABASE = File.dirname(__FILE__) + '/tmp/test.sqlite3'
|
14
|
-
|
15
|
-
RSpec.configure do |c|
|
16
|
-
|
17
|
-
c.before do
|
18
|
-
FileUtils.mkdir_p File.dirname(SPEC_DATABASE)
|
19
|
-
base = ActiveRecord::Base
|
20
|
-
base.establish_connection('adapter' => 'sqlite3', 'database' => SPEC_DATABASE)
|
21
|
-
base.connection.create_table :foos do |t|
|
22
|
-
t.string :name
|
23
|
-
t.integer :age
|
24
|
-
t.string :locale
|
25
|
-
t.string :description
|
26
|
-
t.timestamps
|
27
|
-
end
|
28
|
-
Foo.create!(:name => 'Bogus') {|f| f.description = nil; f.locale = nil }
|
29
|
-
end
|
30
|
-
|
31
|
-
c.after do
|
32
|
-
FileUtils.rm_f(SPEC_DATABASE)
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
class Foo < ActiveRecord::Base
|
38
|
-
attr_accessible :name, :age, :locale
|
39
|
-
attr_accessor :birth_year
|
40
|
-
|
41
|
-
attr_default :description, "(no description)", :if => :blank?
|
42
|
-
attr_default :locale, "en", :persisted => false
|
43
|
-
attr_default :birth_year do |f|
|
44
|
-
f.age ? Time.now.year - f.age : nil
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
class Bar < Foo
|
49
|
-
attr_accessor :some_hash, :some_arr
|
50
|
-
attr_accessible :some_arr
|
51
|
-
|
52
|
-
attr_default :some_hash, :default => {}
|
53
|
-
attr_default :some_arr, :default => [1, 2, 3], :if => :blank?
|
54
|
-
end
|
55
|
-
|
56
|
-
class Baz < ActiveRecord::Base
|
57
|
-
set_table_name 'foos'
|
58
|
-
attr_defaults :description => "Please set ...", :age => { :default => 18, :persisted => false }, :locale => lambda { 'en-US' }
|
59
|
-
end
|