Class: Monokera::SDK::Model

Inherits:
Object
  • Object
show all
Includes:
ActiveModelSerializer
Defined in:
lib/monokera/sdk/model.rb

Direct Known Subclasses

ActiveModel

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveModelSerializer

#read_attribute_for_serialization

Class Method Details

.model_nameObject



44
45
46
# File 'lib/monokera/sdk/model.rb', line 44

def self.model_name
  self.name.to_s
end

Instance Method Details

#add_error(attribute, code, description) ⇒ Object



15
16
17
18
19
20
# File 'lib/monokera/sdk/model.rb', line 15

def add_error(attribute, code, description)
  error = errors.find(-> { Error.new(attribute: attribute) }) { |e| e.attribute == attribute.to_s }
  error.messages << ErrorMessage.new(code: code, description: description)

  errors.add(error)
end

#as_jsonObject



26
27
28
29
30
# File 'lib/monokera/sdk/model.rb', line 26

def as_json(*)
  json = super
  except_attributes!(json, 'errors')
  json.with_indifferent_access
end

#except_attributes!(model, *attributes_to_reject) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/monokera/sdk/model.rb', line 32

def except_attributes!(model, *attributes_to_reject)
  if model.is_a?(Hash)
    model.except!(*attributes_to_reject).each_value do |child|
      except_attributes!(child, *attributes_to_reject)
    end
  end
end

#to_json(_options = nil) ⇒ Object



40
41
42
# File 'lib/monokera/sdk/model.rb', line 40

def to_json(_options = nil)
  JSON.generate(as_json)
end

#valid?Boolean



22
23
24
# File 'lib/monokera/sdk/model.rb', line 22

def valid?
  errors.empty?
end