Class: Webmate::Decorators::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/webmate/decorators/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity, options = {}) ⇒ Base

Returns a new instance of Base.



5
6
7
# File 'lib/webmate/decorators/base.rb', line 5

def initialize(entity, options = {})
  @entity, @options = entity, options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/webmate/decorators/base.rb', line 23

def method_missing(method, *args, &block)
  if entity.respond_to?(method)
    self.class.send :define_method, method do |*args, &blokk|
      entity.send method, *args, &blokk
    end

    send method, *args, &block
  else
    super
  end
rescue NoMethodError => no_method_error
  super if no_method_error.name == method
  raise no_method_error
end

Instance Attribute Details

#entityObject (readonly)

Returns the value of attribute entity.



3
4
5
# File 'lib/webmate/decorators/base.rb', line 3

def entity
  @entity
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/webmate/decorators/base.rb', line 3

def options
  @options
end

Class Method Details

.decorate(entity_or_collection, options = {}) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/webmate/decorators/base.rb', line 10

def decorate(entity_or_collection, options = {})
  if entity_or_collection.respond_to?(:map)
    entity_or_collection.map { |e| self.decorate(e, options) }
  else
    self.new(entity_or_collection, options)
  end
end

Instance Method Details

#idObject



19
20
21
# File 'lib/webmate/decorators/base.rb', line 19

def id
  entity.id.to_s
end