defaults 0.1.0

This diff has not been reviewed by any users.
Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gem "activerecord", "3.0.0"
4
+ gem "sqlite3-ruby"
5
+ gem "rspec", "2.0.0.beta.22"
@@ -0,0 +1,54 @@
1
+ = Defaults
2
+
3
+ == Instalation
4
+
5
+ Install the plugin with <tt>script/plugin install git://github.com/fnando/has_defaults.git</tt>
6
+
7
+ == Usage
8
+
9
+ Add the method call +has_defaults+ to your model.
10
+
11
+ class Page < ActiveRecord::Base
12
+ defaults :title => "New page", :body => "Put your text here"
13
+ end
14
+
15
+ Attributes will be set only if it's a new record and the attribute is blank.
16
+
17
+ Retrieve the default attribute with the +default_for+ instance method:
18
+
19
+ @page.default_for(:title)
20
+
21
+ You can pass Proc as attribute:
22
+
23
+ defaults :expires_at => proc { Time.now }
24
+
25
+ You can override the default attributes as follow:
26
+
27
+ Page.default_options = {:title => "Here's your new page", :body => "Write your page text"}
28
+
29
+ == Maintainer
30
+
31
+ * Nando Vieira - http://simplesideias.com.br
32
+
33
+ == License
34
+
35
+ (The MIT License)
36
+
37
+ Permission is hereby granted, free of charge, to any person obtaining
38
+ a copy of this software and associated documentation files (the
39
+ 'Software'), to deal in the Software without restriction, including
40
+ without limitation the rights to use, copy, modify, merge, publish,
41
+ distribute, sublicense, and/or sell copies of the Software, and to
42
+ permit persons to whom the Software is furnished to do so, subject to
43
+ the following conditions:
44
+
45
+ The above copyright notice and this permission notice shall be
46
+ included in all copies or substantial portions of the Software.
47
+
48
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
49
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
50
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
51
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
52
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
53
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
54
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,28 @@
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"
10
+
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
@@ -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{defaults}
8
+ s.version = "0.1.0"
9
+
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"]
32
+ 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
+
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
56
+ end
57
+
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "defaults"
@@ -0,0 +1,52 @@
1
+ module Defaults
2
+ autoload :Version, "defaults/version"
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+
7
+ class << base
8
+ attr_accessor :default_options
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ # defaults :title => "Add your title here"
14
+ def defaults(attrs)
15
+ raise ArgumentError, "Hash expected; #{attrs.class} given." unless attrs.kind_of?(Hash)
16
+
17
+ include InstanceMethods
18
+
19
+ self.default_options = attrs
20
+ after_initialize :set_default_attributes
21
+ end
22
+
23
+ def has_defaults(attrs)
24
+ warn "[WARNING] Using has_defaults is now deprecated. Please use defaults instead."
25
+ defaults(attrs)
26
+ end
27
+ end
28
+
29
+ module InstanceMethods
30
+ def default_for(name)
31
+ self.class.default_options[name.to_sym]
32
+ end
33
+
34
+ private
35
+ def set_default_attributes
36
+ 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?
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ # ActiveRecord only calls after_initialize callbacks only if is
47
+ # explicit defined in a class.
48
+ if ActiveRecord::VERSION::STRING < "3.0"
49
+ class ActiveRecord::Base; def after_initialize; end; end
50
+ end
51
+
52
+ ActiveRecord::Base.send(:include, Defaults)
@@ -0,0 +1,8 @@
1
+ module Defaults
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 0
6
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
+ end
8
+ end
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+
4
+ class Donut < ActiveRecord::Base
5
+ attr_accessor :class_name
6
+
7
+ defaults :flavor => "cream",
8
+ :name => "Cream",
9
+ :maker => proc { "Dunkin Donuts" },
10
+ :class_name => proc {|donut| donut.class.name }
11
+ end
12
+
13
+ describe Defaults do
14
+ before do
15
+ @donut = create_donut
16
+ @new_donut = Donut.new
17
+ end
18
+
19
+ it "should set defaults" do
20
+ @new_donut.flavor.should == "cream"
21
+ @new_donut.name.should == "Cream"
22
+ @new_donut.maker.should == "Dunkin Donuts"
23
+ @new_donut.class_name.should == "Donut"
24
+ end
25
+
26
+ it "should set defaults only if attributes are blank" do
27
+ @donut.flavor.should == "vanilla"
28
+ end
29
+
30
+ it "should return default value for an attribute" do
31
+ @donut.default_for(:flavor).should == "cream"
32
+ end
33
+
34
+ it "should not set defaults" do
35
+ Donut.first.flavor.should == "vanilla"
36
+ end
37
+
38
+ private
39
+ def create_donut(options={})
40
+ Donut.create({
41
+ :flavor => "vanilla",
42
+ :name => "Vanilla Sky",
43
+ :maker => "Mr. Baker"
44
+ }.merge(options))
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :donuts do |t|
3
+ t.string :flavor, :name, :maker
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+ require "active_record"
3
+ require "rspec"
4
+ require "defaults"
5
+
6
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
7
+ load File.dirname(__FILE__) + "/schema.rb"
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: defaults
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Nando Vieira
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-29 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 2
30
+ - 0
31
+ - 0
32
+ version: 2.0.0
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: activerecord
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ description: Set default values for ActiveRecord attributes
49
+ email: fnando.vieira@gmail.com
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ extra_rdoc_files:
55
+ - README.rdoc
56
+ files:
57
+ - Gemfile
58
+ - README.rdoc
59
+ - Rakefile
60
+ - defaults.gemspec
61
+ - init.rb
62
+ - lib/defaults.rb
63
+ - lib/defaults/version.rb
64
+ - spec/defaults_spec.rb
65
+ - spec/schema.rb
66
+ - spec/spec_helper.rb
67
+ has_rdoc: true
68
+ homepage: http://github.com/fnando/has_defaults
69
+ licenses: []
70
+
71
+ post_install_message:
72
+ rdoc_options:
73
+ - --charset=UTF-8
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ requirements: []
93
+
94
+ rubyforge_project:
95
+ rubygems_version: 1.3.7
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Set default values for ActiveRecord attributes
99
+ test_files:
100
+ - spec/defaults_spec.rb
101
+ - spec/schema.rb
102
+ - spec/spec_helper.rb