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.



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

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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

Class Method Details

.[](index) ⇒ Object

Return a new record corresponding to the specified index.



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

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

.allObject

Return all records.



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

def all
  where
end

.association(attribute, *args) ⇒ Object

Add an association between two Table models.



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

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.



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

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
  define_method("#{attribute}=".to_sym) do |value|
    self.class.send(:_set_state, id, attribute, value)
  end
end

.countObject

Count all records, delegating this to the all Query.



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

def count
  all.count
end

.each(&block) ⇒ Object

Iterate over all records, delegating this to the all Query



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

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

.get(attribute) ⇒ Object



210
211
212
213
214
215
216
217
218
219
# File 'lib/rndb/table.rb', line 210

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.



201
202
203
204
205
206
207
208
# File 'lib/rndb/table.rb', line 201

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.



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

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.



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

def last
  all.last
end

.pluck(*args) ⇒ Object

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



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

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.



163
164
165
166
# File 'lib/rndb/table.rb', line 163

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

.sample(limit = 1) ⇒ Object

Return a Querty that contains a random sampling of records.



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

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

.table_nameObject

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



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

def table_name
  name.downcase.to_sym
end

.value(id, attribute) ⇒ Object

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



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

def value(id, attribute)
  @current = id
  _validate!
  return id if attribute == :id
  override = _get_state(id, attribute)
  return override unless override.nil?
  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



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

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

Instance Method Details

#attributesObject

Generate all attributes, which may be expensive.



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

def attributes
  _generate_all
end

#to_hObject

Return the attributes as a hash.



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

def to_h
  attributes
end

#to_sObject

Return a stringified version of the attributes hash.



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

def to_s
  to_h.to_s
end