attr_boolean 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/attr_boolean.gemspec +23 -0
- data/lib/attr_boolean.rb +52 -0
- data/lib/attr_boolean/version.rb +3 -0
- data/spec/attr_boolean_spec.rb +60 -0
- data/spec/spec_helper.rb +1 -0
- metadata +90 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Daniel Vandersluis
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# AttrBoolean
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'attr_boolean'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install attr_boolean
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'attr_boolean/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "attr_boolean"
|
8
|
+
gem.version = AttrBoolean::VERSION
|
9
|
+
gem.authors = ["Daniel Vandersluis"]
|
10
|
+
gem.email = ["dvandersluis@selfmgmt.com"]
|
11
|
+
gem.description = %q{Shortcut for creating boolean attributes}
|
12
|
+
gem.summary = %q{Shortcut for creating boolean attributes}
|
13
|
+
gem.homepage = "https://www.github.com/talentnest/attr_boolean"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency "activesupport", ">= 3.0.0"
|
21
|
+
|
22
|
+
gem.add_development_dependency "rspec"
|
23
|
+
end
|
data/lib/attr_boolean.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require "active_support/core_ext/array/extract_options"
|
2
|
+
require "attr_boolean/version"
|
3
|
+
|
4
|
+
module AttrBoolean
|
5
|
+
if defined? Rails::Railtie
|
6
|
+
class Railtie < Rails::Railtie
|
7
|
+
initializer "attr_boolean.initialize" do
|
8
|
+
ActiveSupport.on_load :active_record do
|
9
|
+
ActiveRecord::Base.send(:extend, AttrBoolean::ClassMethods)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
def attr_boolean(*args)
|
17
|
+
options = args.extract_options!
|
18
|
+
raise ArgumentError, "cannot specify options if defining multiple boolean attributes in one call" if args.size > 1 and !options.empty?
|
19
|
+
|
20
|
+
args.each do |boolean|
|
21
|
+
boolean = boolean.intern
|
22
|
+
|
23
|
+
self.class_eval { attr_accessor boolean }
|
24
|
+
|
25
|
+
self.define_methods(boolean, options)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
protected
|
30
|
+
def define_methods(name, options)
|
31
|
+
self.class_eval do
|
32
|
+
default = options.key?(:default) ? options[:default] : false
|
33
|
+
|
34
|
+
define_method("#{name}?") do
|
35
|
+
!!send(name)
|
36
|
+
end
|
37
|
+
|
38
|
+
define_method("#{name}!") do
|
39
|
+
send("#{name}=", !default)
|
40
|
+
end
|
41
|
+
|
42
|
+
define_method(name) do
|
43
|
+
if instance_variable_get("@#{name}").nil?
|
44
|
+
default
|
45
|
+
else
|
46
|
+
instance_variable_get("@#{name}")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'attr_boolean'
|
2
|
+
|
3
|
+
class BooleanAttr
|
4
|
+
extend AttrBoolean::ClassMethods
|
5
|
+
attr_boolean :bool
|
6
|
+
end
|
7
|
+
|
8
|
+
class MultipleBooleanAttrs
|
9
|
+
extend AttrBoolean::ClassMethods
|
10
|
+
attr_boolean :bool, :bool2
|
11
|
+
end
|
12
|
+
|
13
|
+
class DefaultBooleanAttr
|
14
|
+
extend AttrBoolean::ClassMethods
|
15
|
+
attr_boolean :bool, :default => true
|
16
|
+
end
|
17
|
+
|
18
|
+
describe AttrBoolean do
|
19
|
+
it "should define methods" do
|
20
|
+
klass = BooleanAttr.new
|
21
|
+
klass.respond_to?(:bool).should be_true
|
22
|
+
klass.respond_to?(:bool=).should be_true
|
23
|
+
klass.respond_to?(:bool!).should be_true
|
24
|
+
klass.respond_to?(:bool?).should be_true
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should set a default value of false if not specified" do
|
28
|
+
klass = BooleanAttr.new
|
29
|
+
klass.bool.should be_false
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should set up a predicate" do
|
33
|
+
klass = BooleanAttr.new
|
34
|
+
klass.bool = true
|
35
|
+
klass.should be_bool
|
36
|
+
klass.bool = false
|
37
|
+
klass.should_not be_bool
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should set up a bang method which changes the value to the opposite of the default" do
|
41
|
+
klass = BooleanAttr.new
|
42
|
+
klass.bool!
|
43
|
+
klass.should be_bool
|
44
|
+
|
45
|
+
klass = DefaultBooleanAttr.new
|
46
|
+
klass.bool!
|
47
|
+
klass.should_not be_bool
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should define multiple boolean attributes if more than one is specified" do
|
51
|
+
klass = MultipleBooleanAttrs.new
|
52
|
+
klass.respond_to?(:bool).should be_true
|
53
|
+
klass.respond_to?(:bool2).should be_true
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should set a default value if given" do
|
57
|
+
klass = DefaultBooleanAttr.new
|
58
|
+
klass.bool.should be_true
|
59
|
+
end
|
60
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rspec'
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attr_boolean
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Daniel Vandersluis
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
prerelease: false
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
none: false
|
23
|
+
type: :runtime
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ! '>='
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: 3.0.0
|
29
|
+
none: false
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
prerelease: false
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
none: false
|
39
|
+
type: :development
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
none: false
|
46
|
+
description: Shortcut for creating boolean attributes
|
47
|
+
email:
|
48
|
+
- dvandersluis@selfmgmt.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- attr_boolean.gemspec
|
59
|
+
- lib/attr_boolean.rb
|
60
|
+
- lib/attr_boolean/version.rb
|
61
|
+
- spec/attr_boolean_spec.rb
|
62
|
+
- spec/spec_helper.rb
|
63
|
+
homepage: https://www.github.com/talentnest/attr_boolean
|
64
|
+
licenses: []
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
none: false
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
none: false
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.8.24
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Shortcut for creating boolean attributes
|
87
|
+
test_files:
|
88
|
+
- spec/attr_boolean_spec.rb
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
has_rdoc:
|