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_RULE_VALUE_LENGTH =
The maximum lengh of the value of a 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
- MAX_RULES_BODY_SIZE =
The maximum size of the HTTP body accepted by PowerTrack /rules calls (in bytes) 5MB since v2
5*1024**2
- DEFAULT_RULE_FEATURES =
The default rule features
{ # no id by default id: nil, # no tag by default tag: nil }.freeze
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#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, features = nil) ⇒ Rule
constructor
Builds a new rule based on a value and some optional features (:id, :tag).
-
#max_value_length ⇒ Object
Returns the maximum length of the rule value.
-
#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, features = nil) ⇒ Rule
Builds a new rule based on a value and some optional features (:id, :tag).
35 36 37 38 39 40 41 |
# File 'lib/powertrack/rules/rule.rb', line 35 def initialize(value, features=nil) @value = value || '' features = DEFAULT_RULE_FEATURES.merge(features || {}) @tag = features[:tag] @id = features[:id] @error = nil end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
31 32 33 |
# File 'lib/powertrack/rules/rule.rb', line 31 def error @error end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
31 32 33 |
# File 'lib/powertrack/rules/rule.rb', line 31 def id @id end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
31 32 33 |
# File 'lib/powertrack/rules/rule.rb', line 31 def tag @tag end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
31 32 33 |
# File 'lib/powertrack/rules/rule.rb', line 31 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.
89 90 91 92 93 |
# File 'lib/powertrack/rules/rule.rb', line 89 def ==(other) other.class == self.class && other.value == @value && other.tag == @tag end |
#hash ⇒ Object
Returns a hash for the rule based on its components. Useful for using rules as Hash keys.
99 100 101 102 |
# File 'lib/powertrack/rules/rule.rb', line 99 def hash # let's assume a nil value for @value or @tag is not different from the empty value "v:#{@value},t:#{@tag}".hash end |
#max_value_length ⇒ Object
Returns the maximum length of the rule value.
105 106 107 |
# File 'lib/powertrack/rules/rule.rb', line 105 def max_value_length MAX_RULE_VALUE_LENGTH end |
#to_hash ⇒ Object
Converts the rule in a Hash.
76 77 78 79 80 81 |
# File 'lib/powertrack/rules/rule.rb', line 76 def to_hash res = {:value => @value} res[:tag] = @tag unless @tag.nil? res[:id] = @id unless @id.nil? res end |
#to_json(options = {}) ⇒ Object
Dumps the rule in a valid JSON format.
71 72 73 |
# File 'lib/powertrack/rules/rule.rb', line 71 def to_json(={}) MultiJson.encode(to_hash, ) end |
#to_s ⇒ Object
Converts the rule in a string, the JSON representation of the rule actually.
84 85 86 |
# File 'lib/powertrack/rules/rule.rb', line 84 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.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/powertrack/rules/rule.rb', line 45 def valid? # reset error @error = nil validation_rules = [ :too_long_value?, :contains_empty_source?, :contains_negated_or?, :too_long_tag?, :contains_explicit_and?, :contains_lowercase_or?, :contains_explicit_not? ] validation_rules.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 |