Class: Hanover::Persistence

Inherits:
Object
  • Object
show all
Defined in:
lib/hanover/persistence.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, key = nil) ⇒ Persistence

Returns a new instance of Persistence.



4
5
6
7
8
9
10
# File 'lib/hanover/persistence.rb', line 4

def initialize(content, key = nil)
  @content = content
  @key = key
  @klass = @content.class
  
  save
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



16
17
18
19
20
# File 'lib/hanover/persistence.rb', line 16

def method_missing(name, *args, &block)
  result = @content.send name, *args, &block
  save unless @robject.raw_data == @content.to_json
  result
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



3
4
5
# File 'lib/hanover/persistence.rb', line 3

def content
  @content
end

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/hanover/persistence.rb', line 3

def key
  @key
end

Class Method Details

.clientObject



46
47
48
49
# File 'lib/hanover/persistence.rb', line 46

def self.client
  return @client if defined? @client
  @client = Riak::Client.new http_port: 8091
end

.find(key) ⇒ Object



40
41
42
43
44
# File 'lib/hanover/persistence.rb', line 40

def self.find(key)
  p = allocate
  p.initialize_from_key key
  return p
end

Instance Method Details

#initialize_from_key(key) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/hanover/persistence.rb', line 22

def initialize_from_key(key)
  @key = key
  @robject = bucket.get key

  get_klass
  @content = @klass.new
  perform_merges
end

#inspectObject



36
37
38
# File 'lib/hanover/persistence.rb', line 36

def inspect
  "#<Hanover::Persistence::0x#{object_id.to_s(16)} @key=#{@robject.key} #{@content.inspect}>"
end

#reloadObject



31
32
33
34
# File 'lib/hanover/persistence.rb', line 31

def reload
  @robject.reload
  perform_merges
end

#respond_to?(name) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/hanover/persistence.rb', line 12

def respond_to?(name)
  methods.include?(name) || @content.respond_to?(name)
end