Class: Regolith::RegolithRecord

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Model, ActiveModel::Serialization
Defined in:
lib/regolith/regolith_record.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allObject



64
65
66
67
68
69
70
71
# File 'lib/regolith/regolith_record.rb', line 64

def self.all
  response = ServiceClient.get(service_name, resource_name)
  if response.code == '200'
    JSON.parse(response.body).map { |attrs| new(attrs) }
  else
    []
  end
end

.belongs_to(association_name, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/regolith/regolith_record.rb', line 34

def self.belongs_to(association_name, options = {})
  service = options[:service]
  foreign_key = options[:foreign_key] || "#{association_name}_id"
  
  # Add the foreign key attribute
  attribute foreign_key, :integer
  
  define_method(association_name) do
    foreign_key_value = send(foreign_key)
    return nil unless foreign_key_value
    
    RegolithAssociation.new(
      self,
      association_name,
      service,
      :belongs_to,
      foreign_key_value
    ).first
  end
end

.create(attributes = {}) ⇒ Object



73
74
75
76
77
# File 'lib/regolith/regolith_record.rb', line 73

def self.create(attributes = {})
  instance = new(attributes)
  instance.save
  instance
end

.create!(attributes = {}) ⇒ Object



79
80
81
82
83
# File 'lib/regolith/regolith_record.rb', line 79

def self.create!(attributes = {})
  instance = create(attributes)
  raise "Record invalid" unless instance.persisted?
  instance
end

.find(id) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/regolith/regolith_record.rb', line 55

def self.find(id)
  response = ServiceClient.get(service_name, "#{resource_name}/#{id}")
  if response.code == '200'
    new(JSON.parse(response.body))
  else
    raise ActiveRecord::RecordNotFound, "Couldn't find #{name} with 'id'=#{id}"
  end
end

.has_many(association_name, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/regolith/regolith_record.rb', line 21

def self.has_many(association_name, options = {})
  service = options[:service]
  
  define_method(association_name) do
    RegolithAssociation.new(
      self,
      association_name,
      service,
      :has_many
    )
  end
end

.inherited(subclass) ⇒ Object



11
12
13
14
15
# File 'lib/regolith/regolith_record.rb', line 11

def self.inherited(subclass)
  super
  # Auto-detect service from class name or allow explicit setting
  subclass.resource_name = subclass.name.underscore
end

.service(service_name) ⇒ Object



17
18
19
# File 'lib/regolith/regolith_record.rb', line 17

def self.service(service_name)
  self.service_name = service_name
end

Instance Method Details

#createObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/regolith/regolith_record.rb', line 93

def create
  response = ServiceClient.post(
    self.class.service_name, 
    self.class.resource_name,
    attributes.to_json
  )
  
  if response.code.to_i.between?(200, 299)
    data = JSON.parse(response.body)
    data.each { |key, value| send("#{key}=", value) if respond_to?("#{key}=") }
    true
  else
    false
  end
end

#destroyObject



119
120
121
122
123
124
125
126
# File 'lib/regolith/regolith_record.rb', line 119

def destroy
  response = ServiceClient.delete(
    self.class.service_name,
    "#{self.class.resource_name}/#{id}"
  )
  
  response.code.to_i.between?(200, 299)
end

#idObject



132
133
134
# File 'lib/regolith/regolith_record.rb', line 132

def id
  send(self.class.primary_key) if respond_to?(self.class.primary_key)
end

#id=(value) ⇒ Object



136
137
138
# File 'lib/regolith/regolith_record.rb', line 136

def id=(value)
  send("#{self.class.primary_key}=", value) if respond_to?("#{self.class.primary_key}=")
end

#persisted?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/regolith/regolith_record.rb', line 128

def persisted?
  id.present?
end

#saveObject



85
86
87
88
89
90
91
# File 'lib/regolith/regolith_record.rb', line 85

def save
  if persisted?
    update
  else
    create
  end
end

#updateObject



109
110
111
112
113
114
115
116
117
# File 'lib/regolith/regolith_record.rb', line 109

def update
  response = ServiceClient.put(
    self.class.service_name,
    "#{self.class.resource_name}/#{id}",
    attributes.to_json
  )
  
  response.code.to_i.between?(200, 299)
end