Class: Disposable::Twin::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/disposable/twin/schema.rb

Overview

TODO: this needs tests and should probably go to Representable. we can move tests from Reform for that.

Class Method Summary collapse

Class Method Details

.from(source_class, options) ⇒ Object

TODO: can we re-use this for all the decorator logic in #validate, etc?



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/disposable/twin/schema.rb', line 3

def self.from(source_class, options) # TODO: can we re-use this for all the decorator logic in #validate, etc?
  representer = Class.new(options[:superclass])
  representer.send :include, *options[:include]

  source_representer = options[:representer_from].call(source_class)

  source_representer.representable_attrs.each do |dfn|
    local_options = dfn[options[:options_from]] || {} # e.g. deserializer: {..}.
    new_options   = dfn.instance_variable_get(:@options).merge(local_options)

    from_scalar!(options, dfn, new_options, representer) && next unless dfn[:extend]
    from_inline!(options, dfn, new_options, representer)
  end

  representer
end