Sha256: f9b4a7f32010cc8e2454b3e82e33116cf735f1603c8b64f9333601b8a90aa789

Contents?: true

Size: 1.71 KB

Versions: 1

Compression:

Stored size: 1.71 KB

Contents

require 'representable/hash'
require 'representable/decorator'

module Reform
  class Representer < Representable::Decorator
    # Invokes #to_hash and/or #from_hash with #options. This provides a hook for other
    # modules to add options for the representational process.
    module WithOptions
      class Options < Hash
        def include!(names)
          self[:include] ||= []
          self[:include] += names
          self
        end

        def exclude!(names)
          self[:exclude] ||= []
          self[:exclude] +=  names
          self
        end
      end

      def options
        Options.new
      end

      def to_hash(*)
        super(options)
      end

      def from_hash(*)
        super(options)
      end
    end

    include Representable::Hash

    # Returns hash of all property names.
    def fields
      representable_attrs.map(&:name)
    end

    def nested_forms(&block)
      clone_config!.
        find_all { |attr| attr.options[:form] }.
        collect  { |attr| [attr, represented.send(attr.getter)] }. # DISCUSS: can't we do this with the Binding itself?
        each(&block)
    end

  private
    def clone_config!
      # TODO: representable_attrs.clone! which does exactly what's done below.
      attrs = Representable::Config.new
      attrs.inherit(representable_attrs) # since in every use case we modify Config we clone.
      @representable_attrs = attrs
    end

    def self.inline_representer(base_module, name, options, &block)
      name = name.to_s.singularize.camelize

      Class.new(Form) do
        instance_exec &block

        @form_name = name

        def self.name # needed by ActiveModel::Validation and I18N.
          @form_name
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reform-0.2.4 lib/reform/representer.rb