Class: RunApi::Core::BaseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/runapi/core/base_model.rb

Overview

Lightweight response model with typed field declarations and recursive coercion.

Defined Under Namespace

Classes: Field

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ BaseModel

Returns a new instance of BaseModel.



87
88
89
90
91
92
93
94
# File 'lib/runapi/core/base_model.rb', line 87

def initialize(attributes = {})
  source = normalize_input(attributes)
  @attributes = {}
  @response_headers = ResponseHeaders.new

  assign_declared_fields!(source)
  assign_extra_fields!(source)
end

Instance Attribute Details

#response_headersObject (readonly)

Returns the value of attribute response_headers.



96
97
98
# File 'lib/runapi/core/base_model.rb', line 96

def response_headers
  @response_headers
end

Class Method Details

.coerce(value, as: DynamicModel) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/runapi/core/base_model.rb', line 35

def coerce(value, as: DynamicModel)
  target_model = resolve_type(as)

  case value
  when nil
    nil
  when BaseModel
    if target_model && target_model <= BaseModel && !value.is_a?(target_model)
      target_model.from_hash(value.to_h).with_response_headers(value.response_headers)
    else
      value
    end
  when Hash
    model = (target_model && target_model <= BaseModel) ? target_model : DynamicModel
    model.from_hash(value)
  when Array
    value.map { |item| coerce(item, as: target_model || DynamicModel) }
  else
    value
  end
end

.fieldsObject



10
11
12
13
14
15
# File 'lib/runapi/core/base_model.rb', line 10

def fields
  @fields ||= begin
    parent_fields = superclass.respond_to?(:fields) ? superclass.fields : {}
    parent_fields.transform_values(&:dup)
  end
end

.from_hash(payload) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/runapi/core/base_model.rb', line 25

def from_hash(payload)
  return payload if payload.is_a?(self)

  unless payload.is_a?(Hash)
    raise TypeError, "Expected Hash for #{name}, got #{payload.class}"
  end

  new(payload)
end

.optional(name, type = nil, enum: nil) ⇒ Object



21
22
23
# File 'lib/runapi/core/base_model.rb', line 21

def optional(name, type = nil, enum: nil)
  define_field(name, type, required: false, enum: enum)
end

.required(name, type = nil, enum: nil) ⇒ Object



17
18
19
# File 'lib/runapi/core/base_model.rb', line 17

def required(name, type = nil, enum: nil)
  define_field(name, type, required: true, enum: enum)
end

Instance Method Details

#==(other) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/runapi/core/base_model.rb', line 138

def ==(other)
  case other
  when BaseModel
    to_h == other.to_h
  when Hash
    to_h == stringify_keys(other)
  else
    super
  end
end

#[](key) ⇒ Object



111
112
113
# File 'lib/runapi/core/base_model.rb', line 111

def [](key)
  @attributes[key.to_s]
end

#dig(*keys) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/runapi/core/base_model.rb', line 115

def dig(*keys)
  current = self
  keys.each do |key|
    current = case current
    when BaseModel
      current[key]
    when Hash
      current[key] || current[key.to_s] || current[key.to_sym]
    when Array
      key.is_a?(Integer) ? current[key] : nil
    end
    return nil if current.nil?
  end
  current
end

#response_header(name) ⇒ Object



98
99
100
# File 'lib/runapi/core/base_model.rb', line 98

def response_header(name)
  response_headers[name]
end

#runapi_task_idObject



102
103
104
# File 'lib/runapi/core/base_model.rb', line 102

def runapi_task_id
  response_header("X-RunAPI-Task-Id")
end

#to_hObject Also known as: to_hash



131
132
133
134
135
# File 'lib/runapi/core/base_model.rb', line 131

def to_h
  @attributes.each_with_object({}) do |(key, value), out|
    out[key] = serialize(value)
  end
end

#with_response_headers(headers) ⇒ Object



106
107
108
109
# File 'lib/runapi/core/base_model.rb', line 106

def with_response_headers(headers)
  @response_headers = headers.is_a?(ResponseHeaders) ? headers : ResponseHeaders.new(headers)
  self
end