Class: InformantCommon::Model::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/informant-common/model/base.rb

Direct Known Subclasses

ActiveModel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, name: nil, field_errors: []) ⇒ Base

Returns a new instance of Base.



7
8
9
10
11
# File 'lib/informant-common/model/base.rb', line 7

def initialize(id: nil, name: nil, field_errors: [])
  self.id = id
  self.name = name
  self.errors = field_errors
end

Instance Attribute Details

#errorsObject



13
14
15
# File 'lib/informant-common/model/base.rb', line 13

def errors
  @errors ||= []
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/informant-common/model/base.rb', line 4

def id
  @id
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/informant-common/model/base.rb', line 4

def name
  @name
end

Instance Method Details

#add_error(field_name, value, message) ⇒ Object



17
18
19
# File 'lib/informant-common/model/base.rb', line 17

def add_error(field_name, value, message)
  errors << FieldError.new(field_name, value, message)
end

#as_json(*_args) ⇒ Object



21
22
23
# File 'lib/informant-common/model/base.rb', line 21

def as_json(*_args)
  { name: name, errors: errors.map(&:as_json) }
end

#to_json(*_args) ⇒ Object



25
26
27
# File 'lib/informant-common/model/base.rb', line 25

def to_json(*_args)
  as_json.to_json
end