Module: Dynameek::Model

Defined in:
lib/dynameek/model.rb,
lib/dynameek/model/query.rb,
lib/dynameek/model/dynamo_db.rb,
lib/dynameek/model/structure.rb

Defined Under Namespace

Modules: ClassMethods, DynamoDb, Query, Structure

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/dynameek/model.rb', line 3

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#attributesObject



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

def attributes
  @attributes ||= {}
end

#deleteObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dynameek/model.rb', line 73

def delete
  if self.class.range?
    range_val = self.class.convert_to_dynamodb(self.class.range_info.type, self.send(self.class.range_info.field))
    if self.class.index_table?
      self.class.table.batch_delete([[act_hash_key, attributes[:dynameek_index]]])
    else
      self.class.table.batch_delete([[hash_key, range_val]])
    end
  else  
    self.class.table.batch_delete([hash_key])
  end
end

#dynamo_itemObject



86
87
88
# File 'lib/dynameek/model.rb', line 86

def dynamo_item
  @dynamo_item ||= nil
end

#dynamo_item=(item) ⇒ Object



90
91
92
# File 'lib/dynameek/model.rb', line 90

def dynamo_item=(item)
  @dynamo_item = item
end

#read_attribute(fieldname) ⇒ Object



65
66
67
# File 'lib/dynameek/model.rb', line 65

def read_attribute(fieldname)
  attributes[fieldname]
end

#saveObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dynameek/model.rb', line 7

def save
  attribs = self.class.fields.reduce({}) do |memo, (field, type)|
    #Always call the read method in case it has been overwritten
    #Note that this is required for the multicolumn key
    val =  self.send field
    val = self.class.convert_to_dynamodb(type, val)
    memo[field] = val unless val.nil?
    memo
  end
  self.class.before_save_callbacks.each{|method| self.send method}
  #puts "---------\nDYNAMEEK DEBUG [SAVE]:"
  if self.class.index_table?
    rng = self.class.convert_to_dynamodb(
      self.class.range_info.type, 
      attributes[self.class.range_info.field]
    )
    curr_range = self.class.current_range(hash_key, rng) + 1
    index_attribs = {
      self.class.hash_key_info.field.to_s =>attribs[self.class.hash_key_info.field],
      self.class.range_info.field.to_s => attribs[self.class.range_info.field],
      "current_range_val"=> curr_range
    }    
    #puts "INDEX:"
    #puts index_attribs 
    self.class.index_table.batch_write(
      :put => [
        index_attribs
      ]
    )
    #puts "-!-"
    attribs[self.class.hash_key_info.field.to_s+"_"+self.class.range_info.field.to_s] = 
       attribs[self.class.hash_key_info.field].to_s +
       self.class.multi_column_join + 
       attribs[self.class.range_info.field].to_s
    attribs[:dynameek_index] = curr_range
    #puts "DATA:"
    #puts attribs
    self.class.table.batch_write(
        :put => [
        attribs
      ]
    )
  else
    #puts "DATA:"
    #puts attribs
    self.class.table.batch_write(
        :put => [
        attribs
      ]
    )
  end
  self
end

#write_attribute(fieldname, value) ⇒ Object



69
70
71
# File 'lib/dynameek/model.rb', line 69

def write_attribute(fieldname, value)
  attributes[fieldname] = value
end