Class: Charlatan

Inherits:
Module
  • Object
show all
Defined in:
lib/charlatan.rb,
lib/charlatan/version.rb

Overview

Charlatan turns your object into a proxy which will forward all method missing calls to object it’s wrapping

Examples:


class UserPresenter
  include Charlatan.new(:user)
end

user      = OpenStruct.new(:name => "Jane")
presenter = UserPresenter.new(user)

user.name # => "Jane"
presenter.user == user # => true

Defined Under Namespace

Modules: Methods

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Charlatan

Returns a new instance of Charlatan.



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

def initialize(name, options = {})
  attr_reader name
  ivar = "@#{name}"

  define_method(:initialize) do |*args, &block|
    instance_variable_set(ivar, args.first)
    @__proxy_args = args[1..args.size] || []
  end

  define_method(:__proxy_target__) do
    instance_variable_get(ivar)
  end

  define_method(:__proxy_kind__) do
    options[:kind] || __proxy_target__.class
  end

  include Methods
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end