predicates 0.1.0
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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +55 -0
- data/Rakefile +11 -0
- data/lib/predicates/version.rb +3 -0
- data/lib/predicates.rb +51 -0
- data/predicates.gemspec +17 -0
- data/test/predicates_test.rb +78 -0
- data/test/test_helper.rb +2 -0
- metadata +57 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 TODO: Write your name
|
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,55 @@
|
|
1
|
+
# Predicates
|
2
|
+
|
3
|
+
Truth accessors for your Ruby classes. Yep, that's about it.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'predicates'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install predicates
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Include Predicates in your class or model and define the predicate by supplying an attribute:
|
23
|
+
|
24
|
+
class User
|
25
|
+
include Predicates
|
26
|
+
|
27
|
+
attr_accessor :confirmed
|
28
|
+
predicate :confirmed?
|
29
|
+
end
|
30
|
+
|
31
|
+
user = User.new
|
32
|
+
user.confirmed = 1
|
33
|
+
user.confirmed? #=> true
|
34
|
+
|
35
|
+
Predicates will define the attribute for you using `attr_accessor` if the attribute you supplied has not been defined yet.
|
36
|
+
|
37
|
+
class User
|
38
|
+
include Predicates
|
39
|
+
|
40
|
+
predicate :confirmed?
|
41
|
+
end
|
42
|
+
|
43
|
+
user = User.new
|
44
|
+
user.confirmed = 1
|
45
|
+
user.confirmed #=> 1
|
46
|
+
user.confirmed? #=> true
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it
|
51
|
+
2. Create your feature branch ( `git checkout -b my-new-feature` )
|
52
|
+
3. Create tests and make them pass ( `rake test` )
|
53
|
+
4. Commit your changes ( `git commit -am 'Added some feature'` )
|
54
|
+
5. Push to the branch ( `git push origin my-new-feature` )
|
55
|
+
6. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/predicates.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require "predicates/version"
|
2
|
+
|
3
|
+
module Predicates
|
4
|
+
class NameError < Exception; end
|
5
|
+
|
6
|
+
# Defines a predicate (truth accessor) for a given attribute. If the attribute has not been defined yet,
|
7
|
+
# the attribute will be created along with the predicate via +attr_accessor+.
|
8
|
+
#
|
9
|
+
# == Class Example
|
10
|
+
#
|
11
|
+
# class User
|
12
|
+
# include Predicates
|
13
|
+
#
|
14
|
+
# predicate :confirmed?
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# user = User.new
|
18
|
+
# user.single = 1
|
19
|
+
# user.confirmed? #=> true
|
20
|
+
# user.single = nil
|
21
|
+
# user.confirmed? #=> false
|
22
|
+
#
|
23
|
+
# == Ohm Model Example
|
24
|
+
#
|
25
|
+
# class User < Ohm::Model
|
26
|
+
# include Predicates
|
27
|
+
#
|
28
|
+
# attribute :confirmed
|
29
|
+
# predicate :confirmed?
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# user = User.new
|
33
|
+
# user.confirmed = 1
|
34
|
+
# user.confirmed? #=> true
|
35
|
+
# user.confirmed = nil
|
36
|
+
# user.confirmed? #=> false
|
37
|
+
|
38
|
+
def predicate(method)
|
39
|
+
if method.to_s =~ /(.*?)\?+$/
|
40
|
+
attribute = $1.to_sym
|
41
|
+
else
|
42
|
+
raise NameError
|
43
|
+
end
|
44
|
+
|
45
|
+
attr_accessor attribute unless method_defined? attribute
|
46
|
+
|
47
|
+
define_method method do
|
48
|
+
!!send(attribute)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/predicates.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/predicates/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Erol Fornoles"]
|
6
|
+
gem.email = ["erol.fornoles@gmail.com"]
|
7
|
+
gem.description = %q{Truth accessors for Ruby classes.}
|
8
|
+
gem.summary = %q{Truth accessors for Ruby classes.}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "predicates"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Predicates::VERSION
|
17
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require_relative 'test_helper.rb'
|
2
|
+
|
3
|
+
require 'predicates.rb'
|
4
|
+
|
5
|
+
class WithPredicates
|
6
|
+
extend Predicates
|
7
|
+
end
|
8
|
+
|
9
|
+
class PredicatesTest < MiniTest::Unit::TestCase
|
10
|
+
def test_predicate_names_must_contain_a_question_mark
|
11
|
+
assert_raises Predicates::NameError do
|
12
|
+
Class.new WithPredicates do
|
13
|
+
predicate :name
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
assert_silent do
|
18
|
+
Class.new WithPredicates do
|
19
|
+
predicate :name?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_define_a_predicate_with_an_existing_attribute_accessor
|
25
|
+
klass = Class.new WithPredicates do
|
26
|
+
attr_accessor :attribute
|
27
|
+
predicate :attribute?
|
28
|
+
end
|
29
|
+
|
30
|
+
object = klass.new
|
31
|
+
|
32
|
+
assert object.respond_to?(:attribute?)
|
33
|
+
|
34
|
+
object.attribute = 1
|
35
|
+
assert object.attribute?
|
36
|
+
|
37
|
+
object.attribute = true
|
38
|
+
assert object.attribute?
|
39
|
+
|
40
|
+
object.attribute = nil
|
41
|
+
refute object.attribute?
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_define_a_predicate_with_an_existing_attribute_reader
|
45
|
+
klass = Class.new WithPredicates do
|
46
|
+
attr_reader :attribute
|
47
|
+
predicate :attribute?
|
48
|
+
|
49
|
+
def initialize
|
50
|
+
@attribute = 1
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
object = klass.new
|
55
|
+
|
56
|
+
assert object.respond_to?(:attribute?)
|
57
|
+
refute object.respond_to?(:attribute=)
|
58
|
+
|
59
|
+
assert object.attribute?
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_define_a_predicate_with_an_existing_attribute_method
|
63
|
+
klass = Class.new WithPredicates do
|
64
|
+
def attribute
|
65
|
+
1
|
66
|
+
end
|
67
|
+
|
68
|
+
predicate :attribute?
|
69
|
+
end
|
70
|
+
|
71
|
+
object = klass.new
|
72
|
+
|
73
|
+
assert object.respond_to?(:attribute?)
|
74
|
+
refute object.respond_to?(:attribute=)
|
75
|
+
|
76
|
+
assert object.attribute?
|
77
|
+
end
|
78
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: predicates
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Erol Fornoles
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-19 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Truth accessors for Ruby classes.
|
15
|
+
email:
|
16
|
+
- erol.fornoles@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/predicates.rb
|
27
|
+
- lib/predicates/version.rb
|
28
|
+
- predicates.gemspec
|
29
|
+
- test/predicates_test.rb
|
30
|
+
- test/test_helper.rb
|
31
|
+
homepage: ''
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.24
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Truth accessors for Ruby classes.
|
55
|
+
test_files:
|
56
|
+
- test/predicates_test.rb
|
57
|
+
- test/test_helper.rb
|