Module: MiniModel

Defined in:
lib/mini_model.rb

Defined Under Namespace

Modules: ClassMethods Classes: Error, MissingId

Constant Summary collapse

VERSION =
'0.0.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(model) ⇒ Object



8
9
10
# File 'lib/mini_model.rb', line 8

def self.included(model)
  model.extend(ClassMethods)
end

Instance Method Details

#==(other) ⇒ Object

Strap in, the is probably the most complicated method in the entire library.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/mini_model.rb', line 164

def ==(other)
  # If the classes don't match, they cannot possibly be equal.
  if self.class != other.class
    return false
  end

  # If the persisted state doesn't match, they also can never be equal.
  if persisted? != other.persisted?
    return false
  end

  # When persisted, check the other's id to see if it's the same,
  # cannot possible be equals if they have different ids.
  if persisted? && id != other.id
    return false
  end

  # Finally, compare the attributes hash. If all key/values match,
  # they are considered equal.
  attributes == other.attributes
end

#attributesObject



146
147
148
# File 'lib/mini_model.rb', line 146

def attributes
  @attributes
end

#attributes=(attributes) ⇒ Object

#attributes= is vulnerable to mass assignment attacks if used directly with user input. Some sort of filter must be in place before setting attributes or initializing a new model. Sending a key in the hash argument that doesn’t have an accessor raises an error.



154
155
156
157
158
159
160
# File 'lib/mini_model.rb', line 154

def attributes=(attributes)
  @attributes = {}

  attributes.each do |key, value|
    send(:"#{key}=", value)
  end
end

#createObject

#create (as well as #update, and #delete) return self on success and nil on failure. This lets us use these actions as if conditions which is convenience though dangerous.



204
205
206
207
208
209
210
211
212
213
214
# File 'lib/mini_model.rb', line 204

def create
  id = dataset.insert(attributes)

  if id
    self.id = id

    self
  else
    nil
  end
end

#datasetObject



127
128
129
# File 'lib/mini_model.rb', line 127

def dataset
  self.class.dataset
end

#deleteObject



226
227
228
229
230
231
232
233
234
235
236
# File 'lib/mini_model.rb', line 226

def delete
  count = dataset.where(id: id).delete

  if count.to_i > 0
    self.id = nil

    self
  else
    nil
  end
end

#idObject



131
132
133
134
135
136
137
138
139
140
# File 'lib/mini_model.rb', line 131

def id
  if @id
    @id
  else
    # If our model does not have an id, raise at the first occurence
    # of anyone expecting it. This prevents us from assigning associations
    # and other logical paths for things that do not exist in the db.
    raise MissingId
  end
end

#id=(id) ⇒ Object



142
143
144
# File 'lib/mini_model.rb', line 142

def id=(id)
  @id = id
end

#initialize(attributes = {}) ⇒ Object



123
124
125
# File 'lib/mini_model.rb', line 123

def initialize(attributes = {})
  self.attributes = attributes # Will set the id if it exists.
end

#persisted?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/mini_model.rb', line 186

def persisted?
  !!@id
end

#saveObject

Use #save to write generic persistence code in things like form objects so you don’t have to reach inside the model to determine the proper method to call.



193
194
195
196
197
198
199
# File 'lib/mini_model.rb', line 193

def save
  if persisted?
    update
  else
    create
  end
end

#updateObject



216
217
218
219
220
221
222
223
224
# File 'lib/mini_model.rb', line 216

def update
  count = dataset.where(id: id).update(attributes)

  if count.to_i > 0
    self
  else
    nil
  end
end