module Mince::DataModel

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

Attributes

id[R]

creates readonly attribute for id and model

model[R]

creates readonly attribute for id and model

Public Class Methods

new(model) click to toggle source
# File lib/mince/data_model.rb, line 297
def initialize(model)
  @model = model
  set_data_field_values
  set_id
end

Public Instance Methods

add_to_interface() click to toggle source
# File lib/mince/data_model.rb, line 337
def add_to_interface
  interface.add(data_collection, attributes)
end
attributes() click to toggle source
# 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() click to toggle source
# File lib/mince/data_model.rb, line 303
def create
  update_timestamps if timestamps?
  add_to_interface
  id
end
data_collection() click to toggle source
# File lib/mince/data_model.rb, line 333
def data_collection
  self.class.data_collection
end
data_fields() click to toggle source
# 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?() click to toggle source
# File lib/mince/data_model.rb, line 329
def infer_fields?
  self.class.infer_fields?
end
interface() click to toggle source
# File lib/mince/data_model.rb, line 318
def interface
  self.class.interface
end
replace_in_interface() click to toggle source
# File lib/mince/data_model.rb, line 341
def replace_in_interface
  interface.replace(data_collection, attributes)
end
timestamps?() click to toggle source
# File lib/mince/data_model.rb, line 314
def timestamps?
  self.class.timestamps?
end
update() click to toggle source
# File lib/mince/data_model.rb, line 309
def update
  update_timestamps if timestamps?
  replace_in_interface
end

Private Instance Methods

field_exists?(field) click to toggle source
# File lib/mince/data_model.rb, line 381
def field_exists?(field)
  model.respond_to?(field) && !model.send(field).nil?
end
generated_id() click to toggle source
# File lib/mince/data_model.rb, line 361
def generated_id
  self.class.generate_unique_id(model)
end
model_has_id?() click to toggle source
# File lib/mince/data_model.rb, line 365
def model_has_id?
  model.respond_to?(:id) && model.id
end
model_instance_values() click to toggle source
# File lib/mince/data_model.rb, line 353
def model_instance_values
  HashWithIndifferentAccess.new(model.instance_values).slice(*data_fields)
end
primary_key() click to toggle source
# File lib/mince/data_model.rb, line 373
def primary_key
  self.class.primary_key
end
set_data_field_value(field) click to toggle source
# File lib/mince/data_model.rb, line 377
def set_data_field_value(field)
  self.send("#{field}=", model.send(field)) if field_exists?(field)
end
set_data_field_values() click to toggle source
# File lib/mince/data_model.rb, line 369
def set_data_field_values
  data_fields.each { |field| set_data_field_value(field) }
end
set_id() click to toggle source
# File lib/mince/data_model.rb, line 357
def set_id
  @id = model_has_id? ? model.id : generated_id
end