Class: Fixjour::MergingProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/fixjour/merging_proxy.rb

Overview

Proxy passed to builder blocks as first argument when a builder block takes two arguments. Automatically merges overrides when the #new method is called.

Instance Method Summary collapse

Constructor Details

#initialize(klass, overrides) ⇒ MergingProxy

Returns a new instance of MergingProxy.



8
9
10
11
# File 'lib/fixjour/merging_proxy.rb', line 8

def initialize(klass, overrides)
  @klass = klass
  @overrides = overrides
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



31
32
33
# File 'lib/fixjour/merging_proxy.rb', line 31

def method_missing(sym, *args, &block)
  @klass.respond_to?(sym) ? @klass.send(sym, *args, &block) : super
end

Instance Method Details

#new(defaults = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/fixjour/merging_proxy.rb', line 20

def new(defaults={})
  attrs = defaults.merge(@overrides)
  accessible, inaccessible = partition(attrs)

  @klass.new(accessible).tap do |instance|
    inaccessible.each do |key,val|
      instance.send("#{key}=", val)
    end
  end
end

#protected(*attrs) ⇒ Object



13
14
15
16
17
18
# File 'lib/fixjour/merging_proxy.rb', line 13

def protected(*attrs)
  attrs = attrs.empty? ?
    @protected :
    @protected = attrs
  Set.new(attrs)
end