defaults 1.0.1 → 2.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: fdb962c44035215d7f9a294c3b93800900435a90
4
- data.tar.gz: 1d295f223bc4589cd84ceee8ddf5ca9cb0c3840e
2
+ SHA256:
3
+ metadata.gz: 99a898d5a8944837f464b37a61488f898031b5153d2a83ce2f84565ba982ea14
4
+ data.tar.gz: 77b4e74688d080bbf227b3ce43923c0389f89bf002e88ffcd97a6444a91ac771
5
5
  SHA512:
6
- metadata.gz: fbbfbfd01d488914ec07f17fcd2728c6aa0eae45d3777fdeee2aac29e765b3c6db630374d4d053f88269d57af30505e7c628869655b8596147b46a011af5ca09
7
- data.tar.gz: cb4819922cc36be60ce1a352317af35a7fac8a5adbca2aaf24fb54eb4b003ffee1ab66b563f1b9a9eb294c64c84470c55fb731a9b3d01e69b7d275810ea879d3
6
+ metadata.gz: fa5ffb6d5f0a70d54124438388a7833980ea0fe7c53928727d4f70f23b771bdec8bb0a1f01f0fd827c25f8c3ede0fab7b9259eb5c358d8dec0234a3f235da490
7
+ data.tar.gz: adac381ed1d12aa7e678e63607875c0a6512b21533e8b91c363a06476e6b7a4d076263474ea5a98c3d5df8632e7eb84ecc1ccf000dfb53c26f5c10938cb10d0e
data/.gitignore CHANGED
@@ -1,3 +1,3 @@
1
1
  pkg
2
2
  .DS_Store
3
- *.lock
3
+ /coverage
@@ -2,14 +2,23 @@ language: ruby
2
2
  script: bundle exec rake
3
3
  sudo: false
4
4
  cache: bundler
5
- addons:
6
- code_climate:
7
- repo_token:
8
- secure: "aViiP4wSO11Sq/7aXMeoo4ksDj3/wPs3do+cP79jSFiF/ZyBnETWOG4BQoO+UWT1IctnlEN/jrAceZZ9VTbxrtkPbJriO4X8AZDweOq4TOvrRa+6jPfuAqh5QZeSbXucrTU54DWCyPDu404bIz+9yh1vFMBtstKQF1mx4MJVXY4="
9
5
  rvm:
10
- - '2.0'
11
- - '2.1'
12
- - '2.2'
13
- - '2.3.0'
6
+ - 2.5.2
7
+ gemfile:
8
+ - Gemfile
9
+ - gemfiles/5_2.gemfile
10
+ - gemfiles/5_1.gemfile
11
+ - gemfiles/5_0.gemfile
14
12
  notifications:
15
13
  email: false
14
+ before_install:
15
+ - gem install bundler
16
+ before_script:
17
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
18
+ - chmod +x ./cc-test-reporter
19
+ - "./cc-test-reporter before-build"
20
+ after_script:
21
+ - "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
22
+ env:
23
+ global:
24
+ secure: G5s7bzgQzX9fcD+Wj9pNq3ePjnFxOeu7Q39q3ZX6KfJctD0VIrKR+tHyXpr08GDjB4sbQjmEAyVVypmn2UeIGd5Uzi72euM4G5h09CssD/zgTROojihMj8EvKLRj2oBVk40zCyPNdl31TGMFI6auSxNzM4VI1f4f1BV6ejgAPt4=
@@ -0,0 +1,72 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ defaults (2.0.0)
5
+ activerecord (>= 5.0.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.2)
11
+ activesupport (= 5.2.2)
12
+ activerecord (5.2.2)
13
+ activemodel (= 5.2.2)
14
+ activesupport (= 5.2.2)
15
+ arel (>= 9.0)
16
+ activesupport (5.2.2)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ arel (9.0.0)
22
+ awesome_print (1.8.0)
23
+ byebug (10.0.2)
24
+ coderay (1.1.2)
25
+ concurrent-ruby (1.1.4)
26
+ docile (1.1.5)
27
+ i18n (1.3.0)
28
+ concurrent-ruby (~> 1.0)
29
+ json (2.1.0)
30
+ method_source (0.9.2)
31
+ minitest (5.11.3)
32
+ minitest-utils (0.4.4)
33
+ minitest
34
+ pry (0.12.2)
35
+ coderay (~> 1.1.0)
36
+ method_source (~> 0.9.0)
37
+ pry-byebug (3.6.0)
38
+ byebug (~> 10.0)
39
+ pry (~> 0.10)
40
+ pry-meta (0.0.10)
41
+ awesome_print
42
+ pry
43
+ pry-byebug
44
+ pry-remote
45
+ pry-remote (0.1.8)
46
+ pry (~> 0.9)
47
+ slop (~> 3.0)
48
+ rake (12.3.2)
49
+ simplecov (0.13.0)
50
+ docile (~> 1.1.0)
51
+ json (>= 1.8, < 3)
52
+ simplecov-html (~> 0.10.0)
53
+ simplecov-html (0.10.2)
54
+ slop (3.6.0)
55
+ sqlite3 (1.3.13)
56
+ thread_safe (0.3.6)
57
+ tzinfo (1.2.5)
58
+ thread_safe (~> 0.1)
59
+
60
+ PLATFORMS
61
+ ruby
62
+
63
+ DEPENDENCIES
64
+ defaults!
65
+ minitest-utils
66
+ pry-meta
67
+ rake
68
+ simplecov
69
+ sqlite3
70
+
71
+ BUNDLED WITH
72
+ 1.17.3
data/README.md CHANGED
@@ -1,19 +1,34 @@
1
1
  # Defaults
2
2
 
3
- [![Build Status](https://travis-ci.org/fnando/defaults.svg)](https://travis-ci.org/fnando/defaults)
3
+ [![Travis-CI](https://travis-ci.org/fnando/defaults.svg)](https://travis-ci.org/fnando/defaults)
4
4
  [![Code Climate](https://codeclimate.com/github/fnando/defaults/badges/gpa.svg)](https://codeclimate.com/github/fnando/defaults)
5
- [![Test Coverage](https://codeclimate.com/github/fnando/defaults/badges/coverage.svg)](https://codeclimate.com/github/fnando/defaults)
5
+ [![Test Coverage](https://codeclimate.com/github/fnando/defaults/badges/coverage.svg)](https://codeclimate.com/github/fnando/defaults/coverage)
6
+ [![Gem](https://img.shields.io/gem/v/defaults.svg)](https://rubygems.org/gems/defaults)
7
+ [![Gem](https://img.shields.io/gem/dt/defaults.svg)](https://rubygems.org/gems/defaults)
6
8
 
7
9
  ## Instalation
8
10
 
9
- Just run `gem install defaults`.
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem "defaults"
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install defaults
24
+
10
25
 
11
26
  ## Usage
12
27
 
13
28
  Here are the rules:
14
29
 
15
30
  1. Assigned attributes have precedence over default values
16
- 2. Default values have precedence over database defaults
31
+ 2. Default values have precedence over database defaults.
17
32
 
18
33
  Add the method call `defaults` to your model.
19
34
 
@@ -31,21 +46,36 @@ Retrieve the default attribute with the `default_for` instance method:
31
46
  @page.default_for(:title)
32
47
  ```
33
48
 
34
- You can pass Proc as attribute:
49
+ You can pass callables (any objects that respond to `.call()` or `.call(record)`) as attribute:
35
50
 
36
51
  ```ruby
37
- defaults expires_at: proc { Time.now }
52
+ class Expiration
53
+ def self.call
54
+ Time.now
55
+ end
56
+ end
57
+
58
+ class Checksum
59
+ def self.call(user)
60
+ Digest::SHA1.hexdigest(user.name.to_s, user.email.to_s)
61
+ end
62
+ end
63
+
64
+ class User < ApplicationRecord
65
+ defaults checksum: Checksum,
66
+ expires_at: -> { Time.now }
67
+ end
38
68
  ```
39
69
 
40
70
  You can override the default attributes as follow:
41
71
 
42
72
  ```ruby
43
- Page.default_options = {title: "Here's your new page", body: "Write your page text"}
73
+ Page.default_values = {title: "Here's your new page", body: "Write your page text"}
44
74
  ```
45
75
 
46
76
  ## Maintainer
47
77
 
48
- * Nando Vieira - http://nandovieira.com
78
+ Nando Vieira - http://nandovieira.com
49
79
 
50
80
  ## License
51
81
 
@@ -16,10 +16,10 @@ Gem::Specification.new do |s|
16
16
  s.executables = `git ls-files -- bin/*`.split("\n").map {|f| File.basename(f) }
17
17
  s.require_paths = ["lib"]
18
18
 
19
- s.add_development_dependency "activerecord", ">= 3.0.0"
20
- s.add_development_dependency "sqlite3-ruby"
21
- s.add_development_dependency "rake"
19
+ s.add_dependency "activerecord", ">= 5.0.0"
22
20
  s.add_development_dependency "minitest-utils"
23
- s.add_development_dependency "codeclimate-test-reporter"
24
21
  s.add_development_dependency "pry-meta"
22
+ s.add_development_dependency "rake"
23
+ s.add_development_dependency "simplecov"
24
+ s.add_development_dependency "sqlite3"
25
25
  end
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+ gemspec path: ".."
3
+ gem "activerecord", "~> 5.0.0"
@@ -0,0 +1,73 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ defaults (1.0.1)
5
+ activerecord (>= 5.0.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.0.7.1)
11
+ activesupport (= 5.0.7.1)
12
+ activerecord (5.0.7.1)
13
+ activemodel (= 5.0.7.1)
14
+ activesupport (= 5.0.7.1)
15
+ arel (~> 7.0)
16
+ activesupport (5.0.7.1)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ arel (7.1.4)
22
+ awesome_print (1.8.0)
23
+ byebug (10.0.2)
24
+ coderay (1.1.2)
25
+ concurrent-ruby (1.1.4)
26
+ docile (1.3.1)
27
+ i18n (1.3.0)
28
+ concurrent-ruby (~> 1.0)
29
+ json (2.1.0)
30
+ method_source (0.9.2)
31
+ minitest (5.11.3)
32
+ minitest-utils (0.4.4)
33
+ minitest
34
+ pry (0.12.2)
35
+ coderay (~> 1.1.0)
36
+ method_source (~> 0.9.0)
37
+ pry-byebug (3.6.0)
38
+ byebug (~> 10.0)
39
+ pry (~> 0.10)
40
+ pry-meta (0.0.10)
41
+ awesome_print
42
+ pry
43
+ pry-byebug
44
+ pry-remote
45
+ pry-remote (0.1.8)
46
+ pry (~> 0.9)
47
+ slop (~> 3.0)
48
+ rake (12.3.2)
49
+ simplecov (0.16.1)
50
+ docile (~> 1.1)
51
+ json (>= 1.8, < 3)
52
+ simplecov-html (~> 0.10.0)
53
+ simplecov-html (0.10.2)
54
+ slop (3.6.0)
55
+ sqlite3 (1.3.13)
56
+ thread_safe (0.3.6)
57
+ tzinfo (1.2.5)
58
+ thread_safe (~> 0.1)
59
+
60
+ PLATFORMS
61
+ ruby
62
+
63
+ DEPENDENCIES
64
+ activerecord (~> 5.0.0)
65
+ defaults!
66
+ minitest-utils
67
+ pry-meta
68
+ rake
69
+ simplecov
70
+ sqlite3
71
+
72
+ BUNDLED WITH
73
+ 1.17.2
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+ gemspec path: ".."
3
+ gem "activerecord", "~> 5.1.0"
@@ -0,0 +1,73 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ defaults (1.0.1)
5
+ activerecord (>= 5.0.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.1.6.1)
11
+ activesupport (= 5.1.6.1)
12
+ activerecord (5.1.6.1)
13
+ activemodel (= 5.1.6.1)
14
+ activesupport (= 5.1.6.1)
15
+ arel (~> 8.0)
16
+ activesupport (5.1.6.1)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ arel (8.0.0)
22
+ awesome_print (1.8.0)
23
+ byebug (10.0.2)
24
+ coderay (1.1.2)
25
+ concurrent-ruby (1.1.4)
26
+ docile (1.3.1)
27
+ i18n (1.3.0)
28
+ concurrent-ruby (~> 1.0)
29
+ json (2.1.0)
30
+ method_source (0.9.2)
31
+ minitest (5.11.3)
32
+ minitest-utils (0.4.4)
33
+ minitest
34
+ pry (0.12.2)
35
+ coderay (~> 1.1.0)
36
+ method_source (~> 0.9.0)
37
+ pry-byebug (3.6.0)
38
+ byebug (~> 10.0)
39
+ pry (~> 0.10)
40
+ pry-meta (0.0.10)
41
+ awesome_print
42
+ pry
43
+ pry-byebug
44
+ pry-remote
45
+ pry-remote (0.1.8)
46
+ pry (~> 0.9)
47
+ slop (~> 3.0)
48
+ rake (12.3.2)
49
+ simplecov (0.16.1)
50
+ docile (~> 1.1)
51
+ json (>= 1.8, < 3)
52
+ simplecov-html (~> 0.10.0)
53
+ simplecov-html (0.10.2)
54
+ slop (3.6.0)
55
+ sqlite3 (1.3.13)
56
+ thread_safe (0.3.6)
57
+ tzinfo (1.2.5)
58
+ thread_safe (~> 0.1)
59
+
60
+ PLATFORMS
61
+ ruby
62
+
63
+ DEPENDENCIES
64
+ activerecord (~> 5.1.0)
65
+ defaults!
66
+ minitest-utils
67
+ pry-meta
68
+ rake
69
+ simplecov
70
+ sqlite3
71
+
72
+ BUNDLED WITH
73
+ 1.17.2
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+ gemspec path: ".."
3
+ gem "activerecord", "~> 5.2.0"
@@ -0,0 +1,73 @@
1
+ PATH
2
+ remote: ..
3
+ specs:
4
+ defaults (1.0.1)
5
+ activerecord (>= 5.0.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.2)
11
+ activesupport (= 5.2.2)
12
+ activerecord (5.2.2)
13
+ activemodel (= 5.2.2)
14
+ activesupport (= 5.2.2)
15
+ arel (>= 9.0)
16
+ activesupport (5.2.2)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ arel (9.0.0)
22
+ awesome_print (1.8.0)
23
+ byebug (10.0.2)
24
+ coderay (1.1.2)
25
+ concurrent-ruby (1.1.4)
26
+ docile (1.3.1)
27
+ i18n (1.3.0)
28
+ concurrent-ruby (~> 1.0)
29
+ json (2.1.0)
30
+ method_source (0.9.2)
31
+ minitest (5.11.3)
32
+ minitest-utils (0.4.4)
33
+ minitest
34
+ pry (0.12.2)
35
+ coderay (~> 1.1.0)
36
+ method_source (~> 0.9.0)
37
+ pry-byebug (3.6.0)
38
+ byebug (~> 10.0)
39
+ pry (~> 0.10)
40
+ pry-meta (0.0.10)
41
+ awesome_print
42
+ pry
43
+ pry-byebug
44
+ pry-remote
45
+ pry-remote (0.1.8)
46
+ pry (~> 0.9)
47
+ slop (~> 3.0)
48
+ rake (12.3.2)
49
+ simplecov (0.16.1)
50
+ docile (~> 1.1)
51
+ json (>= 1.8, < 3)
52
+ simplecov-html (~> 0.10.0)
53
+ simplecov-html (0.10.2)
54
+ slop (3.6.0)
55
+ sqlite3 (1.3.13)
56
+ thread_safe (0.3.6)
57
+ tzinfo (1.2.5)
58
+ thread_safe (~> 0.1)
59
+
60
+ PLATFORMS
61
+ ruby
62
+
63
+ DEPENDENCIES
64
+ activerecord (~> 5.2.0)
65
+ defaults!
66
+ minitest-utils
67
+ pry-meta
68
+ rake
69
+ simplecov
70
+ sqlite3
71
+
72
+ BUNDLED WITH
73
+ 1.17.2
@@ -1,23 +1,51 @@
1
1
  module Defaults
2
2
  require "defaults/version"
3
3
 
4
+ def self.execute_callable(value, record)
5
+ callable = value.is_a?(Proc) ? value : value.method(:call)
6
+ callable.arity == 1 ? value.call(record) : value.call
7
+ end
8
+
9
+ def self.default_value_for_column(model, attribute)
10
+ column_info = model.columns_hash[attribute.to_s]
11
+ ActiveRecord::Type.lookup(column_info.type).cast(column_info.default) if column_info
12
+ end
13
+
14
+ def self.default_value(model, record, attribute)
15
+ value = model.default_values[attribute]
16
+ value = Defaults.execute_callable(value, record) if value.respond_to?(:call)
17
+ value
18
+ end
19
+
20
+ def self.resolve_default_value(model, record, attribute)
21
+ attribute = attribute.to_sym
22
+
23
+ # Check if value has been defined by `defaults`.
24
+ # If it is, try to resolve by checking if value is callable.
25
+ # Otherwise returns the default value of that column.
26
+ if model.default_values.key?(attribute)
27
+ Defaults.default_value(model, record, attribute)
28
+ else
29
+ Defaults.default_value_for_column(model, attribute)
30
+ end
31
+ end
32
+
4
33
  def self.included(base)
5
34
  base.extend ClassMethods
6
35
 
7
36
  class << base
8
- attr_accessor :default_options
37
+ attr_accessor :default_values
9
38
  end
10
39
  end
11
40
 
12
41
  module ClassMethods
13
- # defaults :title => "Add your title here"
14
42
  def defaults(attrs)
15
- raise ArgumentError, "Hash expected; #{attrs.class} given." unless attrs.kind_of?(Hash)
43
+ raise ArgumentError, "Hash expected; #{attrs.class} given." unless attrs.is_a?(Hash)
16
44
 
17
45
  include InstanceMethods
18
46
 
19
- self.default_options ||= {}
20
- self.default_options.merge!(attrs)
47
+ self.default_values ||= {}
48
+ self.default_values.merge!(attrs)
21
49
 
22
50
  after_initialize :set_default_attributes
23
51
  end
@@ -25,26 +53,17 @@ module Defaults
25
53
 
26
54
  module InstanceMethods
27
55
  def default_for(name)
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
-
36
- value
56
+ Defaults.resolve_default_value(self.class, self, name)
37
57
  end
38
58
 
39
- private
40
- def set_default_attributes
59
+ private def set_default_attributes
41
60
  return unless new_record?
42
61
 
43
- self.class.default_options.keys.each do |name|
62
+ self.class.default_values.keys.each do |name|
44
63
  value = read_attribute(name) if changes[name]
45
64
  value = default_for(name) if value.blank?
46
65
 
47
- __send__ "#{name}=", value
66
+ public_send "#{name}=", value
48
67
  end
49
68
  end
50
69
  end
@@ -1,8 +1,8 @@
1
1
  module Defaults
2
2
  module Version
3
- MAJOR = 1
3
+ MAJOR = 2
4
4
  MINOR = 0
5
- PATCH = 1
5
+ PATCH = 0
6
6
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
7
7
  end
8
8
  end
@@ -3,10 +3,17 @@ require "test_helper"
3
3
  class Donut < ActiveRecord::Base
4
4
  attr_accessor :class_name
5
5
 
6
+ slug_generator = Class.new do
7
+ def self.call
8
+ "delicious-donut-#{(1000..9999).to_a.sample}"
9
+ end
10
+ end
11
+
6
12
  defaults flavor: "cream",
7
13
  name: "Cream",
8
14
  maker: proc { "Dunkin Donuts" },
9
- class_name: -> donut { donut.class.name }
15
+ class_name: ->(donut) { donut.class.name },
16
+ slug: slug_generator
10
17
 
11
18
  defaults quantity: 5
12
19
  defaults available: false
@@ -20,11 +27,13 @@ class DefaultsTest < Minitest::Test
20
27
  Donut.delete_all
21
28
  end
22
29
 
23
- test "considers detabase defaults with type casting" do
30
+ test "considers database defaults with type casting" do
31
+ assert_kind_of Float, new_donut.default_for(:price)
24
32
  assert_equal 0.00, new_donut.default_for(:price)
25
33
  end
26
34
 
27
35
  test "has precedence over database defaults" do
36
+ assert ActiveRecord::Type.lookup(:boolean).cast(Donut.columns_hash["available"].default)
28
37
  refute new_donut.available?
29
38
  end
30
39
 
@@ -38,6 +47,7 @@ class DefaultsTest < Minitest::Test
38
47
  assert_equal "Dunkin Donuts", new_donut.maker
39
48
  assert_equal "Donut", new_donut.class_name
40
49
  assert_equal 5, new_donut.quantity
50
+ assert_match /^delicious-donut-\d{4}$/, new_donut.slug
41
51
  end
42
52
 
43
53
  test "sets default values only when have blank attributes" do
@@ -57,10 +67,89 @@ class DefaultsTest < Minitest::Test
57
67
  assert_equal "vanilla", Donut.first.flavor
58
68
  end
59
69
 
70
+ test "considers arity=0 of callable (lambda)" do
71
+ donut_class = model do
72
+ defaults quantity: -> { 1234 }
73
+ end
74
+
75
+ donut = donut_class.new
76
+
77
+ assert_equal 1234, donut.default_for(:quantity)
78
+ end
79
+
80
+ test "considers arity=1 of callable (lambda)" do
81
+ record = nil
82
+
83
+ donut_class = model do
84
+ defaults quantity: ->(r) { record = r }
85
+ end
86
+
87
+ donut = donut_class.new
88
+ donut.default_for(:quantity)
89
+
90
+ assert_equal donut, record
91
+ end
92
+
93
+ test "considers arity=0 of callable (class)" do
94
+ quantity_class = Class.new do
95
+ def self.call
96
+ 1234
97
+ end
98
+ end
99
+
100
+ donut_class = model do
101
+ defaults quantity: quantity_class
102
+ end
103
+
104
+ donut = donut_class.new
105
+
106
+ assert_equal 1234, donut.default_for(:quantity)
107
+ end
108
+
109
+ test "considers arity=1 of callable (class)" do
110
+ quantity_class = Class.new do
111
+ def self.record
112
+ @record
113
+ end
114
+
115
+ def self.call(record)
116
+ @record = record
117
+ end
118
+ end
119
+
120
+ donut_class = model do
121
+ defaults quantity: quantity_class
122
+ end
123
+
124
+ donut = donut_class.new
125
+ donut.default_for(:quantity)
126
+
127
+ assert_equal donut, quantity_class.record
128
+ end
129
+
130
+ test "overrides default values" do
131
+ donut_class = model do
132
+ defaults quantity: -> { 1234 }
133
+ end
134
+
135
+ donut_class.default_values[:quantity] = 42
136
+
137
+ donut = donut_class.new
138
+
139
+ assert_equal 42, donut.default_for(:quantity)
140
+ end
141
+
60
142
  private
61
143
 
62
- def create_donut(options = {})
63
- Donut.create({
144
+ def model(&block)
145
+ Class.new(ActiveRecord::Base) do
146
+ self.table_name = :donuts
147
+ instance_eval(&block)
148
+ end
149
+ end
150
+
151
+ def create_donut(options = {}, model = Donut)
152
+ model.create({
64
153
  flavor: "vanilla",
65
154
  name: "Vanilla Sky",
66
155
  maker: "Mr. Baker"
@@ -1,8 +1,9 @@
1
- ActiveRecord::Schema.define(:version => 0) do
1
+ ActiveRecord::Schema.define(version: 0) do
2
2
  create_table :donuts do |t|
3
3
  t.string :flavor, :name, :maker
4
4
  t.integer :quantity, null: false
5
5
  t.float :price, default: 0.00, null: false
6
6
  t.boolean :available, default: true, null: false
7
+ t.string :slug, null: false
7
8
  end
8
9
  end
@@ -1,5 +1,5 @@
1
- require "codeclimate-test-reporter"
2
- CodeClimate::TestReporter.start
1
+ require "simplecov"
2
+ SimpleCov.start
3
3
 
4
4
  require "bundler/setup"
5
5
  require "active_record"
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.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-06 00:00:00.000000000 Z
11
+ date: 2018-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,16 +16,16 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 3.0.0
20
- type: :development
19
+ version: 5.0.0
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 3.0.0
26
+ version: 5.0.0
27
27
  - !ruby/object:Gem::Dependency
28
- name: sqlite3-ruby
28
+ name: minitest-utils
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rake
42
+ name: pry-meta
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: minitest-utils
56
+ name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: codeclimate-test-reporter
70
+ name: simplecov
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: pry-meta
84
+ name: sqlite3
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -104,9 +104,16 @@ files:
104
104
  - ".gitignore"
105
105
  - ".travis.yml"
106
106
  - Gemfile
107
+ - Gemfile.lock
107
108
  - README.md
108
109
  - Rakefile
109
110
  - defaults.gemspec
111
+ - gemfiles/5_0.gemfile
112
+ - gemfiles/5_0.gemfile.lock
113
+ - gemfiles/5_1.gemfile
114
+ - gemfiles/5_1.gemfile.lock
115
+ - gemfiles/5_2.gemfile
116
+ - gemfiles/5_2.gemfile.lock
110
117
  - lib/defaults.rb
111
118
  - lib/defaults/version.rb
112
119
  - test/defaults/defaults_test.rb
@@ -131,8 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
138
  - !ruby/object:Gem::Version
132
139
  version: '0'
133
140
  requirements: []
134
- rubyforge_project:
135
- rubygems_version: 2.5.1
141
+ rubygems_version: 3.0.1
136
142
  signing_key:
137
143
  specification_version: 4
138
144
  summary: Set default values for ActiveRecord attributes