Module: S3DB::Record

Defined in:
lib/s3db/record.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

Instance Method Details

#_idObject



155
156
157
# File 'lib/s3db/record.rb', line 155

def _id
  @data['_id']
end

#_id=(id) ⇒ Object



151
152
153
# File 'lib/s3db/record.rb', line 151

def _id=(id)
  @data['_id'] = id
end

#initialize(data) ⇒ Object

Instantiate a new record.

data - Hash of data. Required.

returns a new instance of the record.



96
97
98
99
100
101
102
103
# File 'lib/s3db/record.rb', line 96

def initialize(data)
  hash = {}
  data.each_pair do |k,v|
    hash[k.to_s] = v
  end

  @data = hash
end

#new_record?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/s3db/record.rb', line 105

def new_record?
  _id.nil?
end

#saveObject

Save an instantiated record.

returns the record on success or failure.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/s3db/record.rb', line 112

def save
  _set_id

  return false if _id.nil?

  self.class._database.backend.write_record(
    self.class._database.name,
    self.class._collection.name,
    _filename,
    @data.to_json
  )

  self
end

#save!Object

Save an instantiated record, raising an error on failure.

returns the record on success; raises an error on failure.



130
131
132
# File 'lib/s3db/record.rb', line 130

def save!
  save || raise(ArgumentError, 'failed to save!')
end

#update(data) ⇒ Object

Update the data for a record and save.

data - Hash of data for the record. Required.

returns #save.



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/s3db/record.rb', line 139

def update(data)
  return false if _id.nil?

  # Copy the existing id to the new data, if it exists.
  data.merge('_id' => _id)

  # Update the dataset
  @data = data

  save
end