Class: PowerTrack::Rule

Inherits:
Object
  • Object
show all
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
DEFAULT_RULE_FEATURES =

The default rule features

{
  # no id by default
  id: nil,
  # no tag by default
  tag: nil,
  # long determined by value length
  long: nil
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, features = nil) ⇒ Rule

Builds a new rule based on a value and some optional features (:id, :tag, :long).

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 :long feature.



40
41
42
43
44
45
46
47
48
49
# File 'lib/powertrack/rules/rule.rb', line 40

def initialize(value, features=nil)
  @value = value || ''
  features = DEFAULT_RULE_FEATURES.merge(features || {})
  @tag = features[:tag]
  @id = features[:id]
  # check if long is a boolean
  _long = features[:long]
  @long = _long == !!_long ? _long : @value.size > MAX_STD_RULE_VALUE_LENGTH
  @error = nil
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



32
33
34
# File 'lib/powertrack/rules/rule.rb', line 32

def error
  @error
end

#idObject (readonly)

Returns the value of attribute id.



32
33
34
# File 'lib/powertrack/rules/rule.rb', line 32

def id
  @id
end

#tagObject (readonly)

Returns the value of attribute tag.



32
33
34
# File 'lib/powertrack/rules/rule.rb', line 32

def tag
  @tag
end

#valueObject (readonly)

Returns the value of attribute value.



32
33
34
# File 'lib/powertrack/rules/rule.rb', line 32

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.



98
99
100
101
102
103
# File 'lib/powertrack/rules/rule.rb', line 98

def ==(other)
  other.class == self.class &&
    other.value == @value &&
    other.tag == @tag &&
    other.long? == self.long?
end

#hashObject

Returns a hash for the rule based on its components. Useful for using rules as Hash keys.



109
110
111
112
# File 'lib/powertrack/rules/rule.rb', line 109

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.

Returns:

  • (Boolean)


52
53
54
# File 'lib/powertrack/rules/rule.rb', line 52

def long?
  @long
end

#max_value_lengthObject

Returns the maximum length of the rule value according to the type of the rule (long or standard).



116
117
118
# File 'lib/powertrack/rules/rule.rb', line 116

def max_value_length
  long? ? MAX_LONG_RULE_VALUE_LENGTH : MAX_STD_RULE_VALUE_LENGTH
end

#to_hashObject

Converts the rule in a Hash.



85
86
87
88
89
90
# File 'lib/powertrack/rules/rule.rb', line 85

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.



80
81
82
# File 'lib/powertrack/rules/rule.rb', line 80

def to_json(options={})
  MultiJson.encode(to_hash, options)
end

#to_sObject

Converts the rule in a string, the JSON representation of the rule actually.



93
94
95
# File 'lib/powertrack/rules/rule.rb', line 93

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.

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/powertrack/rules/rule.rb', line 58

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