Class: S3DB::Collection

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database, name) {|_self| ... } ⇒ Collection

Instantiate a new collection, without writing it to disk.

database - Database attached to collection. Required. name - String name of the collection. Required.

returns a new Collection, validated but unwritten.

Yields:

  • (_self)

Yield Parameters:



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/s3db/collection.rb', line 26

def initialize(database, name)

  # Store the database and collection name
  @database = database
  @name = Utils.sanitize(name)

  # Sanity check the database and collection name
  validate!

  # Yield self for configs, if people want to.
  yield self if block_given?
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



3
4
5
# File 'lib/s3db/collection.rb', line 3

def database
  @database
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/s3db/collection.rb', line 3

def name
  @name
end

Class Method Details

.create(database, name) ⇒ Object

Create a new collection.

database - Database attached to collection. Required. name - String name of the collection. Required.

returns a new Collection.



12
13
14
15
16
17
# File 'lib/s3db/collection.rb', line 12

def create(database, name)
  collection = new(database, name)
  collection.save

  collection
end

Instance Method Details

#list_recordsObject



54
55
56
57
58
# File 'lib/s3db/collection.rb', line 54

def list_records
  @database.backend.list_records(@database.name, @name).map do |file|
    @database.backend.read_record(@database.name, @name, file)
  end
end

#saveObject

Write the collection skeleton to disk.

Returns nil.



63
64
65
66
67
68
# File 'lib/s3db/collection.rb', line 63

def save
  @database.backend.write_collection(@database.name, @name)
  @database.backend.write_schema(@database.name, @name, @schema.to_json)

  nil
end

#validate!Object

Validate a collection to ensure that it’s sane.

returns nil on success; raises an error on failure.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/s3db/collection.rb', line 42

def validate!
  unless @database.is_a?(S3DB::Database)
    raise ArgumentError, 'database must be an S3DB::Database!'
  end

  unless @name.is_a?(String)
    raise ArgumentError, 'name must be a String!'
  end

  nil
end