Module: Mince::DataModel

Extended by:
ActiveSupport::Concern
Included in:
Timestamps
Defined in:
lib/mince/data_model.rb,
lib/mince/data_model/timestamps.rb

Overview

DataModel

Mince::DataModel is used as a mixin to easily mixin behavior into an object that you wish to act as a data model and interact with mince data interfaces.

Simply mixin this module in order to get a wrapper class to interact with a mince interface for a specific collection

Example:

require 'mince/data_model'

class UserDataModel
include Mince::DataModel

data_collection :users
data_fields :username, :first_name, :last_name, :email, :password
end

To access methods on the data model do the following:

user = User.new first_name: 'Matt'  # initialize a user from some User class
user.id = UserDataModel.store user  # returns the id of the stored user

UserDataModel.find 1                # => returns the data for the user with an id of 1
UserDataModel.find_all              # => returns all users

View the docs for each method available

Defined Under Namespace

Modules: ClassMethods, Timestamps

Instance Method Summary collapse

Instance Method Details

#add_to_interfaceObject



337
338
339
# File 'lib/mince/data_model.rb', line 337

def add_to_interface
  interface.add(data_collection, attributes)
end

#attributesObject



345
346
347
348
349
# File 'lib/mince/data_model.rb', line 345

def attributes
  model_instance_values.merge(primary_key => id).tap do |hash|
    hash.merge!(timestamp_attributes) if timestamps?
  end
end

#createObject



303
304
305
306
307
# File 'lib/mince/data_model.rb', line 303

def create
  update_timestamps if timestamps?
  add_to_interface
  id
end

#data_collectionObject



333
334
335
# File 'lib/mince/data_model.rb', line 333

def data_collection
  self.class.data_collection
end

#data_fieldsObject



322
323
324
325
326
327
# File 'lib/mince/data_model.rb', line 322

def data_fields
  if infer_fields?
    self.class.data_fields *model.fields
  end
  self.class.data_fields
end

#infer_fields?Boolean

Returns:

  • (Boolean)


329
330
331
# File 'lib/mince/data_model.rb', line 329

def infer_fields?
  self.class.infer_fields?
end

#initialize(model) ⇒ Object



297
298
299
300
301
# File 'lib/mince/data_model.rb', line 297

def initialize(model)
  @model = model
  set_data_field_values
  set_id
end

#interfaceObject



318
319
320
# File 'lib/mince/data_model.rb', line 318

def interface
  self.class.interface
end

#replace_in_interfaceObject



341
342
343
# File 'lib/mince/data_model.rb', line 341

def replace_in_interface
  interface.replace(data_collection, attributes)
end

#timestamps?Boolean

Returns:

  • (Boolean)


314
315
316
# File 'lib/mince/data_model.rb', line 314

def timestamps?
  self.class.timestamps?
end

#updateObject



309
310
311
312
# File 'lib/mince/data_model.rb', line 309

def update
  update_timestamps if timestamps?
  replace_in_interface
end