Sha256: ac9fb6e7a32b93ebac7b455da20b88237da21b9a0b00b855cd7772405807fbd1

Contents?: true

Size: 1.29 KB

Versions: 9

Compression:

Stored size: 1.29 KB

Contents

require 'rest-core'

module RestCore::Wrapper
  include RestCore
  def self.included mod
    mod.send(:attr_reader, :init, :middles, :wrapped)
  end

  def initialize &block
    @middles ||= []
    instance_eval(&block) if block_given?
    @wrapped ||= to_app(@init || Ask)
  end

  def use middle, *args, &block
    middles << [middle, args, block]
  end

  def run app
    @init = app
  end

  def members
    middles.map{ |(middle, args, block)|
      if middle.public_method_defined?(:wrapped)
        # TODO: this is hacky... try to avoid calling new!
        middle.members + middle.new(Ask.new, *args, &block).members
      else
        middle.members
      end
    }.flatten
  end

  def to_app init=@init
    # === foldr m.new app middles
    middles.reverse.inject(init.new){ |app_, (middle, args, block)|
      begin
        middle.new(app_, *partial_deep_copy(args), &block)
      rescue ArgumentError => e
        raise ArgumentError.new("#{middle}: #{e}")
      end
    }
  end

  module_function
  def partial_deep_copy obj
    case obj
      when Array; obj.map{ |o| partial_deep_copy(o) }
      when Hash ; obj.inject({}){ |r, (k, v)| r[k] = partial_deep_copy(v); r }
      when Numeric, Symbol, TrueClass, FalseClass, NilClass; obj
      else begin obj.dup; rescue TypeError; obj; end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rest-core-0.3.0 lib/rest-core/wrapper.rb
rest-core-0.3.0.pre.2 lib/rest-core/wrapper.rb
rest-core-0.3.0.pre.1 lib/rest-core/wrapper.rb
rest-core-0.3.0.pre.0 lib/rest-core/wrapper.rb
rest-core-0.2.3 lib/rest-core/wrapper.rb
rest-core-0.2.2 lib/rest-core/wrapper.rb
rest-core-0.2.1 lib/rest-core/wrapper.rb
rest-core-0.2.0 lib/rest-core/wrapper.rb
rest-core-0.0.1 lib/rest-core/wrapper.rb