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.

Direct Known Subclasses

DynamicModel, TaskResponse

Defined Under Namespace

Classes: Field

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
# File 'lib/runapi/core/base_model.rb', line 87

def initialize(attributes = {})
  source = normalize_input(attributes)
  @attributes = {}

  assign_declared_fields!(source)
  assign_extra_fields!(source)
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)
    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



122
123
124
125
126
127
128
129
130
131
# File 'lib/runapi/core/base_model.rb', line 122

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

#[](key) ⇒ Object



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

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

#dig(*keys) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/runapi/core/base_model.rb', line 99

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

#to_hObject Also known as: to_hash



115
116
117
118
119
# File 'lib/runapi/core/base_model.rb', line 115

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