module MiniModel

Constants

VERSION

Public Class Methods

included(model) click to toggle source
# File lib/mini_model.rb, line 8
def self.included(model)
  model.extend(ClassMethods)
end
new(attributes = {}) click to toggle source
# File lib/mini_model.rb, line 125
def initialize(attributes = {})
  self.attributes = attributes # Will set the id if it exists.
end

Public Instance Methods

==(other) click to toggle source

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

# File lib/mini_model.rb, line 169
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
attributes() click to toggle source
# File lib/mini_model.rb, line 148
def attributes
  @attributes
end
attributes=(attributes) click to toggle source

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.

# File lib/mini_model.rb, line 155
def attributes=(attributes)
  @attributes = {}

  attributes.each do |key, value|
    writer = :"#{key}="

    if respond_to?(writer)
      send(writer, value)
    end
  end
end
create() click to toggle source

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.

# File lib/mini_model.rb, line 209
def create
  id = dataset.insert(attributes)

  if id
    self.id = id

    self
  else
    nil
  end
end
dataset() click to toggle source
# File lib/mini_model.rb, line 129
def dataset
  self.class.dataset
end
delete() click to toggle source
# File lib/mini_model.rb, line 231
def delete
  count = dataset.where(id: id).delete

  if count.to_i > 0
    self.id = nil

    self
  else
    nil
  end
end
id() click to toggle source
# File lib/mini_model.rb, line 133
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) click to toggle source
# File lib/mini_model.rb, line 144
def id=(id)
  @id = id
end
persisted?() click to toggle source
# File lib/mini_model.rb, line 191
def persisted?
  !!@id
end
save() click to toggle source

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.

# File lib/mini_model.rb, line 198
def save
  if persisted?
    update
  else
    create
  end
end
update() click to toggle source
# File lib/mini_model.rb, line 221
def update
  count = dataset.where(id: id).update(attributes)

  if count.to_i > 0
    self
  else
    nil
  end
end