Class: IronAdmin::Adapters::Http::Record

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/iron_admin/adapters/http/record.rb

Overview

Represents a single record from an HTTP API response.

Provides dynamic attribute access, change tracking, and ActiveModel-compatible persistence state methods.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Record

Returns a new instance of Record.



15
16
17
18
19
20
21
22
# File 'lib/iron_admin/adapters/http/record.rb', line 15

def initialize(attrs = {})
  @force_new = true if attrs.delete(:_persisted) == false
  @attributes = attrs.transform_keys(&:to_s)
  @id = @attributes["id"]
  @persisted = @force_new ? false : !@id.nil?
  @changes = {}
  @previous_changes = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



74
75
76
77
78
79
# File 'lib/iron_admin/adapters/http/record.rb', line 74

def method_missing(method_name, *args)
  key = method_name.to_s
  return @attributes[key] if @attributes.key?(key)

  super
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



13
14
15
# File 'lib/iron_admin/adapters/http/record.rb', line 13

def id
  @id
end

Instance Method Details

#[](key) ⇒ Object



40
41
42
# File 'lib/iron_admin/adapters/http/record.rb', line 40

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

#assign_attributes(new_attrs) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/iron_admin/adapters/http/record.rb', line 44

def assign_attributes(new_attrs)
  new_attrs.each do |key, value|
    key_s = key.to_s
    old_value = @attributes[key_s]
    next if old_value == value

    @changes[key_s] = [old_value, value]
    @attributes[key_s] = value
  end
end

#attributesObject



36
37
38
# File 'lib/iron_admin/adapters/http/record.rb', line 36

def attributes
  @attributes.dup
end

#changesObject



55
56
57
# File 'lib/iron_admin/adapters/http/record.rb', line 55

def changes
  @changes.dup
end

#mark_as_persistedObject



63
64
65
66
67
68
# File 'lib/iron_admin/adapters/http/record.rb', line 63

def mark_as_persisted
  @previous_changes = @changes.dup
  @changes = {}
  @id = @attributes["id"]
  @persisted = true
end

#new_record?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/iron_admin/adapters/http/record.rb', line 32

def new_record?
  !persisted?
end

#persisted?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/iron_admin/adapters/http/record.rb', line 28

def persisted?
  @persisted
end

#previous_changesObject



59
60
61
# File 'lib/iron_admin/adapters/http/record.rb', line 59

def previous_changes
  @previous_changes.dup
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/iron_admin/adapters/http/record.rb', line 70

def respond_to_missing?(method_name, include_private = false)
  @attributes.key?(method_name.to_s) || super
end

#to_paramObject



24
25
26
# File 'lib/iron_admin/adapters/http/record.rb', line 24

def to_param
  @id&.to_s
end