Module: RAWS::SDB::Model::InstanceMethods

Defined in:
lib/raws/sdb/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(_name, *args) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/raws/sdb/model.rb', line 114

def method_missing(_name, *args)
  name = _name.to_s
  if md = /(.+)=$/.match(name)
    self[md[1]] = args.unshift
  elsif values.key?(name)
    self[name]
  end
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



57
58
59
# File 'lib/raws/sdb/model.rb', line 57

def id
  @id
end

#valuesObject (readonly)

Returns the value of attribute values.



58
59
60
# File 'lib/raws/sdb/model.rb', line 58

def values
  @values
end

Instance Method Details

#[](key) ⇒ Object



73
74
75
# File 'lib/raws/sdb/model.rb', line 73

def [](key)
  values[key]
end

#[]=(key, val) ⇒ Object



77
78
79
# File 'lib/raws/sdb/model.rb', line 77

def []=(key, val)
  values[key] = val
end

#after_deleteObject



106
# File 'lib/raws/sdb/model.rb', line 106

def after_delete; end

#after_initializeObject



104
# File 'lib/raws/sdb/model.rb', line 104

def after_initialize; end

#after_insertObject



112
# File 'lib/raws/sdb/model.rb', line 112

def after_insert; end

#after_saveObject



108
# File 'lib/raws/sdb/model.rb', line 108

def after_save; end

#after_updateObject



110
# File 'lib/raws/sdb/model.rb', line 110

def after_update; end

#before_deleteObject



105
# File 'lib/raws/sdb/model.rb', line 105

def before_delete; end

#before_insertObject



111
# File 'lib/raws/sdb/model.rb', line 111

def before_insert; end

#before_saveObject



107
# File 'lib/raws/sdb/model.rb', line 107

def before_save; end

#before_updateObject



109
# File 'lib/raws/sdb/model.rb', line 109

def before_update; end

#create_idObject



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

def create_id
  self.class.create_id
end

#delete(attrs = {}) ⇒ Object



81
82
83
84
85
86
# File 'lib/raws/sdb/model.rb', line 81

def delete(attrs={})
  before_delete
  self.class.delete_attributes(id, attrs) if id
  @exists = false
  after_delete
end

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  @exists
end

#initialize(values = {}, id = nil, exists = false) ⇒ Object



60
61
62
63
# File 'lib/raws/sdb/model.rb', line 60

def initialize(values={}, id=nil, exists=false)
  @id, @values, @exists = id, values, exists
  after_initialize
end

#saveObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/raws/sdb/model.rb', line 88

def save
  before_save
  if exists?
    before_update
    self.class.put_attributes(id, values, *values.keys)
    after_update
  else
    before_insert
    @id ||= create_id
    self.class.put_attributes(id, values, *values.keys)
    @exists = true
    after_insert
  end
  after_save
end