attribute-defaults 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  spec/tmp
2
+ pkg
@@ -35,12 +35,14 @@ It doesn't override values that are already set ...
35
35
  end
36
36
  Foo.new(:age => 25) # => age: 25
37
37
 
38
- ... but allows you to force overrides when e.g. blank ...
38
+ ... but allows you to set your own conditions e.g. blank? ...
39
39
 
40
40
  class Foo < ActiveRecord::Base
41
- attr_defaults :name => { :default => 'MUST HAVE', :force => :blank }
41
+ attr_defaults :numbers => { :default => [1], :if => :blank? }
42
42
  end
43
- Foo.new(:name => '') # => name: 'MUST HAVE'
43
+ Foo.new(:numbers => nil) # => numbers: [1]
44
+ Foo.new(:numbers => []) # => numbers: [1]
45
+ Foo.new(:numbers => [2]) # => numbers: [2]
44
46
 
45
47
  ... and it respects protected attributes ...
46
48
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -0,0 +1,57 @@
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
+
@@ -28,7 +28,7 @@ module ActiveRecord
28
28
  module_eval(<<-EVAL, __FILE__, __LINE__ + 1)
29
29
  def #{setter}
30
30
  #{'return if persisted?' if options[:persisted] == false}
31
- return unless self.#{sym}.send(#{options[:force] == :blank ? ':blank?' : ':nil?'})
31
+ return unless self.#{sym}.send(:#{options[:if] || 'nil?'})
32
32
  value = #{evaluator}(self)
33
33
  self.#{sym} = value.duplicable? ? value.dup : value
34
34
  rescue ActiveModel::MissingAttributeError
@@ -20,10 +20,14 @@ describe ActiveRecord::AttributesWithDefaults do
20
20
  foo.birth_year.should == 30.years.ago.year
21
21
  end
22
22
 
23
- it 'should allow to foce overrides on blank values' do
23
+ it 'should allow to set custom value conditions' do
24
24
  foo(:locale => '', :description => '')
25
25
  foo.locale.should == ''
26
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]
27
31
  end
28
32
 
29
33
  it 'should not override attributes that were set manually' do
@@ -38,7 +38,7 @@ class Foo < ActiveRecord::Base
38
38
  attr_accessible :name, :age, :locale
39
39
  attr_accessor :birth_year
40
40
 
41
- attr_default :description, "(no description)", :force => :blank
41
+ attr_default :description, "(no description)", :if => :blank?
42
42
  attr_default :locale, "en", :persisted => false
43
43
  attr_default :birth_year do |f|
44
44
  f.age ? Time.now.year - f.age : nil
@@ -46,8 +46,11 @@ class Foo < ActiveRecord::Base
46
46
  end
47
47
 
48
48
  class Bar < Foo
49
- attr_accessor :some_hash
49
+ attr_accessor :some_hash, :some_arr
50
+ attr_accessible :some_arr
51
+
50
52
  attr_default :some_hash, :default => {}
53
+ attr_default :some_arr, :default => [1, 2, 3], :if => :blank?
51
54
  end
52
55
 
53
56
  class Baz < ActiveRecord::Base
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: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dimitrij Denissenko
@@ -65,6 +65,7 @@ files:
65
65
  - README.rdoc
66
66
  - Rakefile
67
67
  - VERSION
68
+ - attribute-defaults.gemspec
68
69
  - lib/attribute-defaults.rb
69
70
  - lib/attribute_defaults.rb
70
71
  - spec/attribute_defaults_spec.rb