module MiniModel::ClassMethods

Public Instance Methods

[](id) click to toggle source
# File lib/mini_model.rb, line 49
def [](id)
  first(id: id)
end
all(&block) click to toggle source
# File lib/mini_model.rb, line 63
def all(&block)
  build(dataset.all(&block))
end
attribute(key, type = nil) click to toggle source

Defines an accessor for the attributes hash. The whole point of the attributes hash vs. instance variables is for easily passing a hash to the dataset for persistence. Maybe this is a bad idea and we should use plain ol’ attr_accessor and build the hash when needed.

# File lib/mini_model.rb, line 26
def attribute(key, type = nil)
  reader = :"#{key}"
  writer = :"#{key}="

  define_method(reader) do
    self.attributes[reader]
  end

  define_method(writer) do |value|
    self.attributes[reader] = value
  end
end
build(dataset) click to toggle source
# File lib/mini_model.rb, line 39
def build(dataset)
  dataset.map { |attributes| new(attributes) }
end
child(association, model_name, foreign_key = to_foreign_key) click to toggle source
# File lib/mini_model.rb, line 89
def child(association, model_name, foreign_key = to_foreign_key)
  define_method(association) do
    model = self.class.const_get(model_name)

    model.first(foreign_key => id)
  end
end
children(association, model_name, foreign_key = to_foreign_key) click to toggle source
# File lib/mini_model.rb, line 81
def children(association, model_name, foreign_key = to_foreign_key)
  define_method(association) do
    model = self.class.const_get(model_name)

    model.where(foreign_key => id)
  end
end
create(attributes = {}) click to toggle source

Convenience for initializin and persisting a new model instance.

# File lib/mini_model.rb, line 45
def create(attributes = {})
  new(attributes).create
end
dataset() click to toggle source
# File lib/mini_model.rb, line 13
def dataset
  @dataset
end
dataset=(dataset) click to toggle source
# File lib/mini_model.rb, line 17
def dataset=(dataset)
  @dataset = dataset
end
first(*args, &block) click to toggle source
# File lib/mini_model.rb, line 53
def first(*args, &block)
  attributes = dataset.first(*args, &block)

  if attributes
    new(attributes)
  else
    nil
  end
end
parent(association, model_name, foreign_key = :" click to toggle source
# File lib/mini_model.rb, line 97
def parent(association, model_name, foreign_key = :"#{association}_id")
  reader = foreign_key
  writer = :"#{foreign_key}="

  define_method(reader) do
    self.attributes[reader]
  end

  define_method(writer) do |value|
    self.attributes[reader] = value
  end

  define_method(association) do
    model = self.class.const_get(model_name)

    model[send(foreign_key)]
  end

  define_method(:"#{association}=") do |value|
    if value
      send(writer, value.id)
    else
      send(writer, value)
    end
  end
end
to_foreign_key() click to toggle source
# File lib/mini_model.rb, line 71
def to_foreign_key
  name.
    to_s.
    match(/^(?:.*::)*(.*)$/)[1].
    gsub(/([a-z\d])([A-Z])/, '\1_\2').
    downcase.
    concat('_id').
    to_sym
end
where(*args, &block) click to toggle source
# File lib/mini_model.rb, line 67
def where(*args, &block)
  build(dataset.where(*args, &block))
end