Sha256: 7ba3ebd197a965cc6ad24246a14217a8315e979e493b479bd73765315fe498f7

Contents?: true

Size: 1 KB

Versions: 6

Compression:

Stored size: 1 KB

Contents

require 'bindable_block'

class Surrogate
  class Options
    attr_accessor :options, :default_proc

    def initialize(options, default_proc)
      self.options, self.default_proc = options, default_proc
    end

    def has?(name)
      options.has_key? name
    end

    def [](key)
      options[key]
    end

    def to_hash
      options
    end

    def default(instance, invocation, &no_default)
      if options.has_key? :default
        options[:default]
      elsif default_proc
        # This works for now, but it's a kind of crappy solution because
        # BindableBlock adds and removes methods for each time it is invoked.
        #
        # A better solution would be to instantiate it before passing it to
        # the options, then we only have to bind it to an instance and invoke
        BindableBlock.new(instance.class, &default_proc)
                     .bind(instance)
                     .call(*invocation.args, &invocation.block)
      else
        no_default.call
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
surrogate-0.5.5 lib/surrogate/options.rb
surrogate-0.5.4 lib/surrogate/options.rb
surrogate-0.5.3 lib/surrogate/options.rb
surrogate-0.5.2 lib/surrogate/options.rb
surrogate-0.5.1 lib/surrogate/options.rb
surrogate-0.5.0 lib/surrogate/options.rb