Class: ApacheAge::Types::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/apache_age/types/factory.rb

Class Method Summary collapse

Class Method Details

.type_for(klass) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/apache_age/types/factory.rb', line 26

def self.type_for(klass)
  Class.new(ActiveModel::Type::Value) do
    define_method(:cast) do |value|
      case value
      when klass
        value
      when Hash
        klass.new(value)
      else
        nil
      end
    end

    define_method(:serialize) do |value|
      value.is_a?(klass) ? value.attributes : nil
    end
  end
end