Class: Surrogate::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/surrogate/options.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, default_proc) ⇒ Options

Returns a new instance of Options.



7
8
9
# File 'lib/surrogate/options.rb', line 7

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

Instance Attribute Details

#default_procObject

Returns the value of attribute default_proc.



5
6
7
# File 'lib/surrogate/options.rb', line 5

def default_proc
  @default_proc
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/surrogate/options.rb', line 5

def options
  @options
end

Instance Method Details

#[](key) ⇒ Object



15
16
17
# File 'lib/surrogate/options.rb', line 15

def [](key)
  options[key]
end

#default(instance, invocation, &no_default) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/surrogate/options.rb', line 23

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

#has?(name) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/surrogate/options.rb', line 11

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

#to_hashObject



19
20
21
# File 'lib/surrogate/options.rb', line 19

def to_hash
  options
end