Module: FlexiModel::ArPersistence::ClassMethods

Defined in:
lib/flexi_model/ar_persistence.rb

Instance Method Summary collapse

Instance Method Details

#create(attributes = { }) ⇒ Object

Create does exactly as ‘save`, but it initiates `:create` callbacks



94
95
96
97
98
99
# File 'lib/flexi_model/ar_persistence.rb', line 94

def create(attributes = { })
  inst = self.new(attributes)
  inst.save

  inst
end

#delete_allObject



89
90
91
# File 'lib/flexi_model/ar_persistence.rb', line 89

def delete_all;
  RECORD.by_namespace(self.get_flexi_namespace).delete_all
end

#destroy_allObject



82
83
84
85
86
87
# File 'lib/flexi_model/ar_persistence.rb', line 82

def destroy_all;
  RECORD.by_namespace(self.get_flexi_namespace).each do |record|
    inst = initialize_with_record(record)
    inst.destroy
  end
end

#flexi_collectionObject



76
77
78
# File 'lib/flexi_model/ar_persistence.rb', line 76

def flexi_collection;
  _flexi_collection
end

#flexi_collection_nameObject

Return collection name based on parametrized class name



51
52
53
# File 'lib/flexi_model/ar_persistence.rb', line 51

def flexi_collection_name
  self.name.parameterize
end

#flexi_label(singular = false) ⇒ Object

Return by default plural label



68
69
70
71
72
73
74
# File 'lib/flexi_model/ar_persistence.rb', line 68

def flexi_label(singular = false)
  if singular
    self._flexi_collection.singular_label
  else
    self._flexi_collection.plural_label
  end
end

#get_flexi_labelObject

Return singular and plural collection label If not defined it will take class name as collection name

Returns array of singular and plural labels



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/flexi_model/ar_persistence.rb', line 38

def get_flexi_label
  labels = [[:label_singular],
            [:label_plural]].compact

  if labels.empty?
    _f_name = _friendly_name(self.name)
    [_f_name.singularize, _f_name.pluralize]
  else
    labels
  end
end

#get_flexi_namespaceObject



55
56
57
# File 'lib/flexi_model/ar_persistence.rb', line 55

def get_flexi_namespace
  self.flexi_collection_name.parameterize
end

#initialize_with_record(record) ⇒ Object

Initialize new instance and set data from record



60
61
62
63
64
65
# File 'lib/flexi_model/ar_persistence.rb', line 60

def initialize_with_record(record)
  inst = self.new
  inst.send(:_record=, record)
  inst.send(:_id=, record.id)
  inst
end

#set_flexi_label(singular, plural) ⇒ Object

Set collection label

singular - Set singular name for the collection plural - Set plural name for the collection



29
30
31
32
# File 'lib/flexi_model/ar_persistence.rb', line 29

def set_flexi_label(singular, plural)
  [:label_singular] = singular
  [:label_plural]   = plural
end