Class: Ohm::Model::Wrapper

Inherits:
Ohm::BasicObject
Defined in:
lib/ohm.rb

Overview

Wraps a model name for lazy evaluation.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Wrapper) initialize(name, &block)

A new instance of Wrapper



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ohm.rb', line 68

def initialize(name, &block)
  @name = name
  @caller = ::Kernel.caller[2]
  @block = block

  class << self
    def method_missing(method_id, *args)
      ::Kernel.raise ::NoMethodError, "You tried to call #{@name}##{method_id}, but #{@name} is not defined on #{@caller}"
    end
  end
end

Class Method Details

+ (Object) wrap(object)



80
81
82
# File 'lib/ohm.rb', line 80

def self.wrap(object)
  object.class == self ? object : new(object.inspect) { object }
end

Instance Method Details

- (Object) class



88
89
90
# File 'lib/ohm.rb', line 88

def class
  Wrapper
end

- (Object) inspect



92
93
94
# File 'lib/ohm.rb', line 92

def inspect
  "<Wrapper for #{@name} (in #{@caller})>"
end

- (Object) unwrap



84
85
86
# File 'lib/ohm.rb', line 84

def unwrap
  @block.call
end