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 ADDED
@@ -0,0 +1,2 @@
1
+ pkg
2
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --format documentation --color
data/Gemfile CHANGED
@@ -1,5 +1,2 @@
1
1
  source :rubygems
2
-
3
- gem "activerecord", "3.0.0"
4
- gem "sqlite3-ruby"
5
- gem "rspec", "2.0.0.beta.22"
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,58 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ defaults (0.1.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ activemodel (3.0.5)
10
+ activesupport (= 3.0.5)
11
+ builder (~> 2.1.2)
12
+ i18n (~> 0.4)
13
+ activerecord (3.0.5)
14
+ activemodel (= 3.0.5)
15
+ activesupport (= 3.0.5)
16
+ arel (~> 2.0.2)
17
+ tzinfo (~> 0.3.23)
18
+ activesupport (3.0.5)
19
+ archive-tar-minitar (0.5.2)
20
+ arel (2.0.9)
21
+ builder (2.1.2)
22
+ columnize (0.3.2)
23
+ diff-lcs (1.1.2)
24
+ i18n (0.5.0)
25
+ linecache19 (0.5.12)
26
+ ruby_core_source (>= 0.1.4)
27
+ rspec (2.5.0)
28
+ rspec-core (~> 2.5.0)
29
+ rspec-expectations (~> 2.5.0)
30
+ rspec-mocks (~> 2.5.0)
31
+ rspec-core (2.5.1)
32
+ rspec-expectations (2.5.0)
33
+ diff-lcs (~> 1.1.2)
34
+ rspec-mocks (2.5.0)
35
+ ruby-debug-base19 (0.11.25)
36
+ columnize (>= 0.3.1)
37
+ linecache19 (>= 0.5.11)
38
+ ruby_core_source (>= 0.1.4)
39
+ ruby-debug19 (0.11.6)
40
+ columnize (>= 0.3.1)
41
+ linecache19 (>= 0.5.11)
42
+ ruby-debug-base19 (>= 0.11.19)
43
+ ruby_core_source (0.1.5)
44
+ archive-tar-minitar (>= 0.5.2)
45
+ sqlite3 (1.3.3)
46
+ sqlite3-ruby (1.3.3)
47
+ sqlite3 (>= 1.3.3)
48
+ tzinfo (0.3.26)
49
+
50
+ PLATFORMS
51
+ ruby
52
+
53
+ DEPENDENCIES
54
+ activerecord (~> 3.0.0)
55
+ defaults!
56
+ rspec (~> 2.5.0)
57
+ ruby-debug19
58
+ sqlite3-ruby
data/README.rdoc CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  == Instalation
4
4
 
5
- Install the plugin with <tt>script/plugin install git://github.com/fnando/has_defaults.git</tt>
5
+ Just run <tt>gem install defaults</tt>
6
6
 
7
7
  == Usage
8
8
 
9
- Add the method call +has_defaults+ to your model.
9
+ Add the method call +defaults+ to your model.
10
10
 
11
11
  class Page < ActiveRecord::Base
12
12
  defaults :title => "New page", :body => "Put your text here"
data/Rakefile CHANGED
@@ -1,28 +1,5 @@
1
- require "./lib/defaults/version"
2
- require "rspec/core/rake_task"
3
-
4
- RSpec::Core::RakeTask.new do |t|
5
- t.ruby_opts = %w[ -Ilib -Ispec ]
6
- end
7
-
8
- begin
9
- require "jeweler"
1
+ require "bundler"
2
+ Bundler::GemHelper.install_tasks
10
3
 
11
- JEWEL = Jeweler::Tasks.new do |gem|
12
- gem.name = "defaults"
13
- gem.version = Defaults::Version::STRING
14
- gem.summary = "Set default values for ActiveRecord attributes"
15
- gem.description = "Set default values for ActiveRecord attributes"
16
- gem.authors = ["Nando Vieira"]
17
- gem.email = "fnando.vieira@gmail.com"
18
- gem.homepage = "http://github.com/fnando/has_defaults"
19
- gem.has_rdoc = false
20
- gem.files = FileList["init.rb", "Gemfile", "Rakefile", "README.rdoc", "defaults.gemspec", "{lib,spec}/**/*"]
21
- gem.add_development_dependency "rspec", ">= 2.0.0"
22
- gem.add_dependency "activerecord"
23
- end
24
-
25
- Jeweler::GemcutterTasks.new
26
- rescue LoadError => e
27
- puts "You don't have Jeweler installed, so you won't be able to build gems."
28
- end
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new
data/defaults.gemspec CHANGED
@@ -1,57 +1,24 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
1
  # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "defaults/version"
5
4
 
6
5
  Gem::Specification.new do |s|
7
- s.name = %q{defaults}
8
- s.version = "0.1.0"
6
+ s.name = "defaults"
7
+ s.version = Defaults::Version::STRING
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Nando Vieira"]
10
+ s.email = ["fnando.vieira@gmail.com"]
11
+ s.homepage = "http://rubygems.org/gems/defaults"
12
+ s.summary = "Set default values for ActiveRecord attributes"
13
+ s.description = s.summary
9
14
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Nando Vieira"]
12
- s.date = %q{2010-09-29}
13
- s.description = %q{Set default values for ActiveRecord attributes}
14
- s.email = %q{fnando.vieira@gmail.com}
15
- s.extra_rdoc_files = [
16
- "README.rdoc"
17
- ]
18
- s.files = [
19
- "Gemfile",
20
- "README.rdoc",
21
- "Rakefile",
22
- "defaults.gemspec",
23
- "init.rb",
24
- "lib/defaults.rb",
25
- "lib/defaults/version.rb",
26
- "spec/defaults_spec.rb",
27
- "spec/schema.rb",
28
- "spec/spec_helper.rb"
29
- ]
30
- s.homepage = %q{http://github.com/fnando/has_defaults}
31
- s.rdoc_options = ["--charset=UTF-8"]
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
32
18
  s.require_paths = ["lib"]
33
- s.rubygems_version = %q{1.3.7}
34
- s.summary = %q{Set default values for ActiveRecord attributes}
35
- s.test_files = [
36
- "spec/defaults_spec.rb",
37
- "spec/schema.rb",
38
- "spec/spec_helper.rb"
39
- ]
40
19
 
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_development_dependency(%q<rspec>, [">= 2.0.0"])
47
- s.add_runtime_dependency(%q<activerecord>, [">= 0"])
48
- else
49
- s.add_dependency(%q<rspec>, [">= 2.0.0"])
50
- s.add_dependency(%q<activerecord>, [">= 0"])
51
- end
52
- else
53
- s.add_dependency(%q<rspec>, [">= 2.0.0"])
54
- s.add_dependency(%q<activerecord>, [">= 0"])
55
- end
20
+ s.add_development_dependency "activerecord", "~> 3.0.0"
21
+ s.add_development_dependency "sqlite3-ruby"
22
+ s.add_development_dependency "rspec", "~> 2.5.0"
23
+ s.add_development_dependency "ruby-debug19" if RUBY_VERSION >= "1.9"
56
24
  end
57
-
data/lib/defaults.rb CHANGED
@@ -16,27 +16,31 @@ module Defaults
16
16
 
17
17
  include InstanceMethods
18
18
 
19
- self.default_options = attrs
20
- after_initialize :set_default_attributes
21
- end
19
+ self.default_options ||= {}
20
+ self.default_options.merge!(attrs)
22
21
 
23
- def has_defaults(attrs)
24
- warn "[WARNING] Using has_defaults is now deprecated. Please use defaults instead."
25
- defaults(attrs)
22
+ after_initialize :set_default_attributes
26
23
  end
27
24
  end
28
25
 
29
26
  module InstanceMethods
30
27
  def default_for(name)
31
- self.class.default_options[name.to_sym]
28
+ value = self.class.default_options[name.to_sym]
29
+ value = value.arity == 1 ? value.call(self) : value.call if value.respond_to?(:call)
30
+ value
32
31
  end
33
32
 
34
33
  private
35
34
  def set_default_attributes
36
35
  if new_record?
37
- self.class.default_options.each do |name, value|
38
- value = value.arity == 1 ? value.call(self) : value.call if value.respond_to?(:call)
39
- send("#{name}=", value) if send(name).blank?
36
+ self.class.default_options.keys.each do |name|
37
+ # First, retrieve default value set through database.
38
+ # Will use this value in order to detect if value should be set.
39
+ info = self.class.columns_hash[name.to_s]
40
+ database_default = info ? info.default : nil
41
+
42
+ value = read_attribute(name)
43
+ __send__ "#{name}=", default_for(name) if value.blank? || value == database_default
40
44
  end
41
45
  end
42
46
  end
@@ -2,7 +2,7 @@ module Defaults
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- PATCH = 0
5
+ PATCH = 1
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
@@ -7,31 +7,45 @@ class Donut < ActiveRecord::Base
7
7
  defaults :flavor => "cream",
8
8
  :name => "Cream",
9
9
  :maker => proc { "Dunkin Donuts" },
10
- :class_name => proc {|donut| donut.class.name }
10
+ :class_name => lambda {|donut| donut.class.name }
11
+
12
+ defaults :quantity => 5
11
13
  end
12
14
 
13
15
  describe Defaults do
14
16
  before do
17
+ Donut.delete_all
15
18
  @donut = create_donut
16
19
  @new_donut = Donut.new
17
20
  end
18
21
 
19
- it "should set defaults" do
22
+ it "raises when invalid argument is provided" do
23
+ expect {
24
+ Donut.defaults nil
25
+ }.to raise_error(ArgumentError)
26
+ end
27
+
28
+ it "sets default values" do
20
29
  @new_donut.flavor.should == "cream"
21
30
  @new_donut.name.should == "Cream"
22
31
  @new_donut.maker.should == "Dunkin Donuts"
23
32
  @new_donut.class_name.should == "Donut"
33
+ @new_donut.quantity.should == 5
24
34
  end
25
35
 
26
- it "should set defaults only if attributes are blank" do
36
+ it "sets default values only when have blank attributes" do
27
37
  @donut.flavor.should == "vanilla"
38
+ @donut.quantity.should == 5
28
39
  end
29
40
 
30
- it "should return default value for an attribute" do
41
+ it "returns default value for an attribute" do
31
42
  @donut.default_for(:flavor).should == "cream"
43
+ @donut.default_for(:maker).should == "Dunkin Donuts"
44
+ @donut.default_for(:class_name).should == "Donut"
45
+ @donut.default_for(:quantity).should == 5
32
46
  end
33
47
 
34
- it "should not set defaults" do
48
+ it "does not set default values" do
35
49
  Donut.first.flavor.should == "vanilla"
36
50
  end
37
51
 
data/spec/schema.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  ActiveRecord::Schema.define(:version => 0) do
2
2
  create_table :donuts do |t|
3
3
  t.string :flavor, :name, :maker
4
+ t.integer :quantity, :default => 0, :null => false
4
5
  end
5
6
  end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
- require "active_record"
3
2
  require "rspec"
3
+ require "active_record"
4
4
  require "defaults"
5
5
 
6
6
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: defaults
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 0
9
- version: 0.1.0
4
+ prerelease:
5
+ version: 0.1.1
10
6
  platform: ruby
11
7
  authors:
12
8
  - Nando Vieira
@@ -14,63 +10,82 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2010-09-29 00:00:00 -03:00
13
+ date: 2011-04-04 00:00:00 -03:00
18
14
  default_executable:
19
15
  dependencies:
20
16
  - !ruby/object:Gem::Dependency
21
- name: rspec
17
+ name: activerecord
22
18
  prerelease: false
23
19
  requirement: &id001 !ruby/object:Gem::Requirement
24
20
  none: false
25
21
  requirements:
26
- - - ">="
22
+ - - ~>
27
23
  - !ruby/object:Gem::Version
28
- segments:
29
- - 2
30
- - 0
31
- - 0
32
- version: 2.0.0
24
+ version: 3.0.0
33
25
  type: :development
34
26
  version_requirements: *id001
35
27
  - !ruby/object:Gem::Dependency
36
- name: activerecord
28
+ name: sqlite3-ruby
37
29
  prerelease: false
38
30
  requirement: &id002 !ruby/object:Gem::Requirement
39
31
  none: false
40
32
  requirements:
41
33
  - - ">="
42
34
  - !ruby/object:Gem::Version
43
- segments:
44
- - 0
45
35
  version: "0"
46
- type: :runtime
36
+ type: :development
47
37
  version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: rspec
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 2.5.0
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: ruby-debug19
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ type: :development
59
+ version_requirements: *id004
48
60
  description: Set default values for ActiveRecord attributes
49
- email: fnando.vieira@gmail.com
61
+ email:
62
+ - fnando.vieira@gmail.com
50
63
  executables: []
51
64
 
52
65
  extensions: []
53
66
 
54
- extra_rdoc_files:
55
- - README.rdoc
67
+ extra_rdoc_files: []
68
+
56
69
  files:
70
+ - .gitignore
71
+ - .rspec
57
72
  - Gemfile
73
+ - Gemfile.lock
58
74
  - README.rdoc
59
75
  - Rakefile
60
76
  - defaults.gemspec
61
- - init.rb
62
77
  - lib/defaults.rb
63
78
  - lib/defaults/version.rb
64
79
  - spec/defaults_spec.rb
65
80
  - spec/schema.rb
66
81
  - spec/spec_helper.rb
67
82
  has_rdoc: true
68
- homepage: http://github.com/fnando/has_defaults
83
+ homepage: http://rubygems.org/gems/defaults
69
84
  licenses: []
70
85
 
71
86
  post_install_message:
72
- rdoc_options:
73
- - --charset=UTF-8
87
+ rdoc_options: []
88
+
74
89
  require_paths:
75
90
  - lib
76
91
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -78,21 +93,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
78
93
  requirements:
79
94
  - - ">="
80
95
  - !ruby/object:Gem::Version
81
- segments:
82
- - 0
83
96
  version: "0"
84
97
  required_rubygems_version: !ruby/object:Gem::Requirement
85
98
  none: false
86
99
  requirements:
87
100
  - - ">="
88
101
  - !ruby/object:Gem::Version
89
- segments:
90
- - 0
91
102
  version: "0"
92
103
  requirements: []
93
104
 
94
105
  rubyforge_project:
95
- rubygems_version: 1.3.7
106
+ rubygems_version: 1.6.0
96
107
  signing_key:
97
108
  specification_version: 3
98
109
  summary: Set default values for ActiveRecord attributes
data/init.rb DELETED
@@ -1 +0,0 @@
1
- require "defaults"