Class: PowerTrack::Rule
- Inherits:
-
Object
- Object
- PowerTrack::Rule
- Defined in:
- lib/powertrack/rules/rule.rb
Overview
A PowerTrack rule with its components and restrictions.
Constant Summary collapse
- MAX_TAG_LENGTH =
The maximum length of a rule tag.
255- MAX_STD_RULE_VALUE_LENGTH =
The maximum lengh of the value of a standard rule
1024- MAX_LONG_RULE_VALUE_LENGTH =
The maximum lengh of the value of a long rule
2048- MAX_POSITIVE_TERMS =
The maximum number of positive terms in a single rule value
30- MAX_NEGATIVE_TERMS =
The maximum number of negative terms in a single rule value
50
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Returns true when the rule is equal to the other rule provided.
-
#hash ⇒ Object
Returns a hash for the rule based on its components.
-
#initialize(value, tag = nil, long = nil) ⇒ Rule
constructor
Builds a new rule based on a value and an optional tag.
-
#long? ⇒ Boolean
Returns true if the rule is long.
-
#max_value_length ⇒ Object
Returns the maximum length of the rule value according to the type of the rule (long or standard).
-
#to_hash ⇒ Object
Converts the rule in a Hash.
-
#to_json(options = {}) ⇒ Object
Dumps the rule in a valid JSON format.
-
#to_s ⇒ Object
Converts the rule in a string, the JSON representation of the rule actually.
-
#valid? ⇒ Boolean
Returns true if the rule is valid, false otherwise.
Constructor Details
#initialize(value, tag = nil, long = nil) ⇒ Rule
Builds a new rule based on a value and an optional tag. By default, the constructor assesses if it’s a long rule or not based on the length of the value. But the ‘long’ feature can be explicitly specified with the third parameter.
28 29 30 31 32 33 34 |
# File 'lib/powertrack/rules/rule.rb', line 28 def initialize(value, tag=nil, long=nil) @value = value || '' @tag = tag # check if long is a boolean @long = long == !!long ? long : @value.size > MAX_STD_RULE_VALUE_LENGTH @error = nil end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
22 23 24 |
# File 'lib/powertrack/rules/rule.rb', line 22 def error @error end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
22 23 24 |
# File 'lib/powertrack/rules/rule.rb', line 22 def tag @tag end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
22 23 24 |
# File 'lib/powertrack/rules/rule.rb', line 22 def value @value end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
Returns true when the rule is equal to the other rule provided.
82 83 84 85 86 87 |
# File 'lib/powertrack/rules/rule.rb', line 82 def ==(other) other.class == self.class && other.value == @value && other.tag == @tag && other.long? == self.long? end |
#hash ⇒ Object
Returns a hash for the rule based on its components. Useful for using rules as Hash keys.
93 94 95 96 |
# File 'lib/powertrack/rules/rule.rb', line 93 def hash # let's assume a nil value for @value or @tag is not different from the empty value "v:#{@value},t:#{@tag},l:#{@long}".hash end |
#long? ⇒ Boolean
Returns true if the rule is long.
37 38 39 |
# File 'lib/powertrack/rules/rule.rb', line 37 def long? @long end |
#max_value_length ⇒ Object
Returns the maximum length of the rule value according to the type of the rule (long or standard).
100 101 102 |
# File 'lib/powertrack/rules/rule.rb', line 100 def max_value_length long? ? MAX_LONG_RULE_VALUE_LENGTH : MAX_STD_RULE_VALUE_LENGTH end |
#to_hash ⇒ Object
Converts the rule in a Hash.
70 71 72 73 74 |
# File 'lib/powertrack/rules/rule.rb', line 70 def to_hash res = {:value => @value} res[:tag] = @tag unless @tag.nil? res end |
#to_json(options = {}) ⇒ Object
Dumps the rule in a valid JSON format.
65 66 67 |
# File 'lib/powertrack/rules/rule.rb', line 65 def to_json(={}) MultiJson.encode(to_hash, ) end |
#to_s ⇒ Object
Converts the rule in a string, the JSON representation of the rule actually.
77 78 79 |
# File 'lib/powertrack/rules/rule.rb', line 77 def to_s to_json end |
#valid? ⇒ Boolean
Returns true if the rule is valid, false otherwise. The validation error can be through the error method.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/powertrack/rules/rule.rb', line 43 def valid? # reset error @error = nil [ :too_long_value?, :too_many_positive_terms?, :too_many_negative_terms?, :contains_empty_source?, :contains_negated_or?, :too_long_tag? ].each do |validator| # stop when 1 validator fails if self.send(validator) @error = validator.to_s.gsub(/_/, ' ').gsub(/\?/, '').capitalize return false end end true end |