fluent_conditions 0.0.2 → 0.0.3
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 +4 -0
- data/.rspec +1 -0
- data/.travis.yml +8 -0
- data/Gemfile +5 -0
- data/MIT-LICENSE +20 -0
- data/README.md +100 -0
- data/Rakefile +10 -0
- data/fluent_conditions.gemspec +20 -0
- data/lib/fluent_conditions.rb +8 -122
- data/lib/fluent_conditions/accessor_definers.rb +123 -0
- data/lib/fluent_conditions/condition_builder.rb +64 -0
- data/lib/fluent_conditions/version.rb +3 -0
- data/spec/fluent_conditions/accessor_definers_spec.rb +63 -0
- data/spec/fluent_conditions/{builder_spec.rb → condition_builder_spec.rb} +24 -35
- data/spec/fluent_conditions/fluent_conditions_spec.rb +2 -2
- data/spec/spec_helper.rb +3 -0
- metadata +28 -5
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2011 Tomasz Lipinski
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
#Fluent Conditions
|
2
|
+
Simplify your conditional expressions.
|
3
|
+
|
4
|
+
[](http://travis-ci.org/pplcanfly/fluent_conditions)
|
5
|
+
|
6
|
+
##Installation
|
7
|
+
gem install fluent_conditions
|
8
|
+
|
9
|
+
##Examples
|
10
|
+
|
11
|
+
###Basic usage with boolean fields
|
12
|
+
|
13
|
+
class User
|
14
|
+
include FluentConditions
|
15
|
+
|
16
|
+
attr_accessor :admin, :logged_in
|
17
|
+
|
18
|
+
fluent :admin
|
19
|
+
fluent :logged_in
|
20
|
+
end
|
21
|
+
|
22
|
+
user = User.new
|
23
|
+
user.admin = true
|
24
|
+
user.logged_in = true
|
25
|
+
|
26
|
+
And instead of:
|
27
|
+
|
28
|
+
user.logged_in && user.admin
|
29
|
+
|
30
|
+
Write it more fluently and DRY:
|
31
|
+
|
32
|
+
user.is.logged_in.admin? #=> true
|
33
|
+
|
34
|
+
User class with FluentConditions module included provides *is* method and *is_not* method as well. It returns negated result of an expression. As an expression methods you may also use negated versions of *admin* and *logged_in* methods - *not_admin*, *not_logged_in*.
|
35
|
+
|
36
|
+
*and* method is optional and in fact it does nothing except increasing readability in some cases.
|
37
|
+
|
38
|
+
user.is_not.logged_in.admin? #=> false
|
39
|
+
user.is.logged_in.and.not_admin? #=> false
|
40
|
+
|
41
|
+
Only last method in expression should be followed by question mark.
|
42
|
+
|
43
|
+
###:values
|
44
|
+
|
45
|
+
This option defines a set of expression methods. It may be useful for fields with limited set of possible values:
|
46
|
+
|
47
|
+
class Product
|
48
|
+
include FluentConditions
|
49
|
+
|
50
|
+
attr_accessor :color
|
51
|
+
|
52
|
+
fluent :color, :values => [:red, :green, :blue]
|
53
|
+
end
|
54
|
+
|
55
|
+
product = Product.new
|
56
|
+
product.color = :red
|
57
|
+
product.is.red? # => true
|
58
|
+
|
59
|
+
You can also use *or operator* in expressions:
|
60
|
+
|
61
|
+
product.is.red.or.green? #=> true
|
62
|
+
product.is.blue.or.green? #=> false
|
63
|
+
|
64
|
+
*or operator* can be placed anywhere between two expression methods and it affects only them.
|
65
|
+
|
66
|
+
So in the case below:
|
67
|
+
|
68
|
+
product.is.red.green.or.blue?
|
69
|
+
|
70
|
+
Expression will be interpreted like:
|
71
|
+
|
72
|
+
product.color == :red && (product.color == :green || product.color == :blue)
|
73
|
+
|
74
|
+
###:if
|
75
|
+
|
76
|
+
The :if option allows you to define custom methods checking some conditions:
|
77
|
+
|
78
|
+
class Product
|
79
|
+
include FluentConditions
|
80
|
+
|
81
|
+
attr_accessor :price
|
82
|
+
|
83
|
+
fluent :price, :as => :cheap, :if => lambda {|price| price < 100}
|
84
|
+
end
|
85
|
+
|
86
|
+
product = Product.new
|
87
|
+
product.price = 1000
|
88
|
+
product.is.cheap? #=> false
|
89
|
+
|
90
|
+
###OR
|
91
|
+
|
92
|
+
With *big OR* you can build even more complex expressions. The difference between *OR* and *or* is how expression will be interpreted.
|
93
|
+
|
94
|
+
For instance if we had a Fruit class with :name and :color attributes, each having some set of possible values, we could write expression like:
|
95
|
+
|
96
|
+
fruit.is.red.apple.OR.yellow.banana?
|
97
|
+
|
98
|
+
This expression would be interpreted like:
|
99
|
+
|
100
|
+
(fruit.color == :red && fruit.name == :apple) || (fruit.color == :yellow && fruit.name == :banana)
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require 'fluent_conditions/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "fluent_conditions"
|
6
|
+
s.version = FluentConditions::VERSION
|
7
|
+
s.required_ruby_version = '>= 1.8.7'
|
8
|
+
|
9
|
+
s.summary = "Simplifies complex conditions"
|
10
|
+
|
11
|
+
s.author = "pplcanfly"
|
12
|
+
s.homepage = "http://github.com/pplcanfly/fluent_conditions"
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.add_development_dependency "rspec"
|
20
|
+
end
|
data/lib/fluent_conditions.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
+
require File.expand_path('../fluent_conditions/condition_builder.rb', __FILE__)
|
2
|
+
require File.expand_path('../fluent_conditions/accessor_definers.rb', __FILE__)
|
3
|
+
|
1
4
|
module FluentConditions
|
2
5
|
|
3
6
|
def self.included(base)
|
4
|
-
base.instance_variable_set(:@builder, Class.new(
|
7
|
+
base.instance_variable_set(:@builder, Class.new(ConditionBuilder))
|
5
8
|
|
6
9
|
base.class_eval do
|
7
10
|
include InstanceMethods
|
@@ -20,131 +23,14 @@ module FluentConditions
|
|
20
23
|
end
|
21
24
|
end
|
22
25
|
|
23
|
-
|
26
|
+
module ClassMethods
|
24
27
|
def fluent(field, options = {})
|
25
|
-
|
26
|
-
|
27
|
-
if options.include?(:values)
|
28
|
-
options[:values].each do |value|
|
29
|
-
builder.class_eval do
|
30
|
-
define_method(value) do
|
31
|
-
update_and_continue(instance_variable_get(:@object).send(field) == value)
|
32
|
-
end
|
33
|
-
|
34
|
-
define_method("#{value}?") do
|
35
|
-
update_and_finish(instance_variable_get(:@object).send(field) == value)
|
36
|
-
end
|
37
|
-
|
38
|
-
define_method("not_#{value}") do
|
39
|
-
update_and_continue(instance_variable_get(:@object).send(field) != value)
|
40
|
-
end
|
41
|
-
|
42
|
-
define_method("not_#{value}?") do
|
43
|
-
update_and_finish(instance_variable_get(:@object).send(field) != value)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
elsif options.include?(:if)
|
48
|
-
field_name = options[:as]
|
49
|
-
condition_check = options[:if]
|
50
|
-
|
51
|
-
builder.class_eval do
|
52
|
-
define_method(field_name) do
|
53
|
-
update_and_continue(condition_check.call(instance_variable_get(:@object).send(field)))
|
54
|
-
end
|
55
|
-
|
56
|
-
define_method("#{field_name}?") do
|
57
|
-
update_and_finish(condition_check.call(instance_variable_get(:@object).send(field)))
|
58
|
-
end
|
59
|
-
|
60
|
-
define_method("not_#{field_name}") do
|
61
|
-
update_and_continue(!condition_check.call(instance_variable_get(:@object).send(field)))
|
62
|
-
end
|
28
|
+
accessor_definer = AccessorDefinerFactory.make_for(options)
|
29
|
+
condition_builder = instance_variable_get(:@builder)
|
63
30
|
|
64
|
-
|
65
|
-
update_and_finish(!condition_check.call(instance_variable_get(:@object).send(field)))
|
66
|
-
end
|
67
|
-
end
|
68
|
-
else
|
69
|
-
builder.class_eval do
|
70
|
-
define_method(field) do
|
71
|
-
update_and_continue(instance_variable_get(:@object).send(field))
|
72
|
-
end
|
73
|
-
|
74
|
-
define_method("#{field}?") do
|
75
|
-
update_and_finish(instance_variable_get(:@object).send(field))
|
76
|
-
end
|
77
|
-
|
78
|
-
define_method("not_#{field}") do
|
79
|
-
update_and_continue(!instance_variable_get(:@object).send(field))
|
80
|
-
end
|
81
|
-
|
82
|
-
define_method("not_#{field}?") do
|
83
|
-
update_and_finish(!instance_variable_get(:@object).send(field))
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
31
|
+
accessor_definer.define(condition_builder, field)
|
87
32
|
end
|
88
33
|
end
|
89
34
|
|
90
|
-
|
91
|
-
class Builder
|
92
|
-
def initialize(object, type)
|
93
|
-
@object = object
|
94
|
-
@type = type
|
95
|
-
@previous, @current, @big_or = true, true, false
|
96
|
-
end
|
97
|
-
|
98
|
-
def or
|
99
|
-
@or_flag = true
|
100
|
-
self
|
101
|
-
end
|
102
|
-
|
103
|
-
def OR
|
104
|
-
@big_or = @big_or || result
|
105
|
-
@big_or_flag = true
|
106
|
-
@previous, @current = true, true
|
107
|
-
self
|
108
|
-
end
|
109
|
-
|
110
|
-
def and
|
111
|
-
self
|
112
|
-
end
|
113
|
-
|
114
|
-
private
|
115
|
-
|
116
|
-
def update_and_continue(field_value)
|
117
|
-
update_result(field_value)
|
118
|
-
self
|
119
|
-
end
|
120
|
-
|
121
|
-
def update_and_finish(field_value)
|
122
|
-
update_result(field_value)
|
123
|
-
end_result
|
124
|
-
end
|
125
|
-
|
126
|
-
def update_result(field_value)
|
127
|
-
if @or_flag
|
128
|
-
@current = @current || field_value
|
129
|
-
@or_flag = false
|
130
|
-
else
|
131
|
-
@previous, @current = @previous && @current, field_value
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
def end_result
|
136
|
-
if @big_or_flag
|
137
|
-
return (@big_or || result) if @type == :positive
|
138
|
-
return !(@big_or || result) if @type == :negative
|
139
|
-
end
|
140
|
-
return result if @type == :positive
|
141
|
-
return !result if @type == :negative
|
142
|
-
end
|
143
|
-
|
144
|
-
def result
|
145
|
-
@previous && @current
|
146
|
-
end
|
147
|
-
|
148
|
-
end
|
149
35
|
end
|
150
36
|
|
@@ -0,0 +1,123 @@
|
|
1
|
+
module FluentConditions
|
2
|
+
class AccessorDefinerFactory
|
3
|
+
def self.make_for(options)
|
4
|
+
if options.include?(:values)
|
5
|
+
TextValueAccessorDefiner.new(options)
|
6
|
+
elsif options.include?(:if)
|
7
|
+
CustomAccessorDefiner.new(options)
|
8
|
+
else
|
9
|
+
BooleanAccessorDefiner.new(options)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class FluentAccessorMethod
|
15
|
+
def initialize(name)
|
16
|
+
@name = name
|
17
|
+
end
|
18
|
+
|
19
|
+
def positive_check
|
20
|
+
@name
|
21
|
+
end
|
22
|
+
|
23
|
+
def negative_check
|
24
|
+
"not_#{@name}"
|
25
|
+
end
|
26
|
+
|
27
|
+
def positive_check_with_result
|
28
|
+
"#{@name}?"
|
29
|
+
end
|
30
|
+
|
31
|
+
def negative_check_with_result
|
32
|
+
"not_#{@name}?"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class TextValueAccessorDefiner
|
37
|
+
def initialize(options)
|
38
|
+
@options = options
|
39
|
+
end
|
40
|
+
|
41
|
+
def define(builder, field)
|
42
|
+
@options[:values].each do |value|
|
43
|
+
accessor_method = FluentAccessorMethod.new(value)
|
44
|
+
|
45
|
+
builder.class_eval do
|
46
|
+
define_method(accessor_method.positive_check) do
|
47
|
+
update_and_continue(instance_variable_get(:@object).send(field) == value)
|
48
|
+
end
|
49
|
+
|
50
|
+
define_method(accessor_method.positive_check_with_result) do
|
51
|
+
update_and_finish(instance_variable_get(:@object).send(field) == value)
|
52
|
+
end
|
53
|
+
|
54
|
+
define_method(accessor_method.negative_check) do
|
55
|
+
update_and_continue(instance_variable_get(:@object).send(field) != value)
|
56
|
+
end
|
57
|
+
|
58
|
+
define_method(accessor_method.negative_check_with_result) do
|
59
|
+
update_and_finish(instance_variable_get(:@object).send(field) != value)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class CustomAccessorDefiner
|
67
|
+
def initialize(options)
|
68
|
+
@options = options
|
69
|
+
end
|
70
|
+
|
71
|
+
def define(builder, field)
|
72
|
+
field_name = @options[:as]
|
73
|
+
condition_check = @options[:if]
|
74
|
+
accessor_method = FluentAccessorMethod.new(field_name)
|
75
|
+
|
76
|
+
builder.class_eval do
|
77
|
+
define_method(accessor_method.positive_check) do
|
78
|
+
update_and_continue(condition_check.call(instance_variable_get(:@object).send(field)))
|
79
|
+
end
|
80
|
+
|
81
|
+
define_method(accessor_method.positive_check_with_result) do
|
82
|
+
update_and_finish(condition_check.call(instance_variable_get(:@object).send(field)))
|
83
|
+
end
|
84
|
+
|
85
|
+
define_method(accessor_method.negative_check) do
|
86
|
+
update_and_continue(!condition_check.call(instance_variable_get(:@object).send(field)))
|
87
|
+
end
|
88
|
+
|
89
|
+
define_method(accessor_method.negative_check_with_result) do
|
90
|
+
update_and_finish(!condition_check.call(instance_variable_get(:@object).send(field)))
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
class BooleanAccessorDefiner
|
97
|
+
def initialize(options)
|
98
|
+
@options = options
|
99
|
+
end
|
100
|
+
|
101
|
+
def define(builder, field)
|
102
|
+
accessor_method = FluentAccessorMethod.new(field)
|
103
|
+
|
104
|
+
builder.class_eval do
|
105
|
+
define_method(accessor_method.positive_check) do
|
106
|
+
update_and_continue(instance_variable_get(:@object).send(field))
|
107
|
+
end
|
108
|
+
|
109
|
+
define_method(accessor_method.positive_check_with_result) do
|
110
|
+
update_and_finish(instance_variable_get(:@object).send(field))
|
111
|
+
end
|
112
|
+
|
113
|
+
define_method(accessor_method.negative_check) do
|
114
|
+
update_and_continue(!instance_variable_get(:@object).send(field))
|
115
|
+
end
|
116
|
+
|
117
|
+
define_method(accessor_method.negative_check_with_result) do
|
118
|
+
update_and_finish(!instance_variable_get(:@object).send(field))
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module FluentConditions
|
2
|
+
class ConditionBuilder
|
3
|
+
def initialize(object, type)
|
4
|
+
@object = object
|
5
|
+
@type = type
|
6
|
+
@previous, @current, @big_or = true, true, false
|
7
|
+
end
|
8
|
+
|
9
|
+
def or
|
10
|
+
@or_used = true
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
def OR
|
15
|
+
update_big_or_result
|
16
|
+
@big_or_used = true
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
def and
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def update_and_continue(field_value)
|
27
|
+
update_current_result(field_value)
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
def update_and_finish(field_value)
|
32
|
+
update_current_result(field_value)
|
33
|
+
update_big_or_result if @big_or_used
|
34
|
+
end_result
|
35
|
+
end
|
36
|
+
|
37
|
+
def update_current_result(field_value)
|
38
|
+
if @or_used
|
39
|
+
@current = @current || field_value
|
40
|
+
@or_used = false
|
41
|
+
else
|
42
|
+
@previous, @current = @previous && @current, field_value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def update_big_or_result
|
47
|
+
@big_or = @big_or || current_result
|
48
|
+
@previous, @current = true, true
|
49
|
+
end
|
50
|
+
|
51
|
+
def current_result
|
52
|
+
@previous && @current
|
53
|
+
end
|
54
|
+
|
55
|
+
def end_result
|
56
|
+
@type == :positive ? sub_result : !sub_result
|
57
|
+
end
|
58
|
+
|
59
|
+
def sub_result
|
60
|
+
@big_or_used ? @big_or : current_result
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module FluentConditions
|
4
|
+
|
5
|
+
describe BooleanAccessorDefiner do
|
6
|
+
it "should add accessor methods" do
|
7
|
+
clazz = Class.new do
|
8
|
+
include FluentConditions
|
9
|
+
attr_accessor :admin
|
10
|
+
fluent :admin
|
11
|
+
end
|
12
|
+
@obj = clazz.new
|
13
|
+
|
14
|
+
@obj.is.should respond_to(:admin)
|
15
|
+
@obj.is.should respond_to(:admin?)
|
16
|
+
@obj.is.should respond_to(:not_admin)
|
17
|
+
@obj.is.should respond_to(:not_admin?)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe TextValueAccessorDefiner do
|
22
|
+
it "should add accessor methods" do
|
23
|
+
clazz = Class.new do
|
24
|
+
include FluentConditions
|
25
|
+
attr_accessor :color
|
26
|
+
fluent :color, :values => [:red, :green]
|
27
|
+
end
|
28
|
+
@product = clazz.new
|
29
|
+
|
30
|
+
@product.is.should respond_to(:red)
|
31
|
+
@product.is.should respond_to(:green)
|
32
|
+
@product.is.should respond_to(:not_red)
|
33
|
+
@product.is.should respond_to(:not_green)
|
34
|
+
|
35
|
+
@product.is.should respond_to(:red?)
|
36
|
+
@product.is.should respond_to(:green?)
|
37
|
+
@product.is.should respond_to(:not_red?)
|
38
|
+
@product.is.should respond_to(:not_green?)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe CustomAccessorDefiner do
|
43
|
+
it "should add accessor methods" do
|
44
|
+
clazz = Class.new do
|
45
|
+
include FluentConditions
|
46
|
+
attr_accessor :length
|
47
|
+
fluent :length, :as => :long, :if => proc { |length| length >= 500 }
|
48
|
+
fluent :length, :as => :short, :if => proc { |length| length < 500 }
|
49
|
+
end
|
50
|
+
@post = clazz.new
|
51
|
+
@post.is.should respond_to(:long)
|
52
|
+
@post.is.should respond_to(:not_long)
|
53
|
+
@post.is.should respond_to(:long?)
|
54
|
+
@post.is.should respond_to(:not_long?)
|
55
|
+
|
56
|
+
@post.is.should respond_to(:short)
|
57
|
+
@post.is.should respond_to(:not_short)
|
58
|
+
@post.is.should respond_to(:short?)
|
59
|
+
@post.is.should respond_to(:not_short?)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module FluentConditions
|
4
4
|
|
5
|
-
describe
|
5
|
+
describe ConditionBuilder do
|
6
6
|
|
7
7
|
describe "checking simple boolean conditions" do
|
8
8
|
before(:each) do
|
@@ -14,13 +14,6 @@ module FluentConditions
|
|
14
14
|
@obj = clazz.new
|
15
15
|
end
|
16
16
|
|
17
|
-
it "should respond to added methods" do
|
18
|
-
@obj.is.should respond_to(:admin)
|
19
|
-
@obj.is.should respond_to(:admin?)
|
20
|
-
@obj.is.should respond_to(:not_admin)
|
21
|
-
@obj.is.should respond_to(:not_admin?)
|
22
|
-
end
|
23
|
-
|
24
17
|
it "should check for admin user if it's admin or not" do
|
25
18
|
@obj.admin = true
|
26
19
|
@obj.is.admin?.should be_true
|
@@ -59,18 +52,27 @@ module FluentConditions
|
|
59
52
|
@obj.good = true
|
60
53
|
@obj.bad = true
|
61
54
|
@obj.is.good.bad?.should be_true
|
55
|
+
@obj.is.not_good.bad?.should be_false
|
56
|
+
@obj.is.good.not_bad?.should be_false
|
57
|
+
@obj.is.not_good.not_bad?.should be_false
|
62
58
|
end
|
63
59
|
|
64
60
|
it "should check two true/false conditions" do
|
65
61
|
@obj.good = true
|
66
62
|
@obj.bad = false
|
67
63
|
@obj.is.good.bad?.should be_false
|
64
|
+
@obj.is.not_good.bad?.should be_false
|
65
|
+
@obj.is.good.not_bad?.should be_true
|
66
|
+
@obj.is.not_good.not_bad?.should be_false
|
68
67
|
end
|
69
68
|
|
70
69
|
it "should check two false conditions" do
|
71
70
|
@obj.good = false
|
72
71
|
@obj.bad = false
|
73
72
|
@obj.is.good.bad?.should be_false
|
73
|
+
@obj.is.not_good.bad?.should be_false
|
74
|
+
@obj.is.good.not_bad?.should be_false
|
75
|
+
@obj.is.not_good.not_bad?.should be_true
|
74
76
|
end
|
75
77
|
end
|
76
78
|
|
@@ -153,28 +155,21 @@ module FluentConditions
|
|
153
155
|
@product = clazz.new
|
154
156
|
end
|
155
157
|
|
156
|
-
it "should respond to accesor methods" do
|
157
|
-
@product.is.should respond_to(:red)
|
158
|
-
@product.is.should respond_to(:green)
|
159
|
-
@product.is.should respond_to(:not_red)
|
160
|
-
@product.is.should respond_to(:not_green)
|
161
|
-
|
162
|
-
@product.is.should respond_to(:red?)
|
163
|
-
@product.is.should respond_to(:green?)
|
164
|
-
@product.is.should respond_to(:not_red?)
|
165
|
-
@product.is.should respond_to(:not_green?)
|
166
|
-
end
|
167
|
-
|
168
158
|
it "should check condition by value" do
|
169
159
|
@product.color = :red
|
170
160
|
|
171
161
|
@product.is.red?.should be_true
|
162
|
+
@product.is.not_red?.should be_false
|
163
|
+
|
172
164
|
@product.is.green?.should be_false
|
165
|
+
@product.is.not_green?.should be_true
|
166
|
+
|
173
167
|
@product.is.red.and.green?.should be_false
|
174
|
-
@product.is.
|
168
|
+
@product.is.not_red.and.green?.should be_false
|
169
|
+
@product.is.red.and.not_green?.should be_true
|
170
|
+
@product.is.not_red.and.not_green?.should be_false
|
175
171
|
|
176
|
-
@product.is.
|
177
|
-
@product.is.not_green?.should be_true
|
172
|
+
@product.is.red.or.green?.should be_true
|
178
173
|
end
|
179
174
|
end
|
180
175
|
|
@@ -189,25 +184,19 @@ module FluentConditions
|
|
189
184
|
@post = clazz.new
|
190
185
|
end
|
191
186
|
|
192
|
-
it "should respond to new methods" do
|
193
|
-
@post.is.should respond_to(:long)
|
194
|
-
@post.is.should respond_to(:not_long)
|
195
|
-
@post.is.should respond_to(:long?)
|
196
|
-
@post.is.should respond_to(:not_long?)
|
197
|
-
|
198
|
-
@post.is.should respond_to(:short)
|
199
|
-
@post.is.should respond_to(:not_short)
|
200
|
-
@post.is.should respond_to(:short?)
|
201
|
-
@post.is.should respond_to(:not_short?)
|
202
|
-
end
|
203
|
-
|
204
187
|
it "should check condition defined by user" do
|
205
188
|
@post.length = 600
|
206
189
|
|
207
190
|
@post.is.long?.should be_true
|
208
191
|
@post.is.not_long?.should be_false
|
192
|
+
|
209
193
|
@post.is.short?.should be_false
|
210
194
|
@post.is.short.or.long?.should be_true
|
195
|
+
|
196
|
+
@post.is.long.and.short?.should be_false
|
197
|
+
@post.is.long.and.not_short?.should be_true
|
198
|
+
@post.is.not_long.and.short?.should be_false
|
199
|
+
@post.is.not_long.and.not_short?.should be_false
|
211
200
|
end
|
212
201
|
end
|
213
202
|
|
@@ -20,8 +20,8 @@ module FluentConditions
|
|
20
20
|
@obj.should respond_to(:is)
|
21
21
|
end
|
22
22
|
|
23
|
-
it "should return
|
24
|
-
@obj.is.should be_kind_of(
|
23
|
+
it "should return ConditionBuilder object when calling 'is'" do
|
24
|
+
@obj.is.should be_kind_of(ConditionBuilder)
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should return new builder each time" do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fluent_conditions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,19 +9,42 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-09
|
12
|
+
date: 2011-11-09 00:00:00.000000000 +01:00
|
13
13
|
default_executable:
|
14
|
-
dependencies:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
requirement: &15565060 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *15565060
|
15
26
|
description:
|
16
27
|
email:
|
17
28
|
executables: []
|
18
29
|
extensions: []
|
19
30
|
extra_rdoc_files: []
|
20
31
|
files:
|
32
|
+
- .gitignore
|
33
|
+
- .rspec
|
34
|
+
- .travis.yml
|
35
|
+
- Gemfile
|
36
|
+
- MIT-LICENSE
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- fluent_conditions.gemspec
|
21
40
|
- lib/fluent_conditions.rb
|
22
|
-
-
|
41
|
+
- lib/fluent_conditions/accessor_definers.rb
|
42
|
+
- lib/fluent_conditions/condition_builder.rb
|
43
|
+
- lib/fluent_conditions/version.rb
|
44
|
+
- spec/fluent_conditions/accessor_definers_spec.rb
|
45
|
+
- spec/fluent_conditions/condition_builder_spec.rb
|
23
46
|
- spec/fluent_conditions/fluent_conditions_spec.rb
|
24
|
-
- spec/
|
47
|
+
- spec/spec_helper.rb
|
25
48
|
has_rdoc: true
|
26
49
|
homepage: http://github.com/pplcanfly/fluent_conditions
|
27
50
|
licenses: []
|