class Tempo::Model::Base
Attributes
id[R]
Public Class Methods
delete(instance)
click to toggle source
# File lib/tempo/models/base.rb, line 146 def delete(instance) id = instance.id index.delete( instance ) ids.delete( id ) end
file()
click to toggle source
example: Tempo::Model::Animal -> tempo_animals.yaml
# File lib/tempo/models/base.rb, line 40 def file FileRecord::FileUtility.new(self).filename end
find(key, value)
click to toggle source
example: Tempo::Model.find(“id”, 1)
# File lib/tempo/models/base.rb, line 126 def find(key, value) key = "@#{key}".to_sym matches = [] index.each do |i| stored_value = i.instance_variable_get( key ) if stored_value.kind_of? String if value.kind_of? Regexp matches << i if value.match stored_value else matches << i if stored_value.downcase.include? value.to_s.downcase end elsif stored_value.kind_of? Integer matches << i if stored_value == value.to_i end end matches end
find_by_id(id)
click to toggle source
find by id should be exact, so we remove the array wrapper
# File lib/tempo/models/base.rb, line 119 def find_by_id(id) matches = find "id", id match = matches[0] end
id_counter()
click to toggle source
Maintain an array of unique ids for the class. Initialize new members with the next numberical id Ids can be assigned on init (for the purpose of reading in records of previous instances). An error will be raised if there is already an instance with that id.
# File lib/tempo/models/base.rb, line 27 def id_counter @id_counter ||= 1 end
ids()
click to toggle source
# File lib/tempo/models/base.rb, line 31 def ids @ids ||= [] end
index()
click to toggle source
# File lib/tempo/models/base.rb, line 35 def index @index ||= [] end
instances_have_attributes?(attrs)
click to toggle source
# File lib/tempo/models/base.rb, line 84 def instances_have_attributes?(attrs) finder_attrs = attrs.map { |key| "@#{key}".to_sym } model_attrs = index.map { |i| i.instance_variables }.flatten.uniq # Make sure all attributes are present in index objects (model_attrs & finder_attrs).size == finder_attrs.size end
method_missing(meth, *args, &block)
click to toggle source
Calls superclass method
# File lib/tempo/models/base.rb, line 54 def method_missing(meth, *args, &block) if respond_to? meth if meth.to_s =~ /^find_by_(.+)$/ self.class.send(:define_method, meth) do |*args| run_find_by_method($1, *args) end self.send(meth, *args) elsif meth.to_s =~ /^sort_by_(.+)$/ self.class.send(:define_method, meth) do |*args, &block| run_sort_by_method($1, *args, &block) end self.send(meth, *args, &block) else super end else super end end
new(options={})
click to toggle source
# File lib/tempo/models/base.rb, line 153 def initialize(options={}) id_candidate = options[:id] if !id_candidate @id = self.class.next_id elsif self.class.ids.include? id_candidate raise IdentityConflictError, "Id #{id_candidate} already exists" else @id = id_candidate end self.class.add_id @id self.class.add_to_index self end
read_from_file(options={})
click to toggle source
pass custom directory through in options
# File lib/tempo/models/base.rb, line 50 def read_from_file(options={}) FileRecord::Record.read_model self, options end
respond_to?(method_id, include_private = false)
click to toggle source
Calls superclass method
# File lib/tempo/models/base.rb, line 76 def respond_to?(method_id, include_private = false) if method_id.to_s =~ /^find_by_(.+)$/ || method_id.to_s =~ /^sort_by_(.+)$/ instances_have_attributes? $1.split('_and_') else super end end
run_find_by_method(attrs, *args)
click to toggle source
# File lib/tempo/models/base.rb, line 99 def run_find_by_method(attrs, *args) # Make an array of attribute names attrs = attrs.split('_and_') attrs_with_args = [attrs, args].transpose filtered = index.clone attrs_with_args.each do | kv | matches = find kv[0], kv[1] return matches if matches.empty? matches.each do |match| matches.delete match unless filtered.include? match filtered = matches end end filtered end
run_sort_by_method(attribute, args=@index.clone, &block)
click to toggle source
# File lib/tempo/models/base.rb, line 92 def run_sort_by_method(attribute, args=@index.clone, &block) attr = "@#{attribute}".to_sym args.sort! { |a,b| a.instance_variable_get( attr ) <=> b.instance_variable_get( attr ) } return args unless block block.call args end
save_to_file(options={})
click to toggle source
pass custom directory through in options
# File lib/tempo/models/base.rb, line 45 def save_to_file(options={}) FileRecord::Record.save_model self, options end
Protected Class Methods
add_id(id)
click to toggle source
# File lib/tempo/models/base.rb, line 190 def self.add_id(id) @ids ||=[] @ids << id @ids.sort! end
add_to_index(member)
click to toggle source
# File lib/tempo/models/base.rb, line 184 def self.add_to_index(member) @index ||= [] @index << member @index.sort! { |a,b| a.id <=> b.id } end
increase_id_counter()
click to toggle source
# File lib/tempo/models/base.rb, line 196 def self.increase_id_counter @id_counter ||= 0 @id_counter = @id_counter.next end
next_id()
click to toggle source
# File lib/tempo/models/base.rb, line 201 def self.next_id while ids.include? id_counter increase_id_counter end id_counter end
Public Instance Methods
delete()
click to toggle source
# File lib/tempo/models/base.rb, line 178 def delete self.class.delete self end
freeze_dry()
click to toggle source
record the state of all instance variables as a hash
# File lib/tempo/models/base.rb, line 167 def freeze_dry record = {} state = instance_variables state.each do |attr| key = attr[1..-1].to_sym val = instance_variable_get attr record[key] = val end record end