Module: FlexiModel::ArPersistence

Extended by:
ActiveSupport::Concern
Included in:
FlexiModel
Defined in:
lib/flexi_model/ar_persistence.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

RECORD =
FlexiModel::ArModels::Record
COLLECTION =
FlexiModel::ArModels::Collection
FIELD =
FlexiModel::ArModels::Field
VALUE =
FlexiModel::ArModels::Value

Instance Method Summary collapse

Instance Method Details

#==(another_instance) ⇒ Object

Ensure object with same _id returns true on equality check



113
114
115
# File 'lib/flexi_model/ar_persistence.rb', line 113

def ==(another_instance)
  self._id && self._id == another_instance._id
end

#destroyObject



139
140
141
142
143
144
145
# File 'lib/flexi_model/ar_persistence.rb', line 139

def destroy
  if _id.present?
    RECORD.delete(self._id)
  else
    false
  end
end

#get_flexi_collectionObject

Return existing or create new collection set



158
159
160
161
162
# File 'lib/flexi_model/ar_persistence.rb', line 158

def get_flexi_collection
  _find_or_update_or_build_collection!

  self._flexi_collection
end

#get_flexi_fields_mapObject

Return flexi fields in name and field object map



165
166
167
168
169
# File 'lib/flexi_model/ar_persistence.rb', line 165

def get_flexi_fields_map
  self._flexi_fields_map ||=
      Hash[get_flexi_collection.fields.
               map { |_field| [_field.name.to_sym, _field] }]
end

#initializeObject



107
108
109
110
# File 'lib/flexi_model/ar_persistence.rb', line 107

def initialize(*)
  super
  _find_or_update_or_build_collection!
end

#load_attributes!Object

Forcefully load all attributes



153
154
155
# File 'lib/flexi_model/ar_persistence.rb', line 153

def load_attributes!
  self.flexi_fields.map { |f| self.send(f.name.to_sym) }
end

#new_record?Boolean

Return true if record is not saved

Returns:

  • (Boolean)


118
119
120
# File 'lib/flexi_model/ar_persistence.rb', line 118

def new_record?
  !_id.present?
end

#reloadObject

Reload object instance



148
149
150
# File 'lib/flexi_model/ar_persistence.rb', line 148

def reload
  self.class.find(self._id)
end

#saveObject

Store record in persistent storage



123
124
125
126
# File 'lib/flexi_model/ar_persistence.rb', line 123

def save
  create_or_update
  _id.present?
end

#update_attribute(key, value) ⇒ Object

Update single attribute by key and value



135
136
137
# File 'lib/flexi_model/ar_persistence.rb', line 135

def update_attribute(key, value)
  self.update_attributes(key => value)
end

#update_attributes(_params) ⇒ Object

Update stored attributes by give hash



129
130
131
132
# File 'lib/flexi_model/ar_persistence.rb', line 129

def update_attributes(_params)
  assign_attributes _params
  save
end