Class: Trax::Model::Attributes::Types::Set

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

Defined Under Namespace

Classes: TypeCaster, Value

Class Method Summary collapse

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

inherited

Class Method Details

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



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

def self.define_attribute(klass, attribute_name, **options, &block)
  klass_name = "#{klass.fields_module.name.underscore}/#{attribute_name}".camelize
  attribute_klass = if options.key?(:extend)
    _klass_prototype = options[:extend].is_a?(::String) ? options[:extend].safe_constantize : options[:extend]
    _klass = ::Trax::Core::NamedClass.new(klass_name, _klass_prototype, :parent_definition => klass, &block)
    _klass
  else
    ::Trax::Core::NamedClass.new(klass_name, Value, :parent_definition => klass, &block)
  end

  klass.attribute(attribute_name, typecaster_klass.new(target_klass: attribute_klass))
  klass.default_value_for(attribute_name) { attribute_klass.new }
end