rails_default_value 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f9bc1c969e0a32cf0658032442cb66206ccfead5
4
+ data.tar.gz: f47efb58aa171da9bef2a13a619efbaaf677bf77
5
+ SHA512:
6
+ metadata.gz: 83f212370956f286ef9e36e17bbf4933aeacfa41f47ab111ff825af42302d0e81836b186347ccce9563982f41cf23d17875507db13dc19025c73da3550d4f01d
7
+ data.tar.gz: cbc010fc7959bcda9835eab20463c53c105f0cb3cf66ba558da0642744498afbff892f1e0932c62592b7009d0f8a7ca49bcdc5addb2a80be0d68a4d64c02d423
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rails_default_value.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Keith Rowell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2016 Keith Rowell
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
1
+ # RailsDefaultValue
2
+
3
+ Assign default values to Model fields in your Rails apps.
4
+
5
+ Other solutions for assigning default values tend to be at the database layer in migrations. If you prefer to keep your model logic in your model code, use `rails_default_value`.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'rails_default_value'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rails_default_value
20
+
21
+ ## Usage
22
+
23
+ class MyModel < ActiveRecord::Base
24
+ #...
25
+ default :field_name => 'some value'
26
+ default :some_other_field_name => :another_value
27
+ default :some_time_based_field_name => -> { Time.now + 14.days }
28
+ end
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it ( https://github.com/keithrowell/rails_default_value/fork )
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,4 @@
1
+ require "rails_default_value/version"
2
+ require "rails_default_value/rails_default_value"
3
+ require "rails_default_value/railtie" if defined? Rails
4
+
@@ -0,0 +1,32 @@
1
+ module RailsDefaultValue
2
+
3
+ module InstanceMethods
4
+
5
+ def set_defaults
6
+ self.class.default_values.each do |key, value|
7
+ if value.is_a? Proc
8
+ self.send("#{key.to_s}=", value.call)
9
+ else
10
+ self.send("#{key.to_s}=", value) if (self.send(key.to_s).respond_to?(:nil?) and self.send(key.to_s).nil?) or (self.send(key.to_s).respond_to?(:empty?) and self.send(key.to_s).empty?)
11
+ end
12
+ end
13
+ end
14
+
15
+ end
16
+
17
+ module ClassMethods
18
+
19
+ def default options
20
+
21
+ class_eval "@@default_values = {} unless defined? @@default_values"
22
+ class_eval "def self.default_values ; @@default_values ; end"
23
+
24
+ options.each do |key, value|
25
+ default_values[key] = value
26
+ end
27
+ include InstanceMethods
28
+ before_validation :set_defaults, :on => :create
29
+ end
30
+ end
31
+
32
+ end
@@ -0,0 +1,9 @@
1
+ module RailsDefaultValue
2
+ class Railtie < Rails::Railtie
3
+ initializer 'rails_default_value.model_additions' do
4
+ ActiveSupport.on_load :active_record do
5
+ extend ClassMethods
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module RailsDefaultValue
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rails_default_value/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_default_value"
8
+ spec.version = RailsDefaultValue::VERSION
9
+ spec.authors = ["Keith Rowell"]
10
+ spec.email = ["keith@keithrowell.com"]
11
+ spec.summary = "default values for active record models"
12
+ spec.description = "set default values for your active record models with a simple, concise DSL. \n\n: default :field_name => :value"
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "acts_as_fu"
25
+ end
@@ -0,0 +1,91 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'acts_as_fu'
4
+
5
+ build_model :my_model do
6
+ extend RailsDefaultValue::ClassMethods
7
+ integer :status
8
+ integer :number
9
+ string :name
10
+ datetime :stored_at
11
+ attr_accessor :status
12
+ attr_accessor :number
13
+ attr_accessor :name
14
+ attr_accessor :stored_at
15
+ default :status => :fixed
16
+ default :number => 1
17
+ default :name => 'Fred'
18
+ default :stored_at => -> { Time.now }
19
+ end
20
+
21
+ build_model :my_other_model do
22
+ extend RailsDefaultValue::ClassMethods
23
+ integer :field_1
24
+ integer :field_2
25
+ attr_accessor :field_1
26
+ attr_accessor :field_2
27
+ default :field_1 => 1
28
+ default :field_2 => 2
29
+
30
+ end
31
+
32
+ describe RailsDefaultValue do
33
+ context "with a new object" do
34
+ it "has a default value only after save" do
35
+ my_model = MyModel.new
36
+ expect(my_model.number).to be_nil
37
+ expect(my_model.save).to be true
38
+ expect(my_model.number).to eq 1
39
+ end
40
+
41
+ it "can override default value" do
42
+ my_model = MyModel.new :number => 5
43
+ expect(my_model.number).to eq 5
44
+ expect(my_model.save).to be true
45
+ expect(my_model.number).to eq 5
46
+ end
47
+ end
48
+
49
+ context "when an object is created" do
50
+ it "has a default status of :fixed" do
51
+ my_model = MyModel.create
52
+ expect(my_model.status).to eq :fixed
53
+ end
54
+
55
+ it "has a default time/proc value of about the right time" do
56
+ sleep 2
57
+ my_model = MyModel.create
58
+ expect(my_model.stored_at.utc).to be_within(1.second).of Time.now
59
+ end
60
+
61
+ it "has a default integer value" do
62
+ my_model = MyModel.create
63
+ expect(my_model.number).to eq 1
64
+ end
65
+
66
+ it "has a default string value" do
67
+ my_model = MyModel.create
68
+ expect(my_model.name).to eq 'Fred'
69
+ end
70
+
71
+ it "default status can be overwritten with create" do
72
+ my_model = MyModel.create :status => :fixed
73
+ expect(my_model.status.to_sym).to eq :fixed
74
+ my_model = MyModel.create :status => :broken
75
+ expect(my_model.status.to_sym).to eq :broken
76
+ end
77
+ end
78
+
79
+ context "with a second model" do
80
+ it "has a default value for field_1 of 1" do
81
+ my_model = MyOtherModel.create
82
+ expect(my_model.field_1).to eq 1
83
+ end
84
+
85
+ it "has a default value for field_2 of 2" do
86
+ my_model = MyOtherModel.create
87
+ expect(my_model.field_2).to eq 2
88
+ end
89
+ end
90
+
91
+ end
@@ -0,0 +1,6 @@
1
+ require 'rails_default_value'
2
+ require 'acts_as_fu'
3
+
4
+ RSpec.configure do |config|
5
+ config.include ActsAsFu
6
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_default_value
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Keith Rowell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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: acts_as_fu
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: "set default values for your active record models with a simple, concise
70
+ DSL. \n\n: default :field_name => :value"
71
+ email:
72
+ - keith@keithrowell.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - Gemfile
80
+ - LICENSE
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - lib/rails_default_value.rb
85
+ - lib/rails_default_value/rails_default_value.rb
86
+ - lib/rails_default_value/railtie.rb
87
+ - lib/rails_default_value/version.rb
88
+ - rails_default_value.gemspec
89
+ - spec/rails_default_value_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: ''
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.3.0
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: default values for active record models
115
+ test_files:
116
+ - spec/rails_default_value_spec.rb
117
+ - spec/spec_helper.rb