Class: KeyVortex::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/key_vortex/field.rb

Overview

Defines a value that can be set on a Record. This generally isn’t built directly, you should use the Record.field directive most of the time. This class ties a name to a Limitation, and delegates most questions on to the latter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, *constraints_array, **constraints_hash) ⇒ Field

Creates an instance of this class and converts any attribute/limit constraint pairs into Constraint objects.

Parameters:

  • name (Symbol)

    Name of the field

  • type (Class)

    Type used for limitation

  • constraints_array (Constraint::Base)

    Any constraints already constructed can be passed in as well

  • constraints_hash (Hash)

    key/value pairs that will be passed on to Constraint.build



24
25
26
27
28
29
30
31
32
# File 'lib/key_vortex/field.rb', line 24

def initialize(name, type, *constraints_array, **constraints_hash)
  @name = name
  @limitation = KeyVortex::Limitation.new(type)

  @limitation.add_constraint(*constraints_array)
  @limitation.add_constraint(*constraints_hash.map do |attribute, value|
    KeyVortex::Constraint.build(attribute, value)
  end)
end

Instance Attribute Details

#limitationLimitation (readonly)

Returns the restrictions placed upon this field.

Returns:

  • (Limitation)

    the restrictions placed upon this field



15
16
17
# File 'lib/key_vortex/field.rb', line 15

def limitation
  @limitation
end

#nameSymbol (readonly)

Returns the name of the field.

Returns:

  • (Symbol)

    the name of the field



13
14
15
# File 'lib/key_vortex/field.rb', line 13

def name
  @name
end

Instance Method Details

#accepts?(value) ⇒ Boolean

Returns true if the value is valid for #limitation.

Parameters:

  • value (Object)

Returns:

  • (Boolean)

    true if the value is valid for #limitation



43
44
45
# File 'lib/key_vortex/field.rb', line 43

def accepts?(value)
  limitation.accepts?(value)
end

#enable_json_additionsObject

Enable JSON additions for the class used for this field.



48
49
50
# File 'lib/key_vortex/field.rb', line 48

def enable_json_additions
  @limitation.enable_json_additions
end

#within?(adapter) ⇒ Boolean

Returns true if the limitations for this field fit within the corresponding limitations for the adapter.

Parameters:

Returns:

  • (Boolean)

    true if the limitations for this field fit within the corresponding limitations for the adapter



36
37
38
39
# File 'lib/key_vortex/field.rb', line 36

def within?(adapter)
  limitation = adapter.limitation_for(self)
  !limitation || self.limitation.within?(limitation)
end