Class: Ribbon::Options

Inherits:
BasicObject
Defined in:
lib/ribbon/options.rb

Overview

Applies options to all method calls.

Ribbon::Options.new(object, option: :value) do
  method_with some: :settings  # equivalent to { some: :settings, option: :value }
  overrides option: { with: :another_value }
end

Ribbon::Options.apply_to(Ribbon.new, separator: '->') do |ribbon|
  ribbon.to_s
  ribbon.inspect
end

Author:

  • Matheus Afonso Martins Moreira

Since:

  • 0.6.0

Instance Method Summary collapse

Constructor Details

#initialize(receiver, options = {}, &block) ⇒ Options

Applies the given options to all methods sent to the receiver. Will apply the block immediately, if given one.

Parameters:

  • receiver

    the object that will be receiving the methods

  • options (Ribbon, Ribbon::Raw, #to_hash) (defaults to: {})

    the options that will be applied to all methods

See Also:

Since:

  • 0.6.0



33
34
35
36
# File 'lib/ribbon/options.rb', line 33

def initialize(receiver, options = {}, &block)
  @receiver, @options = receiver, options
  __yield_or_eval__ &block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object

Merges the options given to the method with the options associated with this instance and sends the method to the receiver as normal.

Since:

  • 0.6.0



40
41
42
43
44
# File 'lib/ribbon/options.rb', line 40

def method_missing(method, *arguments, &block)
  options = arguments.extract_options_as_ribbon!
  arguments << ::Ribbon.deep_merge(@options, options)
  @receiver.__send__ method, *arguments, &block
end