Class: Arkaan::Rulesets::Field
- Inherits:
-
Object
- Object
- Arkaan::Rulesets::Field
- Includes:
- Concerns::Enumerable, Mongoid::Document, Mongoid::Timestamps
- Defined in:
- lib/arkaan/rulesets/field.rb
Overview
A field is an attribute of a blueprint, with a type and a name. It does not have a defined value as it will be given in the instance of the blueprint.
Direct Known Subclasses
Arkaan::Rulesets::Fields::Gauge, Arkaan::Rulesets::Fields::Integer
Instance Attribute Summary collapse
-
#blueprint ⇒ Arkaan::Rulesets::Blueprint
The blueprint in which the field belongs.
-
#data ⇒ Hash
The additional data, mainly constraints and needed values, for the field.
-
#name ⇒ String
The name of the field is comparable to the name of a variable.
Instance Method Summary collapse
-
#check_type(key, required_type) ⇒ Object
Checks the corresponding option key against the given data type.
-
#default_options ⇒ Hash
Default options for this type of field, specialize it in the subclasses.
- #name_unicity ⇒ Object
- #options_validity ⇒ Object
-
#type ⇒ Symbol
Getter for the type of the field, returning simply the last element of the type for simpler use.
Instance Attribute Details
#blueprint ⇒ Arkaan::Rulesets::Blueprint
Returns the blueprint in which the field belongs.
20 |
# File 'lib/arkaan/rulesets/field.rb', line 20 :blueprint, class_name: 'Arkaan::Rulesets::Blueprint', inverse_of: :_fields |
#data ⇒ Hash
Returns the additional data, mainly constraints and needed values, for the field.
16 |
# File 'lib/arkaan/rulesets/field.rb', line 16 field :data, type: Hash, default: {} |
#name ⇒ String
Returns the name of the field is comparable to the name of a variable.
13 |
# File 'lib/arkaan/rulesets/field.rb', line 13 field :name, type: String |
Instance Method Details
#check_type(key, required_type) ⇒ Object
Checks the corresponding option key against the given data type. All types can be used.
63 64 65 66 67 68 |
# File 'lib/arkaan/rulesets/field.rb', line 63 def check_type(key, required_type) parsed_type = Object.const_get("::#{required_type}") if !errors..has_key?(:data) && data[key.to_sym] && !data[key.to_sym].is_a?(parsed_type) errors.add(:data, "#{key.to_s}|type") end end |
#default_options ⇒ Hash
Default options for this type of field, specialize it in the subclasses.
39 40 41 |
# File 'lib/arkaan/rulesets/field.rb', line 39 def return {} end |
#name_unicity ⇒ Object
49 50 51 52 53 54 |
# File 'lib/arkaan/rulesets/field.rb', line 49 def name_unicity has_duplicate = blueprint._fields.where(:_id.ne => _id, name: name).exists? if name? && blueprint && has_duplicate errors.add(:name, 'uniq') end end |
#options_validity ⇒ Object
56 57 58 |
# File 'lib/arkaan/rulesets/field.rb', line 56 def send(:validate_options) rescue true end |
#type ⇒ Symbol
Getter for the type of the field, returning simply the last element of the type for simpler use.
33 34 35 |
# File 'lib/arkaan/rulesets/field.rb', line 33 def type return _type.split('::').last.to_sym end |