Class: Trax::Model::Attributes::Types::Boolean

Inherits:
Trax::Model::Attributes::Type show all
Defined in:
lib/trax/model/attributes/types/boolean.rb

Defined Under Namespace

Classes: Attribute, TypeCaster

Class Method Summary collapse

Methods inherited from Trax::Model::Attributes::Type

inherited

Class Method Details

.define_attribute(klass, attribute_name, **options, &block) ⇒ Object

this will by default validate boolean values



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/trax/model/attributes/types/boolean.rb', line 7

def self.define_attribute(klass, attribute_name, **options, &block)
  klass_name = "#{klass.fields_module.name.underscore}/#{attribute_name}".camelize
  attribute_klass = if options.key?(:class_name)
    options[:class_name].constantize
  else
    ::Trax::Core::NamedClass.new(klass_name, ::Trax::Model::Attributes[:boolean]::Attribute, :parent_definition => klass, &block)
  end

  klass.attribute(attribute_name, ::Trax::Model::Attributes::Types::Boolean::TypeCaster.new)
  klass.validates(attribute_name, :boolean => true) unless options.key?(:validate) && !options[:validate]
  klass.default_value_for(attribute_name) { options[:default] } if options.key?(:default)
end