boolize_attr 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e8f0570b2d461294c6da0c18da477fc79e880aa9
4
+ data.tar.gz: 7d9c59ebc69200d20c0c0e45bc01c725c125efac
5
+ SHA512:
6
+ metadata.gz: 0d4c868b0e42c8199955faed03b98147a1a8239984618365583da8a6eba846a6b1057d07a3844312c20c9ac4a5a9c25fa053705c738e3fe571cbdf701050c986
7
+ data.tar.gz: a7f1f4b6c69931533bc1a49358e24b91625ecafbd9359220d369b7c6e8506865030db77b6eb8721d234021bcbdfe567a4f5598b4f4873ac6f5153bfb1ba6c1e5
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in boolize_attr.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Takashi CHIBA
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.
22
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Takashi CHIBA
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,89 @@
1
+ # boolize_attr
2
+
3
+ Boolize string attributes and define accessors.
4
+
5
+ ## Usage
6
+
7
+ ### Before boolize
8
+
9
+ ```ruby
10
+ # Model
11
+ class Job
12
+ include ActiveModel::Model
13
+
14
+ attr_accessor :is_running
15
+ end
16
+
17
+ # Controller
18
+ class JobsController
19
+ def new
20
+ @job = Job.new
21
+ end
22
+
23
+ def create
24
+ @job = Job.new(params[:job])
25
+
26
+ p @job.is_running # returns String which '1' or '0' from a checkbox
27
+ !!@job.is_running # returns always true
28
+ end
29
+ end
30
+ ```
31
+
32
+ ### After boolize
33
+
34
+ ```ruby
35
+ # Model
36
+ class Job
37
+ include ActiveModel::Model
38
+
39
+ attr_accessor :is_running
40
+ boolize_attr_reader :is_running
41
+ end
42
+
43
+ # Controller
44
+ class JobsController
45
+ def new
46
+ @job = Job.new
47
+ end
48
+
49
+ def create
50
+ @job = Job.new(params[:job])
51
+
52
+ p @job.is_running # returns true or false
53
+ !!@job.is_running # returns true or false
54
+
55
+ @job.is_running = '0'
56
+ p @job.is_running #=> false
57
+
58
+ @job.is_running = '1'
59
+ p @job.is_running #=> true
60
+
61
+ @job.is_running = nil
62
+ p @job.is_running #=> false
63
+ end
64
+ end
65
+ ```
66
+
67
+ ## Installation
68
+
69
+ Add this line to your application's Gemfile:
70
+
71
+ ```ruby
72
+ gem 'boolize_attr'
73
+ ```
74
+
75
+ And then execute:
76
+
77
+ $ bundle
78
+
79
+ Or install it yourself as:
80
+
81
+ $ gem install boolize_attr
82
+
83
+ ## Contributing
84
+
85
+ 1. Fork it ( https://github.com/[my-github-username]/boolize_attr/fork )
86
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
87
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
88
+ 4. Push to the branch (`git push origin my-new-feature`)
89
+ 5. Create a new Pull Request
@@ -0,0 +1,12 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.libs << '.'
7
+ t.test_files = FileList['test/test*.rb']
8
+ t.verbose = true
9
+ t.warning = true
10
+ end
11
+
12
+ task :default => :test
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'boolize_attr/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "boolize_attr"
8
+ spec.version = BoolizeAttr::VERSION
9
+ spec.authors = ["Takashi CHIBA"]
10
+ spec.email = ["contact@tachiba.jp"]
11
+ spec.summary = %q{Boolize string attributes and define accessors}
12
+ spec.description = %q{}
13
+ spec.homepage = "https://github.com/tachiba/boolize_attr"
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"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+ end
@@ -0,0 +1,35 @@
1
+ require "boolize_attr/version"
2
+
3
+ module BoolizeAttr
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def boolize_attr_reader(*args)
10
+ args.each do |attr|
11
+ remove_method attr if defined?(attr)
12
+
13
+ define_method attr do
14
+ value = if instance_variable_defined?(:"@#{attr}")
15
+ instance_variable_get(:"@#{attr}")
16
+ end
17
+
18
+ return false if value.nil?
19
+ return value if value.is_a?(TrueClass) || value.is_a?(FalseClass)
20
+
21
+ value && value.is_a?(String) && value.to_i == 1
22
+ end
23
+ end
24
+ end
25
+
26
+ def boolize_attr_accessor(*args)
27
+ attr_accessor(*args)
28
+ boolize_attr_reader(*args)
29
+ end
30
+
31
+ def boolize_attr_writer(*args)
32
+ attr_writer(*args)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module BoolizeAttr
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,46 @@
1
+ require 'minitest/autorun'
2
+ require 'boolize_attr'
3
+
4
+ class TestBoolizeAttr < Minitest::Test
5
+ def setup
6
+ @klass = Class.new
7
+ @klass.include BoolizeAttr
8
+ end
9
+
10
+ def test_reader
11
+ @klass.class_eval do
12
+ attr_accessor :hoge
13
+ boolize_attr_reader :hoge
14
+ end
15
+
16
+ @instance = @klass.new
17
+
18
+ assert_equal false, @instance.hoge
19
+
20
+ @instance.hoge = '0'
21
+
22
+ assert_equal false, @instance.hoge
23
+
24
+ @instance.hoge = '1'
25
+
26
+ assert_equal true, @instance.hoge
27
+ end
28
+
29
+ def test_accessor
30
+ @klass.class_eval do
31
+ boolize_attr_accessor :hoge
32
+ end
33
+
34
+ @instance = @klass.new
35
+
36
+ assert_equal false, @instance.hoge
37
+
38
+ @instance.hoge = '0'
39
+
40
+ assert_equal false, @instance.hoge
41
+
42
+ @instance.hoge = '1'
43
+
44
+ assert_equal true, @instance.hoge
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: boolize_attr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Takashi CHIBA
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
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: minitest
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
+ description: ''
56
+ email:
57
+ - contact@tachiba.jp
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - boolize_attr.gemspec
69
+ - lib/boolize_attr.rb
70
+ - lib/boolize_attr/version.rb
71
+ - test/test_boolize_attr.rb
72
+ homepage: https://github.com/tachiba/boolize_attr
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.4.5
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Boolize string attributes and define accessors
96
+ test_files:
97
+ - test/test_boolize_attr.rb