criteria_operator 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +69 -2
- data/lib/criteria_operator/base_operator.rb +24 -1
- data/lib/criteria_operator/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1eaabc0ba95046a9d84d5b3232829539f29ce3e6
|
4
|
+
data.tar.gz: c63f6abba970c73c9c61bcd1b912f39f33e3f0fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de96f70089d90c409bc411eabcac52d1f239955fff519125013efd6f483ed27721d08a75f6eb181a3d534a1a74c1b06d24103081d917085eed514559f10a2e29
|
7
|
+
data.tar.gz: 6c40216bfe50a557a4e1e736079ad92690b4a23ed9c1cbe6c6fc44a92db9e3abfe3e3ae42cb7f076b81f59322e9e9d9f72bce241c89287aee5c288850577e10e
|
data/README.md
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
|
11
11
|
# CriteriaOperator
|
12
12
|
|
13
|
-
This gem provides classes to create arbitrary complex conditions, by building an expression tree.
|
13
|
+
This gem provides classes to create arbitrary complex conditions, by building an expression tree. Currently only basic operations are implemented.
|
14
14
|
|
15
15
|
## Installation
|
16
16
|
|
@@ -30,7 +30,72 @@ Or install it yourself as:
|
|
30
30
|
|
31
31
|
## Usage
|
32
32
|
|
33
|
-
|
33
|
+
For complete usage information, [read the docs](http://www.rubydoc.info/github/TheFlow0360/criteria_operator/master/frames)!
|
34
|
+
|
35
|
+
All relevant classes inherit from `BaseOperator`. Generally, you'll want to have a root node of this type, which will in turn contain the whole expression tree. Currently, there are the following operators:
|
36
|
+
|
37
|
+
### BinaryOperator
|
38
|
+
|
39
|
+
The `BinaryOperator` is the most commonly used. It represents a binary operation, meaning an operator with a left and right hand side operand, each. The operands can be any `BaseOperator`. The operator has to be one of the following types:
|
40
|
+
- Equal *(default)*
|
41
|
+
- Not Equal
|
42
|
+
- Greater
|
43
|
+
- Greater or Equal
|
44
|
+
- Less
|
45
|
+
- Less or Equal
|
46
|
+
|
47
|
+
These types are represented by constants in the module `BinaryOperatorType`.
|
48
|
+
|
49
|
+
Usage example:
|
50
|
+
|
51
|
+
# checks if the operands op1 and op2 are different
|
52
|
+
operator = CriteriaOperator::BinaryOperator.new op1, op2, CriteriaOperator::BinaryOperatorType::NOT_EQUAL
|
53
|
+
|
54
|
+
|
55
|
+
### GroupOperator
|
56
|
+
|
57
|
+
The `GroupOperator` is used to connect an amount of conditions (`BaseOperator`s) with either `AND` *(default)* or `OR` (see `GroupOperatorType`).
|
58
|
+
|
59
|
+
Usage example:
|
60
|
+
|
61
|
+
# checks if any of the conditions represented by operands op1 through op3 is true
|
62
|
+
operator = CriteriaOperator::GroupOperator.new [op1, op2, op3], CriteriaOperator::GroupOperatorType::OR
|
63
|
+
|
64
|
+
|
65
|
+
### UnaryOperator
|
66
|
+
|
67
|
+
The `UnaryOperator` is used to perform simple checks or transform a value represented by an operand (`BaseOperator`). It takes on operand as well and has one of the following types:
|
68
|
+
- NOT *(default)*
|
69
|
+
- IS NULL
|
70
|
+
- Plus
|
71
|
+
- Minus
|
72
|
+
|
73
|
+
These types can be found in the `UnaryOperatorType` module.
|
74
|
+
|
75
|
+
Usage example:
|
76
|
+
|
77
|
+
# invert the sign of the result returned by operand op1
|
78
|
+
operator = CriteriaOperator::UnaryOperator.new op1, CriteriaOperator::UnaryOperatorType::MINUS
|
79
|
+
|
80
|
+
|
81
|
+
### OperandProperty
|
82
|
+
|
83
|
+
The `OperandProperty` is, as the name implies, an operand. This means, it does not work with any other operands like operators do. If presented as a tree, operands always are leaves. The `OperandProperty` describes a property through it's name.
|
84
|
+
|
85
|
+
Usage example:
|
86
|
+
|
87
|
+
# represents a column named 'integer_value' (without quotes)
|
88
|
+
operand = CriteriaOperator::OperandProperty.new 'integer_value'
|
89
|
+
|
90
|
+
|
91
|
+
### OperandValue
|
92
|
+
|
93
|
+
The `OperandValue` is, just like the `OperandProperty`, an operand. It represents any kind of value.
|
94
|
+
|
95
|
+
Usage example:
|
96
|
+
|
97
|
+
# represents the numeric value forty-two
|
98
|
+
operand = CriteriaOperator::OperandValue.new 42
|
34
99
|
|
35
100
|
## Development
|
36
101
|
|
@@ -42,6 +107,8 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
42
107
|
|
43
108
|
Bug reports and pull requests are welcome on GitHub at https://github.com/TheFlow0360/criteria_operator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
44
109
|
|
110
|
+
Prerequisite to the acceptance of any pull requests is a successful build (this will be checked automatically) as well as test coverage and complete documentation.
|
111
|
+
|
45
112
|
|
46
113
|
## License
|
47
114
|
|
@@ -14,10 +14,33 @@ module CriteriaOperator
|
|
14
14
|
raise NotImplementedError
|
15
15
|
end
|
16
16
|
|
17
|
+
# Returns a string representation of the operator (including all sub-operators).
|
18
|
+
# YAML is used for serialization.
|
19
|
+
# @return [String] The serialized operator.
|
20
|
+
def serialize
|
21
|
+
BaseOperator.serialize(self)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns a string representation of an operator (including all sub-operators).
|
25
|
+
# YAML is used for serialization.
|
26
|
+
# @param [BaseOperator] op The operator to serialize.
|
27
|
+
# @return [String] The serialized operator.
|
28
|
+
def self.serialize(op)
|
29
|
+
YAML.dump(op)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Deserializes an operator from a string.
|
33
|
+
# String must be YAML-serialized.
|
34
|
+
# @param [String] serialized_op The serialized operator.
|
35
|
+
# @return [BaseOperator] The deserialized operator.
|
36
|
+
def self.deserialize(serialized_op)
|
37
|
+
YAML.safe_load(serialized_op, ObjectSpace.each_object(Class).select { |klass| klass < BaseOperator })
|
38
|
+
end
|
39
|
+
|
17
40
|
protected
|
18
41
|
|
19
42
|
# Clones the passed operator if it isn't nil.
|
20
|
-
# @param [BaseOperator] op
|
43
|
+
# @param [BaseOperator] op The operator to clone.
|
21
44
|
# @return [BaseOperator, nil] The cloned base operator, if it exists, or nil, otherwise.
|
22
45
|
def clone_or_nil(op)
|
23
46
|
return nil if op.nil? || !op.is_a?(BaseOperator)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: criteria_operator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Florian Koch
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-07-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|