Sha256: 246a9e74f93e025a54e19117db8c7db392bf9895d6a31ab2e2aae6ef6f610dc9

Contents?: true

Size: 1.22 KB

Versions: 10

Compression:

Stored size: 1.22 KB

Contents

# TODO: this needs tests and should probably go to Representable. we can move tests from Reform for that.
class Disposable::Twin::Schema
  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

private
  def self.from_scalar!(options, dfn, new_options, representer)
    representer.property(dfn.name, new_options)
  end

  def self.from_inline!(options, dfn, new_options, representer)
    nested      = dfn[:extend].evaluate(nil) # nested now can be a Decorator, a representer module, a Form, a Twin.
    dfn_options = new_options.merge(extend: from(nested, options))

    representer.property(dfn.name, dfn_options)
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
disposable-0.1.9 lib/disposable/twin/schema.rb
disposable-0.1.8 lib/disposable/twin/schema.rb
disposable-0.1.7 lib/disposable/twin/schema.rb
disposable-0.1.6 lib/disposable/twin/schema.rb
disposable-0.1.5 lib/disposable/twin/schema.rb
disposable-0.1.4 lib/disposable/twin/schema.rb
disposable-0.1.3 lib/disposable/twin/schema.rb
disposable-0.1.2 lib/disposable/twin/schema.rb
disposable-0.1.1 lib/disposable/twin/schema.rb
disposable-0.1.0 lib/disposable/twin/schema.rb