Class: StorageRoom::Model

Inherits:
Resource show all
Defined in:
lib/storage_room/model.rb

Overview

Abstract superclass for classes that can persist to the remote servers

Direct Known Subclasses

Collection, DeletedEntry, Entry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

handle_critical_response_errors, #loaded?, meta_data?, #reload

Methods included from Plugins

#included, #plugin, #plugins

Methods included from Accessors

#[], #as_json, #attributes, #attributes=, #eql?, #hash, #inspect, #loaded?, #proxy?, #response_data, #response_data=, #set_from_response_data

Constructor Details

#initialize(attributes = {}) ⇒ Model

Create a new model and set its attributes



41
42
43
44
45
# File 'lib/storage_room/model.rb', line 41

def initialize(attributes={})
  @errors = []

  super
end

Instance Attribute Details

#skip_webhooksObject

Returns the value of attribute skip_webhooks.



6
7
8
# File 'lib/storage_room/model.rb', line 6

def skip_webhooks
  @skip_webhooks
end

Class Method Details

.allObject

Get all models (could be paginated)



17
18
19
# File 'lib/storage_room/model.rb', line 17

def all
  load(index_path)
end

.create(attributes = {}) ⇒ Object

Create a new model with the passed attributes



10
11
12
13
14
# File 'lib/storage_room/model.rb', line 10

def create(attributes={})
  entry = new(attributes)
  entry.create
  entry
end

.find(id) ⇒ Object

Find a model with the specified id



22
23
24
# File 'lib/storage_room/model.rb', line 22

def find(id)
  load(show_path(id))
end

.index_pathObject

:nodoc:



27
28
29
# File 'lib/storage_room/model.rb', line 27

def index_path # :nodoc:
  raise StorageRoom::AbstractMethodError.new
end

.json_nameObject

:nodoc:



35
36
37
# File 'lib/storage_room/model.rb', line 35

def json_name  # :nodoc:
  raise StorageRoom::AbstractMethodError.new
end

.show_path(id) ⇒ Object

:nodoc:



31
32
33
# File 'lib/storage_room/model.rb', line 31

def show_path(id) # :nodoc:
  raise StorageRoom::AbstractMethodError.new
end

Instance Method Details

#createObject

Create a new model on the server



65
66
67
68
69
70
71
72
73
# File 'lib/storage_room/model.rb', line 65

def create
  return false unless new_record?
  run_callbacks :save do
    run_callbacks :create do
      httparty = self.class.post(self.class.index_path, request_options.merge(:body => to_json))
      handle_save_response(httparty)
    end
  end
end

#destroyObject

Delete an existing model on the server



87
88
89
90
91
92
93
94
95
96
# File 'lib/storage_room/model.rb', line 87

def destroy
  return false if new_record?

  run_callbacks :destroy do
    httparty = self.class.delete(self[:@url], request_options)
    self.class.handle_critical_response_errors(httparty)
  end

  true
end

#errorsObject

The validation errors that were returned by the server



117
118
119
# File 'lib/storage_room/model.rb', line 117

def errors
  @errors ||= []
end

#new_record?Boolean

Has this model been saved to the API already?

Returns:

  • (Boolean)


55
56
57
# File 'lib/storage_room/model.rb', line 55

def new_record?
  self['@version'] ? false : true
end

#reset!Object

Reset the model to its default state



48
49
50
51
52
# File 'lib/storage_room/model.rb', line 48

def reset!
  super
  @errors = []
  true
end

#saveObject

Create a new model or update an existing model on the server



60
61
62
# File 'lib/storage_room/model.rb', line 60

def save
  new_record? ? create : update
end

#to_hash(args = {}) ⇒ Object

ActiveSupport caused problems when using as_json, so using to_hash



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/storage_room/model.rb', line 104

def to_hash(args = {}) # :nodoc:
  args ||= {}

  if args[:nested]
    {'url' => self[:@url] || self[:url]}
  else
    hash = super
    hash.merge!('@version' => self['@version']) unless new_record?
    {self.class.json_name => hash}
  end
end

#updateObject

Update an existing model on the server



76
77
78
79
80
81
82
83
84
# File 'lib/storage_room/model.rb', line 76

def update
  return false if new_record?
  run_callbacks :save do
    run_callbacks :update do
      httparty = self.class.put(self[:@url], request_options.merge(:body => to_json))
      handle_save_response(httparty)
    end
  end
end

#valid?Boolean

Is the model valid or were there validation errors on the server?

Returns:

  • (Boolean)


99
100
101
# File 'lib/storage_room/model.rb', line 99

def valid?
  self.errors.empty?
end