Class: GlobalCollect::RequestModels::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/global_collect/request_models/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Base

Returns a new instance of Base.



12
13
14
15
# File 'lib/global_collect/request_models/base.rb', line 12

def initialize(attributes)
  @attributes = {}
  attributes.each {|k,v| self[k] = v }
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



3
4
5
# File 'lib/global_collect/request_models/base.rb', line 3

def errors
  @errors
end

Instance Method Details

#[](key) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
# File 'lib/global_collect/request_models/base.rb', line 27

def [](key)
  raise ArgumentError.new("Invalid attribute name '#{key}'!") unless fields.key?(key)
  @attributes[key]
end

#[]=(key, value) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
# File 'lib/global_collect/request_models/base.rb', line 32

def []=(key, value)
  raise ArgumentError.new("Invalid attribute name '#{key}'!") unless fields.key?(key)
  @attributes[key] = value
end

#fieldsObject



8
9
10
# File 'lib/global_collect/request_models/base.rb', line 8

def fields
  {}
end

#suggested_response_mixinsObject



4
5
6
# File 'lib/global_collect/request_models/base.rb', line 4

def suggested_response_mixins
  []
end

#validateObject



17
18
19
20
21
22
23
24
25
# File 'lib/global_collect/request_models/base.rb', line 17

def validate
  fields.each do |field, validations|
    validator = GlobalCollect::FieldValidator.new(*validations)
    unless validator.validate(@attributes[field])
      @errors = { field => "is invalid. Should conform to #{validations.inspect}" }
    end
  end
  return @errors.blank?
end