Class: Rbdb::Table

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

Constant Summary collapse

ALLOWED_TYPES =
[:string, :integer, :float, :boolean]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Table

Returns a new instance of Table.



9
10
11
12
13
14
# File 'lib/rbdb/table.rb', line 9

def initialize(name, options = {})
  @name = name
  @options = options
  @fields = {}
  @records = {}
end

Instance Attribute Details

#fieldsObject

Returns the value of attribute fields.



7
8
9
# File 'lib/rbdb/table.rb', line 7

def fields
  @fields
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/rbdb/table.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/rbdb/table.rb', line 7

def options
  @options
end

#recordsObject (readonly)

Returns the value of attribute records.



7
8
9
# File 'lib/rbdb/table.rb', line 7

def records
  @records
end

Instance Method Details

#delete(record_id) ⇒ Object



75
76
77
# File 'lib/rbdb/table.rb', line 75

def delete(record_id)
  @records.delete("#{record_id}")
end

#find_all(fields) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rbdb/table.rb', line 48

def find_all(fields)
  found = []
  if fields.length == 1
    return [find_by_id(fields.values.first)] if fields.keys.first == 'id'
    find(fields) do |record|
      found << record
    end
  else
    find_multiple_fields(fields) do |record|
      found << record
    end
  end

  found
end

#find_by(fields) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rbdb/table.rb', line 29

def find_by(fields)
  if fields.length == 1
    return find_by_id(fields.values.first) if fields.keys.first.to_s == 'id'
    find(fields) do |record|
      return record
    end
    return nil
  else
    find_multiple_fields(fields) do |record|
      return record
    end
    nil
  end
end

#find_by_id(id) ⇒ Object



44
45
46
# File 'lib/rbdb/table.rb', line 44

def find_by_id(id)
  return @records["#{id}"]
end

#insert(record) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/rbdb/table.rb', line 21

def insert(record)
  check_record(record) do
    id = records.length + 1
    @records[id.to_s] = record.merge!({:id => id})
    return record.merge({:id => id})
  end
end

#update(record) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/rbdb/table.rb', line 64

def update(record)
  id = record[:id]
  if find_by_id(record[:id])
    record.delete(:id)
    check_record(record) do
      @records["#{id}"] = record.merge!({:id => id})
      return record
    end
  end
end