attribute_predicates 0.1.2 → 0.2.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/CHANGELOG.rdoc +4 -0
- data/Rakefile +1 -1
- data/lib/attribute_predicates/extensions/active_record.rb +44 -46
- data/lib/attribute_predicates/extensions/module.rb +67 -69
- metadata +5 -5
data/CHANGELOG.rdoc
CHANGED
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ require 'rake/contrib/sshpublisher'
|
|
5
5
|
|
6
6
|
spec = Gem::Specification.new do |s|
|
7
7
|
s.name = 'attribute_predicates'
|
8
|
-
s.version = '0.
|
8
|
+
s.version = '0.2.0'
|
9
9
|
s.platform = Gem::Platform::RUBY
|
10
10
|
s.summary = 'Adds automatic generation of predicate methods for attributes.'
|
11
11
|
|
@@ -1,53 +1,51 @@
|
|
1
|
-
module
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
::ActiveRecord::ConnectionAdapters::Column.value_to_boolean(instance_variable_get("@#{symbol}")) == true
|
44
|
-
end
|
1
|
+
module AttributePredicates
|
2
|
+
module Extensions
|
3
|
+
# Adds support for automatically defining predicate methods using +attr_predicate+
|
4
|
+
# when defining attributes using +attr+, +attr_reader+, +attr_reader+, and
|
5
|
+
# +attr_accessor+. In comparison to normal Ruby attributes, ActiveRecord
|
6
|
+
# predicates use a different system for defining true/false.
|
7
|
+
#
|
8
|
+
# == Examples
|
9
|
+
#
|
10
|
+
# The predicate methods for attributes use ActiveRecord's type conversion
|
11
|
+
# for booleans for determing whether to return true or false. For example,
|
12
|
+
#
|
13
|
+
# class Person < ActiveRecord::Base
|
14
|
+
# attr_accessor :value
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# p = Person.new
|
18
|
+
# p.value = false
|
19
|
+
# p.value? # => false
|
20
|
+
#
|
21
|
+
# p.value = 'false'
|
22
|
+
# p.value? # => false
|
23
|
+
#
|
24
|
+
# p.value = 'true'
|
25
|
+
# p.value? # => true
|
26
|
+
#
|
27
|
+
# p.value = 't'
|
28
|
+
# p.value? # => true
|
29
|
+
#
|
30
|
+
# p.value = 1
|
31
|
+
# p.value? # => true
|
32
|
+
module ActiveRecord
|
33
|
+
private
|
34
|
+
# For Strings, returns true when value is:
|
35
|
+
# * "true"
|
36
|
+
# * "t"
|
37
|
+
#
|
38
|
+
# For Integers, returns true when value is:
|
39
|
+
# * 1
|
40
|
+
def attr_predicate(symbol)
|
41
|
+
define_method("#{symbol}?") do
|
42
|
+
::ActiveRecord::ConnectionAdapters::Column.value_to_boolean(instance_variable_get("@#{symbol}")) == true
|
45
43
|
end
|
46
|
-
|
44
|
+
end
|
47
45
|
end
|
48
46
|
end
|
49
47
|
end
|
50
48
|
|
51
49
|
ActiveRecord::Base.class_eval do
|
52
|
-
extend
|
50
|
+
extend AttributePredicates::Extensions::ActiveRecord
|
53
51
|
end if defined?(ActiveRecord)
|
@@ -1,81 +1,79 @@
|
|
1
|
-
module
|
2
|
-
module
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
1
|
+
module AttributePredicates
|
2
|
+
module Extensions
|
3
|
+
# Adds support for automatically defining predicate methods using +attr_predicate+
|
4
|
+
# when defining attributes using +attr+, +attr_reader+, +attr_reader+, and
|
5
|
+
# +attr_accessor+.
|
6
|
+
#
|
7
|
+
# == Examples
|
8
|
+
#
|
9
|
+
# The predicate methods for attributes checks whether the value is blank?
|
10
|
+
# for determining whether true or false should be returned. For example,
|
11
|
+
#
|
12
|
+
# class Person
|
13
|
+
# attr_accessor :value
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# p = Person.new
|
17
|
+
# p.value = false
|
18
|
+
# p.value? # => false
|
19
|
+
#
|
20
|
+
# p.value = true
|
21
|
+
# p.value? # => true
|
22
|
+
#
|
23
|
+
# p.value = []
|
24
|
+
# p.value? # => false
|
25
|
+
#
|
26
|
+
# p.value = [false]
|
27
|
+
# p.value? # => true
|
28
|
+
module Module
|
29
|
+
def self.included(base) #:nodoc:
|
30
|
+
base.class_eval do
|
31
|
+
%w(attr attr_reader attr_writer attr_accessor).each do |method|
|
32
|
+
alias_method "#{method}_without_predicates", method
|
33
|
+
alias_method method, "#{method}_with_predicates"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Defines a predicate method, using +attr_predicate+, in addition to the
|
39
|
+
# attribute accessors. For example,
|
12
40
|
#
|
13
|
-
#
|
14
|
-
#
|
41
|
+
# module Mod
|
42
|
+
# attr :is_okay
|
15
43
|
# end
|
16
44
|
#
|
17
|
-
#
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
# p.value = [false]
|
28
|
-
# p.value? # => true
|
29
|
-
module Module
|
30
|
-
def self.included(base) #:nodoc:
|
31
|
-
base.class_eval do
|
32
|
-
%w(attr attr_reader attr_writer attr_accessor).each do |method|
|
33
|
-
alias_method "#{method}_without_predicates", method
|
34
|
-
alias_method method, "#{method}_with_predicates"
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
# Defines a predicate method, using +attr_predicate+, in addition to the
|
40
|
-
# attribute accessors. For example,
|
41
|
-
#
|
42
|
-
# module Mod
|
43
|
-
# attr :is_okay
|
44
|
-
# end
|
45
|
-
#
|
46
|
-
# Mod.instance-methods.sort # => ["is_okay", "is_okay?"]
|
47
|
-
def attr_with_predicates(*args)
|
48
|
-
attr_without_predicates(*args)
|
49
|
-
attr_predicate(args.first)
|
50
|
-
end
|
51
|
-
|
52
|
-
[:attr_reader, :attr_writer, :attr_accessor].each do |method|
|
53
|
-
define_method("#{method}_with_predicates") do |*symbols|
|
54
|
-
send("#{method}_without_predicates", *symbols)
|
55
|
-
symbols.each {|symbol| attr_predicate(symbol)}
|
56
|
-
end
|
45
|
+
# Mod.instance-methods.sort # => ["is_okay", "is_okay?"]
|
46
|
+
def attr_with_predicates(*args)
|
47
|
+
attr_without_predicates(*args)
|
48
|
+
attr_predicate(args.first)
|
49
|
+
end
|
50
|
+
|
51
|
+
[:attr_reader, :attr_writer, :attr_accessor].each do |method|
|
52
|
+
define_method("#{method}_with_predicates") do |*symbols|
|
53
|
+
send("#{method}_without_predicates", *symbols)
|
54
|
+
symbols.each {|symbol| attr_predicate(symbol)}
|
57
55
|
end
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
# Returns true if the specified variable is not blank, otherwise false
|
60
|
+
def attr_predicate(symbol)
|
61
|
+
define_method("#{symbol}?") do
|
62
|
+
value = instance_variable_get("@#{symbol}")
|
63
|
+
if value.respond_to?(:blank?)
|
64
|
+
# Use ActiveSupport's implementation
|
65
|
+
!value.blank?
|
66
|
+
elsif value.respond_to?(:empty?)
|
67
|
+
!value.empty?
|
68
|
+
else
|
69
|
+
!!value
|
72
70
|
end
|
73
71
|
end
|
74
|
-
|
72
|
+
end
|
75
73
|
end
|
76
74
|
end
|
77
75
|
end
|
78
76
|
|
79
77
|
::Module.class_eval do
|
80
|
-
include
|
78
|
+
include AttributePredicates::Extensions::Module
|
81
79
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribute_predicates
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aaron Pfeifer
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-
|
12
|
+
date: 2008-12-14 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -22,13 +22,13 @@ extensions: []
|
|
22
22
|
extra_rdoc_files: []
|
23
23
|
|
24
24
|
files:
|
25
|
+
- lib/attribute_predicates.rb
|
25
26
|
- lib/attribute_predicates
|
26
27
|
- lib/attribute_predicates/extensions
|
27
|
-
- lib/attribute_predicates/extensions/active_record.rb
|
28
28
|
- lib/attribute_predicates/extensions/module.rb
|
29
|
-
- lib/attribute_predicates.rb
|
30
|
-
- test/module_test.rb
|
29
|
+
- lib/attribute_predicates/extensions/active_record.rb
|
31
30
|
- test/test_helper.rb
|
31
|
+
- test/module_test.rb
|
32
32
|
- test/active_record_test.rb
|
33
33
|
- CHANGELOG.rdoc
|
34
34
|
- init.rb
|