Class: RnDB::Table

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Defined in:
lib/rndb/table.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Table

Create a new record wit the given ID.



11
12
13
14
# File 'lib/rndb/table.rb', line 11

def initialize(id)
  _validate!
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/rndb/table.rb', line 8

def id
  @id
end

Class Method Details

.[](index) ⇒ Object

Return a new record corresponding to the specified index.



80
81
82
83
# File 'lib/rndb/table.rb', line 80

def [](index)
  _validate!
  new(index) if index < count
end

.allObject

Return all records.



92
93
94
# File 'lib/rndb/table.rb', line 92

def all
  where
end

.association(attribute, *args) ⇒ Object

Add an association between two Table models.



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/rndb/table.rb', line 147

def association(attribute, *args)
  args.each do |arg|
    _schema[:associations][attribute] = arg
  end
  define_method("#{attribute}_id".to_sym) do
    _generate_association_id(attribute)
  end
  define_method(attribute) do
    _generate_association(attribute)
  end
end

.column(attribute, *args) ⇒ Object

Add a new column to the Table model.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rndb/table.rb', line 122

def column(attribute, *args)
  column = _schema[:columns][attribute]
  args.each do |arg|
    index =
      case arg
      when Hash, Array
        :distribution
      when Proc
        :generator
      else
        raise "unsupported column parameter"
      end
    column[index] = arg
  end
  if column[:generator] && column[:distribution]
    define_method("#{attribute}_key") do
      _generate_column_key(attribute)
    end
  end
  define_method(attribute) do
    _generate_column(attribute)
  end
end

.countObject

Count all records, delegating this to the all Query.



97
98
99
# File 'lib/rndb/table.rb', line 97

def count
  all.count
end

.each(&block) ⇒ Object

Iterate over all records, delegating this to the all Query



107
108
109
# File 'lib/rndb/table.rb', line 107

def each(&block)
  all.each(&block)
end

.get(attribute) ⇒ Object



206
207
208
209
210
211
212
213
214
215
# File 'lib/rndb/table.rb', line 206

def get(attribute)
  raise unless @current
  if _schema[:columns].key?(attribute)
    value(@current, attribute)
  elsif _schema[:associations].key?(attribute)
    join(@current, attribute)
  else
    raise "no such attribute"
  end
end

.join(id, name) ⇒ Object

Return the instance joined to the current ID.



197
198
199
200
201
202
203
204
# File 'lib/rndb/table.rb', line 197

def join(id, name)
  @current = id
  _schema[:associations][name].each do |context|
    next unless (index = where(context[:where]).index(id))
    return where(context[:joins])[index]
  end
  nil
end

.key(id, attribute) ⇒ Object

Retrieve the key that can be queried on for generated attributes.



167
168
169
170
171
172
173
174
175
# File 'lib/rndb/table.rb', line 167

def key(id, attribute)
  @current = id
  _validate!
  column = _schema[:columns][attribute]
  return if column[:distribution].nil?
  column[:mapping].find do |_, ids|
    ids.include?(id)
  end&.first
end

.lastObject

Return the last record, to be consistent with #first, which we get by magic.



102
103
104
# File 'lib/rndb/table.rb', line 102

def last
  all.last
end

.pluck(*args) ⇒ Object

Pluck specified attributes from all records, delegating this to the all query.



112
113
114
# File 'lib/rndb/table.rb', line 112

def pluck(*args)
  all.pluck(args)
end

.rand(*args) ⇒ Object

Generate a random number, intended to be used in lambdas. The number will have been seeded appropriately to ensure determinism.



161
162
163
164
# File 'lib/rndb/table.rb', line 161

def rand(*args)
  _validate!
  _db.prng.rand(*args)
end

.sample(limit = 1) ⇒ Object

Return a Querty that contains a random sampling of records.



117
118
119
# File 'lib/rndb/table.rb', line 117

def sample(limit=1)
  all.sample(limit)
end

.table_nameObject

Return the name of the table, which is derived from the class name.



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

def table_name
  name.downcase.to_sym
end

.value(id, attribute) ⇒ Object

Retrieve the value of the given attribute for the given ID.



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/rndb/table.rb', line 178

def value(id, attribute)
  @current = id
  _validate!
  return id if attribute == :id
  column = _schema[:columns][attribute]
  value = key(id, attribute)
  unless column[:generator].nil?
    _seed_prng(id, attribute)
    value =
      if column[:distribution].nil?
        column[:generator].call
      else
        column[:generator].call(value)
      end
  end
  value
end

.where(constraints = {}) ⇒ Object

Return a Query that matches the supplied constraints



86
87
88
89
# File 'lib/rndb/table.rb', line 86

def where(constraints={})
  _validate!
  Query.new(self, _query(constraints, _schema[:size]))
end

Instance Method Details

#attributesObject

Generate all attributes, which may be expensive.



17
18
19
# File 'lib/rndb/table.rb', line 17

def attributes
  _generate_all
end

#to_hObject

Return the attributes as a hash.



22
23
24
# File 'lib/rndb/table.rb', line 22

def to_h
  attributes
end

#to_sObject

Return a stringified version of the attributes hash.



27
28
29
# File 'lib/rndb/table.rb', line 27

def to_s
  to_h.to_s
end