Module: SimplyCouch::Model::Persistence

Defined in:
lib/simply_couch/model/persistence.rb

Defined Under Namespace

Modules: ActiveModelCompliance, Callbacks, DirtyAttributes, ForbiddenAttributesProtection, Json, MagicTimestamps, Properties, PropertyMethods, Revisions, Validation Classes: SimpleProperty, TypeCaster

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/simply_couch/model/persistence.rb', line 11

def self.included(base)
  base.instance_variable_set(:@properties, nil) if base.instance_variable_defined?(:@properties)
  base.send :include, Properties
  base.send :include, Callbacks
  base.send :include, Json
  base.send :include, DirtyAttributes
  base.send :include, MagicTimestamps
  base.send :include, ActiveModelCompliance
  base.send :include, ForbiddenAttributesProtection
  base.send :include, Revisions
  base.send :include, Validation
  base.send :include, View::CustomViews
  base.send :include, View::Lists

  base.class_eval do
    attr_accessor :_id, :_rev, :_deleted, :_attachments, :database
    alias_method :id, :_id
    alias_method :id=, :_id=
  end
end

Instance Method Details

#==(other) ⇒ Object



61
62
63
# File 'lib/simply_couch/model/persistence.rb', line 61

def ==(other)
  super || (self.class == other.class && self._id.present? && self._id == other._id)
end

#[](attribute) ⇒ Object



55
# File 'lib/simply_couch/model/persistence.rb', line 55

def [](attribute); public_send(attribute); end

#[]=(attribute, value) ⇒ Object



54
# File 'lib/simply_couch/model/persistence.rb', line 54

def []=(attribute, value); public_send("#{attribute}=", value); end

#attributesObject



47
48
49
50
51
52
# File 'lib/simply_couch/model/persistence.rb', line 47

def attributes
  self.class.properties.inject(ActiveSupport::HashWithIndifferentAccess.new) do |res, property|
    property.value(res, self)
    res
  end
end

#attributes=(hash) ⇒ Object



43
44
45
# File 'lib/simply_couch/model/persistence.rb', line 43

def attributes=(hash)
  hash.each { |attribute, value| self.public_send "#{attribute}=", value }
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


64
# File 'lib/simply_couch/model/persistence.rb', line 64

def eql?(other); self == other; end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


56
# File 'lib/simply_couch/model/persistence.rb', line 56

def has_key?(key); attributes.has_key?(key); end

#hashObject



65
# File 'lib/simply_couch/model/persistence.rb', line 65

def hash; _id.hash * (_id.hash.to_s.size ** 10) + _rev.hash; end

#initialize(attributes = {}) {|_self| ... } ⇒ Object

── initialize / attributes ────────────────────────────────────────

Yields:

  • (_self)

Yield Parameters:



34
35
36
37
38
39
40
41
# File 'lib/simply_couch/model/persistence.rb', line 34

def initialize(attributes = {})
  if attributes
    @skip_dirty_tracking = true
    self.attributes = attributes
    @skip_dirty_tracking = false
  end
  yield self if block_given?
end

#inspectObject



68
69
70
71
# File 'lib/simply_couch/model/persistence.rb', line 68

def inspect
  attrs = attributes.map {|k,v| "#{k}: #{v.inspect}"}.join(", ")
  %Q{#<#{self.class} _id: "#{_id}", _rev: "#{_rev}", #{attrs}>}
end

#new?Boolean Also known as: new_record?

Returns:

  • (Boolean)


57
# File 'lib/simply_couch/model/persistence.rb', line 57

def new?; _rev.nil?; end

#reloadObject



66
# File 'lib/simply_couch/model/persistence.rb', line 66

def reload; database.load id; end

#to_paramObject



59
# File 'lib/simply_couch/model/persistence.rb', line 59

def to_param; _id; end