Module: Makura::Model::InstanceMethods

Defined in:
lib/makura/model.rb

Instance Method Summary collapse

Instance Method Details

#==(obj) ⇒ Object



131
132
133
# File 'lib/makura/model.rb', line 131

def ==(obj)
  self.class == obj.class and self._id == obj._id
end

#[](key) ⇒ Object



63
64
65
# File 'lib/makura/model.rb', line 63

def [](key)
  @_hash[key.to_s]
end

#[]=(key, value) ⇒ Object



67
68
69
# File 'lib/makura/model.rb', line 67

def []=(key, value)
  @_hash[key.to_s] = value
end

#attach(*args) ⇒ Object

path, file, args = {})



109
110
111
112
113
# File 'lib/makura/model.rb', line 109

def attach(*args)
  response = self.class.database.put_attachment(self, *args)
  self._rev = response['rev']
  response
end

#cloneObject



143
144
145
146
147
148
# File 'lib/makura/model.rb', line 143

def clone
  hash = @_hash.dup
  hash.delete('_id')
  hash.delete('_rev')
  self.class.new(hash)
end

#destroyObject



127
128
129
# File 'lib/makura/model.rb', line 127

def destroy
  self.class.database.delete(_id, :rev => _rev)
end

#detach(name) ⇒ Object

delete attachment by name. we make sure the parameter is given and a nonempty string to avoid destroying the document itself



118
119
120
121
122
123
124
125
# File 'lib/makura/model.rb', line 118

def detach(name)
  name.strip!
  return if name.empty?
  self.class.database.request(:delete, "#{_id}/#{name}", :rev => _rev)
rescue Makura::Error::Conflict
  self['_rev'] = self.class[self._id]['_rev']
  retry
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/makura/model.rb', line 139

def eql?(other)
  other == self && other.hash == self.hash
end

#hashObject



135
136
137
# File 'lib/makura/model.rb', line 135

def hash
  @_hash.hash
end

#initialize(hash = {}) ⇒ Object



39
40
41
42
# File 'lib/makura/model.rb', line 39

def initialize(hash = {})
  @_hash = self.class.defaults.dup
  merge!(hash)
end

#inspectObject



79
80
81
# File 'lib/makura/model.rb', line 79

def inspect
  "#<#{self.class} #{@_hash.inspect}>"
end

#merge!(hash) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/makura/model.rb', line 44

def merge!(hash)
  case hash
  when Makura::Model
    merge!(hash.to_hash)
  when Hash
    hash.each{|key, value|
      meth = "#{key}="

      if respond_to?(meth)
        self.send("#{key}=", value)
      else
        self[key.to_s] = value
      end
    }
  else
    raise ArgumentError, "This is neither relation data nor an Hash"
  end
end

#pretty_print(o) ⇒ Object



83
84
85
# File 'lib/makura/model.rb', line 83

def pretty_print(o)
  ["#<#{self.class} ", @_hash, ">"].each{|e| e.pretty_print(o) }
end

#saveObject



87
88
89
90
# File 'lib/makura/model.rb', line 87

def save
  return unless valid? if respond_to?(:valid?)
  save!
end

#save!Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/makura/model.rb', line 92

def save!
  hash = self.to_hash

  self.class.makura_relation.each do |kind, relation_hash|
    relation_hash.each do |key, value|
      hash[key.to_s] = hash[key.to_s] #._id
    end
  end

  response = self.class.database.save(hash)
  self._rev = response['rev']
  self._id = response['id']

  return self
end

#to_hashObject



71
72
73
# File 'lib/makura/model.rb', line 71

def to_hash
  @_hash.dup
end

#to_json(*args) ⇒ Object



75
76
77
# File 'lib/makura/model.rb', line 75

def to_json(*args)
  @_hash.to_json(*args)
end