class Yequel::Model

Attributes

name[RW]
rarray[RW]

Public Class Methods

new(name) click to toggle source

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

[](key) click to toggle source

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() click to toggle source

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() click to toggle source

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(key) click to toggle source

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(options ={}) click to toggle source

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(args) click to toggle source

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() click to toggle source

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
load() click to toggle source

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(key) click to toggle source

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
store() click to toggle source
# 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(args) click to toggle source

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(*args) click to toggle source

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