class Shoegaze::Model

Attributes

store[W]
id[RW]

Public Class Methods

[](id)
Alias for: find
all() click to toggle source
# File lib/shoegaze/model.rb, line 61
def all
  records_for_class(self)
end
count() click to toggle source
# File lib/shoegaze/model.rb, line 87
def count
  records_for_class(self).length
end
create(attrs = {}) click to toggle source
# File lib/shoegaze/model.rb, line 95
def create(attrs = {})
  instance = self.new(attrs)
  register_model(instance)
end
destroy(id) click to toggle source
# File lib/shoegaze/model.rb, line 104
def destroy(id)
  find(id).destroy
end
drop_records() click to toggle source
# File lib/shoegaze/model.rb, line 12
def drop_records
  Shoegaze::Model.store = {}
end
ensure_model_namespace(model) click to toggle source
# File lib/shoegaze/model.rb, line 24
def ensure_model_namespace(model)
  # using an array here so that the models appear in the order they were created
  store[model.class] ||= []
end
exists?(id) click to toggle source
# File lib/shoegaze/model.rb, line 83
def exists?(id)
  raw_find(id) != nil
end
find(id) click to toggle source
# File lib/shoegaze/model.rb, line 78
def find(id)
  raw_find(id)
end
Also aliased as: []
find_by(options) click to toggle source
# File lib/shoegaze/model.rb, line 57
def find_by(options)
  where(options).first
end
first() click to toggle source
# File lib/shoegaze/model.rb, line 65
def first
  all[0]
end
last() click to toggle source
# File lib/shoegaze/model.rb, line 69
def last
  all[-1]
end
new(attrs = {}) click to toggle source
# File lib/shoegaze/model.rb, line 131
def initialize(attrs = {})
  @id = SecureRandom.uuid
  @__data = RecursiveOpenStruct.new(attrs)
end
records_for_class(klass) click to toggle source
# File lib/shoegaze/model.rb, line 41
def records_for_class(klass)
  store[klass] || []
end
register_model(model) click to toggle source
# File lib/shoegaze/model.rb, line 29
def register_model(model)
  ensure_model_namespace(model)
  store[model.class] << model
  model
end
select(&block) click to toggle source
# File lib/shoegaze/model.rb, line 91
def select(&block)
  records_for_class(self).select(&block)
end
store() click to toggle source
# File lib/shoegaze/model.rb, line 16
def store
  if self == Shoegaze::Model
    @store ||= {}
  else
    Shoegaze::Model.store
  end
end
unregister_model(model) click to toggle source
# File lib/shoegaze/model.rb, line 35
def unregister_model(model)
  ensure_model_namespace(model)
  store[model.class].delete(model)
  model
end
update(id, atts) click to toggle source
# File lib/shoegaze/model.rb, line 100
def update(id, atts)
  find(id).update_attributes(atts)
end
where(options) click to toggle source
# File lib/shoegaze/model.rb, line 45
def where(options)
  records_for_class(self).select do |r|
    options.all? do |k, v|
      if v.is_a?(Enumerable)
        v.include?(r.send(k))
      else
        r.send(k) == v
      end
    end
  end
end

Public Instance Methods

as_json(_options = nil) click to toggle source
# File lib/shoegaze/model.rb, line 161
def as_json(_options = nil) # options not presently actually supported
  @__data.to_h.with_indifferent_access
end
Also aliased as: attributes
attributes(_options = nil)
Alias for: as_json
destroy() click to toggle source
# File lib/shoegaze/model.rb, line 141
def destroy
  Shoegaze::Model.unregister_model(self)
end
Also aliased as: destroy!
destroy!()
Alias for: destroy
reload() click to toggle source
# File lib/shoegaze/model.rb, line 153
def reload
  self.class.find(id) || raise(Shoegaze::Model::UnknownRecordError)
end
save() click to toggle source
# File lib/shoegaze/model.rb, line 136
def save
  Shoegaze::Model.register_model(self)
end
Also aliased as: save!
save!()
Alias for: save
update_attributes(attrs) click to toggle source
# File lib/shoegaze/model.rb, line 146
def update_attributes(attrs)
  attrs.each do |k, v|
    send("#{k}=", v)
  end
end
Also aliased as: update_attributes!
update_attributes!(attrs)
Alias for: update_attributes