defaults 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fc734e69238d4fda99df26e36a733e7de84045f0
4
- data.tar.gz: 533d486e9367b67eb614bdd128c9ca078a0c5c60
3
+ metadata.gz: fdb962c44035215d7f9a294c3b93800900435a90
4
+ data.tar.gz: 1d295f223bc4589cd84ceee8ddf5ca9cb0c3840e
5
5
  SHA512:
6
- metadata.gz: 7ffda37edf3c7f3524b5176bc5dea771fc417032e6fd5a68b4758fdcef5adb290b172ff16d7dd4a18847025c143e09353b0b597fb624c4918e904248ad5c0bdd
7
- data.tar.gz: ca9666b46363045ab7f43232ea3aeea29547b0f2624771e05a069d34eb0b0c8ca0256bcdc61586e00f229178efa850efac092ec96e09b74868e54ce0e036bc16
6
+ metadata.gz: fbbfbfd01d488914ec07f17fcd2728c6aa0eae45d3777fdeee2aac29e765b3c6db630374d4d053f88269d57af30505e7c628869655b8596147b46a011af5ca09
7
+ data.tar.gz: cb4819922cc36be60ce1a352317af35a7fac8a5adbca2aaf24fb54eb4b003ffee1ab66b563f1b9a9eb294c64c84470c55fb731a9b3d01e69b7d275810ea879d3
@@ -1,5 +1,5 @@
1
1
  language: ruby
2
- script: bundle exec rspec
2
+ script: bundle exec rake
3
3
  sudo: false
4
4
  cache: bundler
5
5
  addons:
@@ -7,6 +7,9 @@ addons:
7
7
  repo_token:
8
8
  secure: "aViiP4wSO11Sq/7aXMeoo4ksDj3/wPs3do+cP79jSFiF/ZyBnETWOG4BQoO+UWT1IctnlEN/jrAceZZ9VTbxrtkPbJriO4X8AZDweOq4TOvrRa+6jPfuAqh5QZeSbXucrTU54DWCyPDu404bIz+9yh1vFMBtstKQF1mx4MJVXY4="
9
9
  rvm:
10
- - '2.0'
11
- - '2.1'
12
- - '2.2'
10
+ - '2.0'
11
+ - '2.1'
12
+ - '2.2'
13
+ - '2.3.0'
14
+ notifications:
15
+ email: false
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
  gemspec
data/README.md CHANGED
@@ -10,6 +10,11 @@ Just run `gem install defaults`.
10
10
 
11
11
  ## Usage
12
12
 
13
+ Here are the rules:
14
+
15
+ 1. Assigned attributes have precedence over default values
16
+ 2. Default values have precedence over database defaults
17
+
13
18
  Add the method call `defaults` to your model.
14
19
 
15
20
  ```ruby
@@ -40,7 +45,7 @@ Page.default_options = {title: "Here's your new page", body: "Write your page te
40
45
 
41
46
  ## Maintainer
42
47
 
43
- * Nando Vieira - http://simplesideias.com.br
48
+ * Nando Vieira - http://nandovieira.com
44
49
 
45
50
  ## License
46
51
 
data/Rakefile CHANGED
@@ -1,5 +1,10 @@
1
- require "bundler"
2
- Bundler::GemHelper.install_tasks
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
3
 
4
- require "rspec/core/rake_task"
5
- RSpec::Core::RakeTask.new
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList["test/**/*_test.rb"]
7
+ t.warning = false
8
+ end
9
+
10
+ task default: :test
@@ -9,14 +9,17 @@ Gem::Specification.new do |s|
9
9
  s.homepage = "http://rubygems.org/gems/defaults"
10
10
  s.summary = "Set default values for ActiveRecord attributes"
11
11
  s.description = s.summary
12
+ s.licenses = ["MIT"]
12
13
 
13
14
  s.files = `git ls-files`.split("\n")
14
15
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map {|f| File.basename(f) }
16
17
  s.require_paths = ["lib"]
17
18
 
18
19
  s.add_development_dependency "activerecord", ">= 3.0.0"
19
20
  s.add_development_dependency "sqlite3-ruby"
20
- s.add_development_dependency "rspec"
21
+ s.add_development_dependency "rake"
22
+ s.add_development_dependency "minitest-utils"
21
23
  s.add_development_dependency "codeclimate-test-reporter"
24
+ s.add_development_dependency "pry-meta"
22
25
  end
@@ -1,5 +1,5 @@
1
1
  module Defaults
2
- autoload :Version, "defaults/version"
2
+ require "defaults/version"
3
3
 
4
4
  def self.included(base)
5
5
  base.extend ClassMethods
@@ -25,23 +25,26 @@ module Defaults
25
25
 
26
26
  module InstanceMethods
27
27
  def default_for(name)
28
- value = self.class.default_options[name.to_sym]
29
- value = value.arity == 1 ? value.call(self) : value.call if value.respond_to?(:call)
28
+ if self.class.default_options.key?(name.to_sym)
29
+ value = self.class.default_options[name.to_sym]
30
+ value = value.arity == 1 ? value.call(self) : value.call if value.respond_to?(:call)
31
+ else
32
+ column_info = self.class.columns_hash[name.to_s]
33
+ value = column_info.type_cast_from_user(column_info.default) if column_info
34
+ end
35
+
30
36
  value
31
37
  end
32
38
 
33
39
  private
34
40
  def set_default_attributes
35
- if new_record?
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
44
- end
41
+ return unless new_record?
42
+
43
+ self.class.default_options.keys.each do |name|
44
+ value = read_attribute(name) if changes[name]
45
+ value = default_for(name) if value.blank?
46
+
47
+ __send__ "#{name}=", value
45
48
  end
46
49
  end
47
50
  end
@@ -2,7 +2,7 @@ module Defaults
2
2
  module Version
3
3
  MAJOR = 1
4
4
  MINOR = 0
5
- PATCH = 0
5
+ PATCH = 1
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
@@ -0,0 +1,69 @@
1
+ require "test_helper"
2
+
3
+ class Donut < ActiveRecord::Base
4
+ attr_accessor :class_name
5
+
6
+ defaults flavor: "cream",
7
+ name: "Cream",
8
+ maker: proc { "Dunkin Donuts" },
9
+ class_name: -> donut { donut.class.name }
10
+
11
+ defaults quantity: 5
12
+ defaults available: false
13
+ end
14
+
15
+ class DefaultsTest < Minitest::Test
16
+ let(:donut) { create_donut }
17
+ let(:new_donut) { Donut.new }
18
+
19
+ setup do
20
+ Donut.delete_all
21
+ end
22
+
23
+ test "considers detabase defaults with type casting" do
24
+ assert_equal 0.00, new_donut.default_for(:price)
25
+ end
26
+
27
+ test "has precedence over database defaults" do
28
+ refute new_donut.available?
29
+ end
30
+
31
+ test "raises when invalid argument is provided" do
32
+ assert_raises(ArgumentError) { Donut.defaults nil }
33
+ end
34
+
35
+ test "sets default values" do
36
+ assert_equal "cream", new_donut.flavor
37
+ assert_equal "Cream", new_donut.name
38
+ assert_equal "Dunkin Donuts", new_donut.maker
39
+ assert_equal "Donut", new_donut.class_name
40
+ assert_equal 5, new_donut.quantity
41
+ end
42
+
43
+ test "sets default values only when have blank attributes" do
44
+ assert_equal "vanilla", donut.flavor
45
+ assert_equal 5, donut.quantity
46
+ end
47
+
48
+ test "returns default value for an attribute" do
49
+ assert_equal "cream", donut.default_for(:flavor)
50
+ assert_equal "Dunkin Donuts", donut.default_for(:maker)
51
+ assert_equal "Donut", donut.default_for(:class_name)
52
+ assert_equal 5, donut.default_for(:quantity)
53
+ end
54
+
55
+ test "does not set default values" do
56
+ assert_equal donut, Donut.first
57
+ assert_equal "vanilla", Donut.first.flavor
58
+ end
59
+
60
+ private
61
+
62
+ def create_donut(options = {})
63
+ Donut.create({
64
+ flavor: "vanilla",
65
+ name: "Vanilla Sky",
66
+ maker: "Mr. Baker"
67
+ }.merge(options))
68
+ end
69
+ end
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :donuts do |t|
3
+ t.string :flavor, :name, :maker
4
+ t.integer :quantity, null: false
5
+ t.float :price, default: 0.00, null: false
6
+ t.boolean :available, default: true, null: false
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
3
+
4
+ require "bundler/setup"
5
+ require "active_record"
6
+ require "defaults"
7
+
8
+ require "minitest/utils"
9
+ require "minitest/autorun"
10
+
11
+ ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
12
+ load File.dirname(__FILE__) + "/schema.rb"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: defaults
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-19 00:00:00.000000000 Z
11
+ date: 2016-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -39,7 +39,21 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rspec
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-utils
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
59
  - - ">="
@@ -66,6 +80,20 @@ dependencies:
66
80
  - - ">="
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-meta
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
69
97
  description: Set default values for ActiveRecord attributes
70
98
  email:
71
99
  - fnando.vieira@gmail.com
@@ -74,7 +102,6 @@ extensions: []
74
102
  extra_rdoc_files: []
75
103
  files:
76
104
  - ".gitignore"
77
- - ".rspec"
78
105
  - ".travis.yml"
79
106
  - Gemfile
80
107
  - README.md
@@ -82,11 +109,12 @@ files:
82
109
  - defaults.gemspec
83
110
  - lib/defaults.rb
84
111
  - lib/defaults/version.rb
85
- - spec/defaults_spec.rb
86
- - spec/schema.rb
87
- - spec/spec_helper.rb
112
+ - test/defaults/defaults_test.rb
113
+ - test/schema.rb
114
+ - test/test_helper.rb
88
115
  homepage: http://rubygems.org/gems/defaults
89
- licenses: []
116
+ licenses:
117
+ - MIT
90
118
  metadata: {}
91
119
  post_install_message:
92
120
  rdoc_options: []
@@ -104,11 +132,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
132
  version: '0'
105
133
  requirements: []
106
134
  rubyforge_project:
107
- rubygems_version: 2.4.6
135
+ rubygems_version: 2.5.1
108
136
  signing_key:
109
137
  specification_version: 4
110
138
  summary: Set default values for ActiveRecord attributes
111
139
  test_files:
112
- - spec/defaults_spec.rb
113
- - spec/schema.rb
114
- - spec/spec_helper.rb
140
+ - test/defaults/defaults_test.rb
141
+ - test/schema.rb
142
+ - test/test_helper.rb
data/.rspec DELETED
@@ -1 +0,0 @@
1
- --color
@@ -1,60 +0,0 @@
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 => lambda {|donut| donut.class.name }
11
-
12
- defaults :quantity => 5
13
- end
14
-
15
- describe Defaults do
16
- before do
17
- Donut.delete_all
18
- @donut = create_donut
19
- @new_donut = Donut.new
20
- end
21
-
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
29
- expect(@new_donut.flavor).to eq("cream")
30
- expect(@new_donut.name).to eq("Cream")
31
- expect(@new_donut.maker).to eq("Dunkin Donuts")
32
- expect(@new_donut.class_name).to eq("Donut")
33
- expect(@new_donut.quantity).to eq(5)
34
- end
35
-
36
- it "sets default values only when have blank attributes" do
37
- expect(@donut.flavor).to eq("vanilla")
38
- expect(@donut.quantity).to eq(5)
39
- end
40
-
41
- it "returns default value for an attribute" do
42
- expect(@donut.default_for(:flavor)).to eq("cream")
43
- expect(@donut.default_for(:maker)).to eq("Dunkin Donuts")
44
- expect(@donut.default_for(:class_name)).to eq("Donut")
45
- expect(@donut.default_for(:quantity)).to eq(5)
46
- end
47
-
48
- it "does not set default values" do
49
- expect(Donut.first.flavor).to eq("vanilla")
50
- end
51
-
52
- private
53
- def create_donut(options={})
54
- Donut.create({
55
- :flavor => "vanilla",
56
- :name => "Vanilla Sky",
57
- :maker => "Mr. Baker"
58
- }.merge(options))
59
- end
60
- end
@@ -1,6 +0,0 @@
1
- ActiveRecord::Schema.define(:version => 0) do
2
- create_table :donuts do |t|
3
- t.string :flavor, :name, :maker
4
- t.integer :quantity, :null => false
5
- end
6
- end
@@ -1,9 +0,0 @@
1
- require 'codeclimate-test-reporter'
2
- CodeClimate::TestReporter.start
3
-
4
- require 'bundler/setup'
5
- require 'active_record'
6
- require 'defaults'
7
-
8
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
9
- load File.dirname(__FILE__) + "/schema.rb"