Class: Arkaan::Rulesets::Field

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

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#blueprintArkaan::Rulesets::Blueprint

Returns the blueprint in which the field belongs.

Returns:



20
# File 'lib/arkaan/rulesets/field.rb', line 20

embedded_in :blueprint, class_name: 'Arkaan::Rulesets::Blueprint', inverse_of: :_fields

#dataHash

Returns the additional data, mainly constraints and needed values, for the field.

Returns:

  • (Hash)

    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: {}

#nameString

Returns the name of the field is comparable to the name of a variable.

Returns:

  • (String)

    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.

Parameters:

  • key (String)

    the name of the key in the options you want to check the type of.

  • required_type (String)

    the exact name of the class you want to check the option against.



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.messages.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_optionsHash

Default options for this type of field, specialize it in the subclasses.

Returns:

  • (Hash)

    a hash with the default value for all options you want default values on.



39
40
41
# File 'lib/arkaan/rulesets/field.rb', line 39

def default_options
  return {}
end

#name_unicityObject



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_validityObject



56
57
58
# File 'lib/arkaan/rulesets/field.rb', line 56

def options_validity
  send(:validate_options) rescue true
end

#typeSymbol

Getter for the type of the field, returning simply the last element of the type for simpler use.

Returns:

  • (Symbol)

    the name of the type of the field (eq :Integer or :Gauge)



33
34
35
# File 'lib/arkaan/rulesets/field.rb', line 33

def type
  return _type.split('::').last.to_sym
end