class Yequel::Model
Attributes
Public Class Methods
Initialize the model object. Provides a wrapper around the dataset Each table is created as unique class, and an instance is created
Example:
class Artists < Yequel::Model end Artist=Artists.new
# File lib/yequel.rb, line 23 def initialize(name) @name = name load end
Public Instance Methods
-
is query command that shows a record base on the :id key
Examples:
Artist[1]
# File lib/yequel.rb, line 171 def[](key) #k = @rkeys.index(key) #@rarray[k] p key #p self.rarray self.rarray.get(key).merge!({:mod_name=>self.name}) end
All is query command that shows all records in the dataset
Examples:
Artist.all
# File lib/yequel.rb, line 155 def all @rarray end
Count is query command that counts the records in the dataset
Examples:
Artist.count
# File lib/yequel.rb, line 163 def count @rarray.length end
Delete is an action command that deletes a single record An argument error is thrown if the record key does not exist
Examples:
Artist.delete(4)
# File lib/yequel.rb, line 119 def delete(key) result = @rarray.get(key) if result @rarray.delete(result) store else raise ArgumentError,'Cannot delete - no matching id', caller end end
First is query command that shows the first record in the dataset
Examples:
Artist.first Artist.first(:name=>'AS')
# File lib/yequel.rb, line 135 def first(options ={}) if options.length > 0 @rarray.where(options).first.merge!({:mod_name=>self.name}) else @rarray.first.merge!({:mod_name=>self.name}) end end
Insert is an action command and adds new records to the dataset All data is stored in string format An argument error is thrown if the record key is already used
Examples:
Artist.insert(:id=>1, :name=>"YJ")
# File lib/yequel.rb, line 82 def insert (args) #p @name #p '.......' p args p self.rarray if self.rarray.get(args[:id]).nil? model={:mod_name=>@name} p self #self.rarray << args.merge(model) self.rarray << args self.store else raise ArgumentError, 'Cannot insert - id already exists', caller end end
Last is query command that shows the last record in the dataset
Examples:
Artist.last
# File lib/yequel.rb, line 147 def last @rarray.reverse.first end
Loads the dataset with the name provided from initialization Method is also used to refresh data after a dataset action YAML::Store data will be loaded into a hash image (:dshash) that is accessible using the :id number as a key Hash
will be converted to a Array
of hashed records to support querying of the dataset
Example:
load
# File lib/yequel.rb, line 37 def load #p 'loading ...' #p @name @rarray = Array.new begin dshash = YAML.load_file('db/'+@name+'.store') #p dshash #@rkeys = Array.new #p 'loading ...' dshash.each {|k,v| # converts strings into symbols cid = dshash[k]["id"] next if cid < 1 # do not transform if id < 1 #@rkeys << k rhash = Hash.new v.each {|k2,v2| #p k2 #p v2 rhash[k2.to_sym] = v2 } @rarray << rhash } rescue p 'no file now' self.insert({:id=>0}) end end
Order is a query command that orders an array based on a key This command is chainable
# File lib/yequel.rb, line 181 def order(key) #newarray=Array.new #@dataset.values.each { |item| newarray << item.varray} #newarray.order(key) @rarray.order(key) end
# File lib/yequel.rb, line 64 def store dshash = Hash.new @rarray.each { |item| outhash = Hash.new item.each { |k,v| outhash[k.to_s] = v} dshash[item[:id]]=outhash } File.open('db/'+@name+'.store', 'w') do |out| # To file YAML.dump(dshash, out) end end
Update is an action command and updates existing records An argument error is thrown if the record key does not exist
Examples:
Artist.update(:id=>1, :name=>"YJM")
# File lib/yequel.rb, line 103 def update (args) p 'updating ...' if self.rarray.get(args[:id]) key = args[:id] - 1 @rarray[key].merge!(args) else raise ArgumentError, 'Cannot update - id not found', caller end self.store end
Where is a query command that filters the array This command is chainable
# File lib/yequel.rb, line 190 def where(*args) @rarray.where(*args) end