Sha256: 1edb1ccf042d576439135104bf5f86eb5b0987137a139789eb64fbf4d74859e3

Contents?: true

Size: 1.82 KB

Versions: 8

Compression:

Stored size: 1.82 KB

Contents

require 'disposable/composition'

# TODO: replace that with lazy Twin and Composition from Disposable.
module Reform
  class Expose
    include Disposable::Composition

    # DISCUSS: this might be moved to Disposable::Twin::Expose.
    class << self
      # Builder for a concrete Composition class with configurations from the form's representer.
      def from(representer)
        options = {}

        representer.representable_attrs.each do |definition|
          process_definition!(options, definition)
        end

        Class.new(self).tap do |composition| # for 1.8 compat. you're welcome.
          composition.map(options)
          # puts composition@map.inspect
        end
      end

    private
      def process_definition!(options, definition)
        options[:model] ||= []
        options[:model] << [definition[:private_name], definition.name].compact
      end
    end
  end

  # Keeps composition of models and knows how to transform a plain hash into a nested hash.
  class Composition < Expose

    # DISCUSS: this might be moved to Disposable::Twin::Composition.
    class << self
      # Builder for a concrete Composition class with configurations from the form's representer.
      def process_definition!(options, definition)
        options[definition[:on]] ||= []
        options[definition[:on]] << [definition[:private_name], definition.name].compact
      end
    end

    def save
      each.collect { |model| model.save }.all?
    end

    def nested_hash_for(attrs)
      {}.tap do |hsh|
        attrs.each do |name, val|
          #obj = self.class.model_for_property(name)
          config = self.class.instance_variable_get(:@map)[name.to_sym]

          model  = config[:model]
          method = config[:method]

          hsh[model] ||= {}
          hsh[model][method] = val
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
reform-1.2.6 lib/reform/composition.rb
reform-1.2.5 lib/reform/composition.rb
reform-1.2.4 lib/reform/composition.rb
reform-1.2.3 lib/reform/composition.rb
reform-1.2.2 lib/reform/composition.rb
reform-1.2.1 lib/reform/composition.rb
reform-1.2.0.beta2 lib/reform/composition.rb
reform-1.2.0.beta1 lib/reform/composition.rb