Class: KeyVortex::Limitation
- Inherits:
-
Object
- Object
- KeyVortex::Limitation
- Defined in:
- lib/key_vortex/limitation.rb
Overview
Represents a collection of constraints which apply to a given class. While these can be constructed directly, the intended mechanism for applying these to a record is through Record.field.
Two independent concepts which commonly use the same terminology exist in this project. Consider the term “allows”, is that referring to what values are allowed or what constraints are allowed? To help navigate these two concepts, the following terminology convention is used throughout this codebase:
-
Encompass/Within: Used when comparing limitations and constraints to each other
-
Accepts/Rejects: Used when determining if a value is valid for constraints
Instance Attribute Summary collapse
-
#constraints ⇒ Array
readonly
The constraints which apply to this class.
-
#type ⇒ Class
readonly
The class these constraints applies to.
Instance Method Summary collapse
-
#accepts?(value) ⇒ Boolean
Determine if a given value meets all of the constraints.
-
#add_constraint(*constraints) ⇒ Object
Add constraints to this limitation.
-
#enable_json_additions ⇒ Object
Ensure that JSON additions for #type are loaded in case the record needs to be serialized.
-
#initialize(type, *constraints) ⇒ Limitation
constructor
A new instance of Limitation.
-
#to_s ⇒ String
Return information about this limitation.
-
#within?(limitation) ⇒ Boolean
Determine if any of the constraints in the provided limitation exceed this limitation’s constraints.
Constructor Details
#initialize(type, *constraints) ⇒ Limitation
Returns a new instance of Limitation.
25 26 27 28 29 |
# File 'lib/key_vortex/limitation.rb', line 25 def initialize(type, *constraints) @type = type @constraints = [] add_constraint(*constraints) end |
Instance Attribute Details
#constraints ⇒ Array (readonly)
Returns The constraints which apply to this class.
21 22 23 |
# File 'lib/key_vortex/limitation.rb', line 21 def constraints @constraints end |
#type ⇒ Class (readonly)
Returns The class these constraints applies to.
18 19 20 |
# File 'lib/key_vortex/limitation.rb', line 18 def type @type end |
Instance Method Details
#accepts?(value) ⇒ Boolean
Determine if a given value meets all of the constraints.
59 60 61 |
# File 'lib/key_vortex/limitation.rb', line 59 def accepts?(value) value.is_a?(type) && @constraints.all? { |constraint| constraint.accepts?(value) } end |
#add_constraint(*constraints) ⇒ Object
Add constraints to this limitation. It is also possible to do this through the constructor, but it is sometimes easier to do it one at a time.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/key_vortex/limitation.rb', line 35 def add_constraint(*constraints) constraints.each do |constraint| unless constraint.is_a?(KeyVortex::Constraint::Base) raise KeyVortex::Error, "Not a constraint: #{constraint.class}" end end @constraints += constraints end |
#enable_json_additions ⇒ Object
Ensure that JSON additions for #type are loaded in case the record needs to be serialized.
65 66 67 68 |
# File 'lib/key_vortex/limitation.rb', line 65 def enable_json_additions path = JSON_ADDITIONS[@type.class.name] require path if path end |
#to_s ⇒ String
Return information about this limitation. The information will be formatted as follows:
Limitation: String
minimum: 10
maximum: 100
76 77 78 |
# File 'lib/key_vortex/limitation.rb', line 76 def to_s "Limitation: #{@type}\n\t#{@constraints.join('\n\t')}" end |
#within?(limitation) ⇒ Boolean
Determine if any of the constraints in the provided limitation exceed this limitation’s constraints.
50 51 52 53 54 |
# File 'lib/key_vortex/limitation.rb', line 50 def within?(limitation) limitation.constraints.all? do |constraint| within_constraint?(constraint) end end |