Module: Friendly::Document

Defined in:
lib/friendly/document.rb

Defined Under Namespace

Modules: ClassMethods

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.documentsObject



19
20
21
# File 'lib/friendly/document.rb', line 19

def documents
  @documents ||= []
end

Class Method Details

.create_tables!Object



23
24
25
# File 'lib/friendly/document.rb', line 23

def create_tables!
  documents.each { |d| d.create_tables! }
end

.included(klass) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/friendly/document.rb', line 9

def included(klass)
  documents << klass
  klass.class_eval do
    extend ClassMethods
    attribute :id,         UUID
    attribute :created_at, Time
    attribute :updated_at, Time
  end
end

Instance Method Details

#==(comparison_object) ⇒ Object



243
244
245
246
247
248
# File 'lib/friendly/document.rb', line 243

def ==(comparison_object)
  comparison_object.equal?(self) ||
    (comparison_object.is_a?(self.class) &&
      !comparison_object.new_record? && 
        comparison_object.id == id)
end

#attributes=(attrs) ⇒ Object



200
201
202
203
# File 'lib/friendly/document.rb', line 200

def attributes=(attrs)
  assert_no_duplicate_keys(attrs)
  attrs.each { |name, value| send("#{name}=", value) }
end

#destroyObject



214
215
216
# File 'lib/friendly/document.rb', line 214

def destroy
  storage_proxy.destroy(self)
end

#initialize(opts = {}) ⇒ Object



196
197
198
# File 'lib/friendly/document.rb', line 196

def initialize(opts = {})
  self.attributes = opts
end

#new_recordObject



230
231
232
233
# File 'lib/friendly/document.rb', line 230

def new_record
  @new_record = true if @new_record.nil?
  @new_record
end

#new_record=(value) ⇒ Object



235
236
237
# File 'lib/friendly/document.rb', line 235

def new_record=(value)
  @new_record = value
end

#new_record?Boolean

Returns:



226
227
228
# File 'lib/friendly/document.rb', line 226

def new_record?
  new_record
end

#saveObject



205
206
207
# File 'lib/friendly/document.rb', line 205

def save
  new_record? ? storage_proxy.create(self) : storage_proxy.update(self)
end

#storage_proxyObject



239
240
241
# File 'lib/friendly/document.rb', line 239

def storage_proxy
  self.class.storage_proxy
end

#table_nameObject



222
223
224
# File 'lib/friendly/document.rb', line 222

def table_name
  self.class.table_name
end

#to_hashObject



218
219
220
# File 'lib/friendly/document.rb', line 218

def to_hash
  Hash[*self.class.attributes.keys.map { |n| [n, send(n)] }.flatten]
end

#update_attributes(attributes) ⇒ Object



209
210
211
212
# File 'lib/friendly/document.rb', line 209

def update_attributes(attributes)
  self.attributes = attributes
  save
end