Class: Unleash::Constraint
- Inherits:
-
Object
- Object
- Unleash::Constraint
- Defined in:
- lib/unleash/constraint.rb
Constant Summary collapse
- OPERATORS =
{ IN: ->(context_v, constraint_v){ constraint_v.include? context_v }, NOT_IN: ->(context_v, constraint_v){ !constraint_v.include? context_v }, STR_STARTS_WITH: ->(context_v, constraint_v){ constraint_v.any?{ |v| context_v.start_with? v } }, STR_ENDS_WITH: ->(context_v, constraint_v){ constraint_v.any?{ |v| context_v.end_with? v } }, STR_CONTAINS: ->(context_v, constraint_v){ constraint_v.any?{ |v| context_v.include? v } }, NUM_EQ: ->(context_v, constraint_v){ on_valid_float(constraint_v, context_v){ |x, y| (x - y).abs < Float::EPSILON } }, NUM_LT: ->(context_v, constraint_v){ on_valid_float(constraint_v, context_v){ |x, y| (x > y) } }, NUM_LTE: ->(context_v, constraint_v){ on_valid_float(constraint_v, context_v){ |x, y| (x >= y) } }, NUM_GT: ->(context_v, constraint_v){ on_valid_float(constraint_v, context_v){ |x, y| (x < y) } }, NUM_GTE: ->(context_v, constraint_v){ on_valid_float(constraint_v, context_v){ |x, y| (x <= y) } }, DATE_AFTER: ->(context_v, constraint_v){ on_valid_date(constraint_v, context_v){ |x, y| (x < y) } }, DATE_BEFORE: ->(context_v, constraint_v){ on_valid_date(constraint_v, context_v){ |x, y| (x > y) } }, SEMVER_EQ: ->(context_v, constraint_v){ on_valid_version(constraint_v, context_v){ |x, y| (x == y) } }, SEMVER_GT: ->(context_v, constraint_v){ on_valid_version(constraint_v, context_v){ |x, y| (x < y) } }, SEMVER_LT: ->(context_v, constraint_v){ on_valid_version(constraint_v, context_v){ |x, y| (x > y) } } }.freeze
- LIST_OPERATORS =
[:IN, :NOT_IN, :STR_STARTS_WITH, :STR_ENDS_WITH, :STR_CONTAINS].freeze
Instance Attribute Summary collapse
-
#case_insensitive ⇒ Object
Returns the value of attribute case_insensitive.
-
#context_name ⇒ Object
Returns the value of attribute context_name.
-
#inverted ⇒ Object
Returns the value of attribute inverted.
-
#operator ⇒ Object
Returns the value of attribute operator.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
- .on_valid_date(val1, val2) ⇒ Object
- .on_valid_float(val1, val2) ⇒ Object
- .on_valid_version(val1, val2) ⇒ Object
Instance Method Summary collapse
-
#initialize(context_name, operator, value = [], inverted: false, case_insensitive: false) ⇒ Constraint
constructor
A new instance of Constraint.
- #matches_context?(context) ⇒ Boolean
-
#validate_constraint_value_type(operator, value) ⇒ Object
This should be a private method but for some reason this fails on Ruby 2.5.
Constructor Details
#initialize(context_name, operator, value = [], inverted: false, case_insensitive: false) ⇒ Constraint
Returns a new instance of Constraint.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/unleash/constraint.rb', line 27 def initialize(context_name, operator, value = [], inverted: false, case_insensitive: false) raise ArgumentError, "context_name is not a String" unless context_name.is_a?(String) raise ArgumentError, "operator does not hold a valid value:" + OPERATORS.keys unless OPERATORS.include? operator.to_sym self.validate_constraint_value_type(operator.to_sym, value) self.context_name = context_name self.operator = operator.to_sym self.value = value self.inverted = !!inverted self.case_insensitive = !!case_insensitive end |
Instance Attribute Details
#case_insensitive ⇒ Object
Returns the value of attribute case_insensitive.
5 6 7 |
# File 'lib/unleash/constraint.rb', line 5 def case_insensitive @case_insensitive end |
#context_name ⇒ Object
Returns the value of attribute context_name.
5 6 7 |
# File 'lib/unleash/constraint.rb', line 5 def context_name @context_name end |
#inverted ⇒ Object
Returns the value of attribute inverted.
5 6 7 |
# File 'lib/unleash/constraint.rb', line 5 def inverted @inverted end |
#operator ⇒ Object
Returns the value of attribute operator.
5 6 7 |
# File 'lib/unleash/constraint.rb', line 5 def operator @operator end |
#value ⇒ Object
Returns the value of attribute value.
5 6 7 |
# File 'lib/unleash/constraint.rb', line 5 def value @value end |
Class Method Details
.on_valid_date(val1, val2) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/unleash/constraint.rb', line 51 def self.on_valid_date(val1, val2) val1 = DateTime.parse(val1) val2 = DateTime.parse(val2) yield(val1, val2) rescue ArgumentError Unleash.logger.warn "Unleash::ConstraintMatcher unable to parse either context_value (#{val1}) \ or constraint_value (#{val2}) into a date. Returning false!" false end |
.on_valid_float(val1, val2) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/unleash/constraint.rb', line 61 def self.on_valid_float(val1, val2) val1 = Float(val1) val2 = Float(val2) yield(val1, val2) rescue ArgumentError Unleash.logger.warn "Unleash::ConstraintMatcher unable to parse either context_value (#{val1}) \ or constraint_value (#{val2}) into a number. Returning false!" false end |
.on_valid_version(val1, val2) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/unleash/constraint.rb', line 71 def self.on_valid_version(val1, val2) val1 = Gem::Version.new(val1) val2 = Gem::Version.new(val2) yield(val1, val2) rescue ArgumentError Unleash.logger.warn "Unleash::ConstraintMatcher unable to parse either context_value (#{val1}) \ or constraint_value (#{val2}) into a version. Return false!" false end |
Instance Method Details
#matches_context?(context) ⇒ Boolean
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/unleash/constraint.rb', line 40 def matches_context?(context) Unleash.logger.debug "Unleash::Constraint matches_context? value: #{self.value} context.get_by_name(#{self.context_name})" \ " #{context.get_by_name(self.context_name)} " match = matches_constraint?(context) self.inverted ? !match : match rescue KeyError Unleash.logger.warn "Attemped to resolve a context key during constraint resolution: #{self.context_name} but it wasn't \ found on the context" false end |
#validate_constraint_value_type(operator, value) ⇒ Object
This should be a private method but for some reason this fails on Ruby 2.5
82 83 84 85 |
# File 'lib/unleash/constraint.rb', line 82 def validate_constraint_value_type(operator, value) raise ArgumentError, "context_name is not an Array" if LIST_OPERATORS.include?(operator) && value.is_a?(String) raise ArgumentError, "context_name is not a String" if !LIST_OPERATORS.include?(operator) && value.is_a?(Array) end |