Sha256: fbc029bfa32aa0de9372e46a10ca87b9689e5afb745db8d01521adf7e3579905

Contents?: true

Size: 1.98 KB

Versions: 6

Compression:

Stored size: 1.98 KB

Contents

require "representable"

module Representable
  class Decorator
    attr_reader :represented
    alias_method :decorated, :represented

    # TODO: when moving all class methods into ClassMethods, i get a segfault.
    def self.prepare(represented)
      new(represented)
    end

    def self.default_inline_class
      Representable::Decorator
    end

    include Representable # include after class methods so Decorator::prepare can't be overwritten by Representable::prepare.

    # TODO: implement that just by calling ::property(name, options){include mod} on the inheriting representer.
    module InheritModule
      def inherit_module!(parent)
        inherited_attrs = parent.representable_attrs[:definitions].keys

        super # in Representable, calls representable_attrs.inherit!(parent.representable_attrs).
        # now, inline representers are still modules, which is wrong.
        manifest!(inherited_attrs)
      end

    private
       # one level deep manifesting modules into Decorators.
      def manifest!(names)
        names.each do |name| # only definitions.
          definition = representable_attrs.get(name)
          next unless definition[:_inline] and mod = definition.representer_module # only inline representers.

          # here, we can include Decorator features.
          inline_representer = build_inline(nil, representable_attrs.features, definition.name, {}) {
            include mod
          } # the includer controls what "wraps" the module.

          definition.merge!(:extend => inline_representer)
        end
      end
    end
    extend InheritModule


    def initialize(represented)
      @represented = represented
    end

  private
    def self.build_inline(base, features, name, options, &block)
      Class.new(base || default_inline_class).tap do |decorator|
        decorator.class_eval do # Ruby 1.8.7 wouldn't properly execute the block passed to Class.new!
          feature *features
          class_eval &block
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
representable-2.2.2 lib/representable/decorator.rb
representable-2.2.1 lib/representable/decorator.rb
representable-2.2.0 lib/representable/decorator.rb
representable-2.1.8 lib/representable/decorator.rb
representable-2.1.7 lib/representable/decorator.rb
representable-2.1.6 lib/representable/decorator.rb