Module: Yt::Associations::HasAttribute::ClassMethods

Defined in:
lib/yt/associations/has_attribute.rb

Instance Method Summary collapse

Instance Method Details

#has_attribute(attribute, options = {}, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/yt/associations/has_attribute.rb', line 23

def has_attribute(attribute, options = {}, &block)
  define_memoized_method(attribute) do
    field = options.fetch(:from, attribute).to_s
    field = field.camelize(:lower) if options.fetch(:camelize, true)
    value = @data.fetch field, options[:default]
    value = type_cast value, options[:type] if options[:type]
    block_given? ? instance_exec(value, &block) : value
  end
end