Class: Yequel::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/yequel.rb

Defined Under Namespace

Classes: Array, Hash

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Model

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


23
24
25
26
# File 'lib/yequel.rb', line 23

def initialize(name)
  @name = name
  load       
end

Instance Attribute Details

#nameObject

, :rkeys



11
12
13
# File 'lib/yequel.rb', line 11

def name
  @name
end

#rarrayObject

, :rkeys



11
12
13
# File 'lib/yequel.rb', line 11

def rarray
  @rarray
end

Instance Method Details

#[](key) ⇒ Object

is query command that shows a record base on the :id key

Examples:

Artist[1]


171
172
173
174
175
176
177
# 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

#allObject

All is query command that shows all records in the dataset

Examples:

Artist.all


155
156
157
# File 'lib/yequel.rb', line 155

def all
  @rarray
end

#countObject

Count is query command that counts the records in the dataset

Examples:

Artist.count


163
164
165
# File 'lib/yequel.rb', line 163

def count
  @rarray.length
end

#delete(key) ⇒ Object

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)


119
120
121
122
123
124
125
126
127
# 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 = {}) ⇒ Object

First is query command that shows the first record in the dataset

Examples:

Artist.first
Artist.first(:name=>'AS')


135
136
137
138
139
140
141
# 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) ⇒ Object

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")


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# 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

#lastObject

Last is query command that shows the last record in the dataset

Examples:

Artist.last


147
148
149
# File 'lib/yequel.rb', line 147

def last
  @rarray.reverse.first    
end

#loadObject

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


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Object

Order is a query command that orders an array based on a key This command is chainable



181
182
183
184
185
186
# 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

#storeObject



64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object

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")


103
104
105
106
107
108
109
110
111
112
# 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) ⇒ Object

Where is a query command that filters the array This command is chainable



190
191
192
# File 'lib/yequel.rb', line 190

def where(*args)
  @rarray.where(*args)
end