Class: Contracts::Decorator

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

Direct Known Subclasses

Contract

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, method) ⇒ Decorator

Returns a new instance of Decorator.



46
47
48
# File 'lib/contracts/decorators.rb', line 46

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

Class Attribute Details

.decoratorsObject

Returns the value of attribute decorators.



27
28
29
# File 'lib/contracts/decorators.rb', line 27

def decorators
  @decorators
end

Class Method Details

.inherited(klass) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/contracts/decorators.rb', line 29

def self.inherited(klass)
  super
  name = klass.name.gsub(/^./) { |m| m.downcase }

  return if name =~ /^[^A-Za-z_]/ || name =~ /[^0-9A-Za-z_]/

  # the file and line parameters set the text for error messages
  # make a new method that is the name of your decorator.
  # that method accepts random args and a block.
  # inside, `decorate` is called with those params.
  MethodDecorators.module_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
    def #{klass}(*args, &blk)
      ::Contracts::Engine.fetch_from(self).decorate(#{klass}, *args, &blk)
    end
  RUBY_EVAL
end