Module: HuggORM::Persistence::InstanceMethods

Defined in:
lib/hugg_orm/persistence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataObject

Returns the value of attribute data.



129
130
131
# File 'lib/hugg_orm/persistence.rb', line 129

def data
  @data
end

#uuidObject

Returns the value of attribute uuid.



129
130
131
# File 'lib/hugg_orm/persistence.rb', line 129

def uuid
  @uuid
end

Instance Method Details

#<=>(o) ⇒ Object



216
217
218
# File 'lib/hugg_orm/persistence.rb', line 216

def <=>(o)
  o.kind_of?(self.class) && @data == o.data ? 0 : nil
end

#created?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'lib/hugg_orm/persistence.rb', line 196

def created?
  @state == :created
end

#destroyObject



165
166
167
168
169
170
171
172
# File 'lib/hugg_orm/persistence.rb', line 165

def destroy
  if self.class.destroy(key)
    @data = {}
    @state = :destroyed
  end

  self
end

#destroyed?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/hugg_orm/persistence.rb', line 188

def destroyed?
  @state == :destroyed
end

#dumpObject



200
201
202
203
204
205
206
# File 'lib/hugg_orm/persistence.rb', line 200

def dump
  str = "#{self.class.name}:\n"
  @data.sort.each do |field, value|
    str << "  #{field}: #{value}\n"
  end
  str
end

#fetch(*args) ⇒ Object



208
209
210
# File 'lib/hugg_orm/persistence.rb', line 208

def fetch(*args)
  @data.send(:fetch, *args)
end

#has_field?(field) ⇒ Boolean

Returns:

  • (Boolean)


212
213
214
# File 'lib/hugg_orm/persistence.rb', line 212

def has_field?(field)
  @data.has_key?(field)
end

#initialize(data = {}) ⇒ Object



131
132
133
134
# File 'lib/hugg_orm/persistence.rb', line 131

def initialize(data = {})
  @data = {}
  update_attributes(data) unless data.empty?
end

#keyObject



136
137
138
# File 'lib/hugg_orm/persistence.rb', line 136

def key
  @data[self.class.key_field]
end

#saveObject



174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/hugg_orm/persistence.rb', line 174

def save
  updated_data = self.class.where("#{self.class.primary_key} = ?", [key]).update(@data, true)

  unless updated_data.empty?
    @data = updated_data.first
    @state = :updated
  else
    self.class.new_query.insert(@data)
    @state = :created
  end

  self
end

#set_idObject



140
141
142
143
144
# File 'lib/hugg_orm/persistence.rb', line 140

def set_id
  # Set id based on key field, but only if id isn't already set and
  # the key method has been overridden and returns data.
  @data[self.class.key_field] = key if @data[self.class.key_field].nil? && key
end

#update_attributes(update_data) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/hugg_orm/persistence.rb', line 146

def update_attributes(update_data)
  # Remove fields not set to be included
  unless self.class.include_fields.empty?
    update_data = self.class.include_fields.each_with_object({}) do |key, hash|
      hash[key] = update_data[key] if update_data.has_key? key
    end
  end

  # Update all fields unless they have been excluded
  update_data.each do |field, value|
    @data[field] = value unless self.class.exclude_fields.include?(field)
  end

  # Set id based on key method.
  set_id

  self
end

#updated?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/hugg_orm/persistence.rb', line 192

def updated?
  @state == :updated
end