Metrix DB
Simple DB base on ruby Array. The main usage is data mapping.
Notice: Do not use it in large dataset
Installation
gem install metrix_db
Usage
class MyDB
include MetrixDB
field :col1, :uniq => false
field :col2, :ref => true
dataset [[1,2],[3,4]]
end
- Include
MetrixDB - Define field using
field, the order of field should match index of your dataset - Set dataset, dataset is the real data of one DB
Field
Options for field definition
uniq: Check if column is unique. Default value is true.
ref: Support [] query syntax. If not set for all field will use first field as ref field
Query
mydb = MyDB.on(:col1 => 1) # nil or instance of MyDB.
mydb.col1 # => 1
mydb.col2 # => 2
use [] as query syntax, it will use ref field to query data.
mydb = MyDB[1] # nil or instance of MyDB.
mydb.col1 # => 1
mydb.col2 # => 2
Iteration
MyDB.all #=> Array of MyDB instance
MyDB.all.each do |inst|
puts inst.col1
end
MyDB.all.each_with_index do |inst, index|
puts inst.col1
end
MyDB.all.map { |inst| inst.col1 } # [1,2]