Class: Steroids::Types::Base

Inherits:
SerializableType show all
Defined in:
lib/steroids/types/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SerializableType

attr_accessor, attribute, #attributes, attributes, native_attr_accessor

Class Method Details

.import(options, object) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/steroids/types/base.rb', line 62

def import(options, object)
  instance = new({}, true)
  instance.import(options, object) if options.present?
  instance
rescue Exception => e
  raise Steroids::Errors::InternalServerError.new(
    cause: e,
    message: 'Import failed'
  )
end

.missing_attributes_for(payload) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/steroids/types/base.rb', line 73

def missing_attributes_for(payload)
  missing_attributes = []
  required_attributes.each do |attribute|
    unless payload.keys.include?(attribute) && !payload[attribute].nil?
      missing_attributes << attribute
    end
  end
  missing_attributes
end

.new(options = {}, ignore_required = false) ⇒ Object



95
96
97
98
# File 'lib/steroids/types/base.rb', line 95

def new(options = {}, ignore_required = false)
  validate_required!(options) unless ignore_required
  super(options)
end

.required_attributesObject



52
53
54
# File 'lib/steroids/types/base.rb', line 52

def required_attributes
  @required_attributes ||= []
end

.requires(attribute, _options = {}) ⇒ Object



56
57
58
59
60
# File 'lib/steroids/types/base.rb', line 56

def requires(attribute, _options = {})
  unless required_attributes.include?(attribute)
    required_attributes << attribute
  end
end

.validate_required(payload) ⇒ Object



83
84
85
# File 'lib/steroids/types/base.rb', line 83

def validate_required(payload)
  missing_attributes_for(payload).empty?
end

.validate_required!(payload) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/steroids/types/base.rb', line 87

def validate_required!(payload)
  unless validate_required(payload)
    raise Steroids::Errors::InternalServerError.new(
      message: name + ': Missing required attributes ' + missing_attributes_for(payload).to_s
    )
  end
end

Instance Method Details

#import(_options, _object = {}) ⇒ Object



4
5
6
7
8
# File 'lib/steroids/types/base.rb', line 4

def import(_options, _object = {})
  raise Steroids::Errors::InternalServerError.new(
    message: name + ': Import not implemented'
  )
end

#missing_attributesObject



47
48
49
# File 'lib/steroids/types/base.rb', line 47

def missing_attributes
  self.class.missing_attributes_for(values)
end

#to_sObject



10
11
12
13
14
15
16
# File 'lib/steroids/types/base.rb', line 10

def to_s
  if attributes.present?
    values.to_json
  else
    ''
  end
end

#validateObject



26
27
28
# File 'lib/steroids/types/base.rb', line 26

def validate
  validate_required && super
end

#validate!Object



30
31
32
33
# File 'lib/steroids/types/base.rb', line 30

def validate!
  validate_required!
  super
end

#validate_requiredObject



35
36
37
# File 'lib/steroids/types/base.rb', line 35

def validate_required
  missing_attributes.empty?
end

#validate_required!Object



39
40
41
42
43
44
45
# File 'lib/steroids/types/base.rb', line 39

def validate_required!
  unless validate_required
    raise Steroids::Errors::InternalServerError.new(
      message: self.class.name + ': Missing required attributes ' + missing_attributes.to_s
    )
  end
end

#valuesObject



18
19
20
21
22
23
24
# File 'lib/steroids/types/base.rb', line 18

def values
  hash = {}
  attributes.each do |attribute|
    hash[attribute] = read_attribute_for_serialization(attribute)
  end
  hash
end