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' user.id = UserDataModel.store user
UserDataModel.find 1 UserDataModel.find_all
View the docs for each method available
Defined Under Namespace
Modules: ClassMethods, Timestamps
Instance Method Summary
collapse
Instance Method Details
#add_to_interface ⇒ Object
337
338
339
|
# File 'lib/mince/data_model.rb', line 337
def add_to_interface
interface.add(data_collection, attributes)
end
|
#attributes ⇒ Object
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
|
#create ⇒ Object
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_collection ⇒ Object
333
334
335
|
# File 'lib/mince/data_model.rb', line 333
def data_collection
self.class.data_collection
end
|
#data_fields ⇒ Object
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
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
|
#interface ⇒ Object
318
319
320
|
# File 'lib/mince/data_model.rb', line 318
def interface
self.class.interface
end
|
#replace_in_interface ⇒ Object
341
342
343
|
# File 'lib/mince/data_model.rb', line 341
def replace_in_interface
interface.replace(data_collection, attributes)
end
|
#timestamps? ⇒ Boolean
314
315
316
|
# File 'lib/mince/data_model.rb', line 314
def timestamps?
self.class.timestamps?
end
|
#update ⇒ Object
309
310
311
312
|
# File 'lib/mince/data_model.rb', line 309
def update
update_timestamps if timestamps?
replace_in_interface
end
|