Module: GL::Contract

Defined in:
lib/gl/command/contract.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/gl/command/contract.rb', line 5

def self.included(base)
  base.class_eval do
    include ActiveModel::Validations
    extend ClassMethods

    before :validate_contract!
    after :validate_return_contract!
  end
end

Instance Method Details

#klassObject



101
102
103
# File 'lib/gl/command/contract.rb', line 101

def klass
  self.class
end

#return_attributesObject



93
94
95
# File 'lib/gl/command/contract.rb', line 93

def return_attributes
  klass.instance_variable_get(:@return_attributes) || []
end

#return_strong_attributesObject



97
98
99
# File 'lib/gl/command/contract.rb', line 97

def return_strong_attributes
  klass.instance_variable_get(:@return_strong_attributes) || {}
end

#validate_contract!Object

Raises:



77
78
79
80
81
82
# File 'lib/gl/command/contract.rb', line 77

def validate_contract!
  return if valid?

  context.errors.copy!(errors)
  raise ContractFailure
end

#validate_return_contract!Object

Raises:



84
85
86
87
88
89
90
91
# File 'lib/gl/command/contract.rb', line 84

def validate_return_contract!
  klass.enforce_attribute_presence(*return_attributes)
  klass.enforce_attribute_types(**return_strong_attributes)
  return if valid?

  context.errors.copy!(errors)
  raise ContractFailure
end