Class: Ozy

Inherits:
Hash
  • Object
show all
Defined in:
lib/ozy.rb

Defined Under Namespace

Classes: KeyError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, options = {}) ⇒ Ozy

Returns a new instance of Ozy.



73
74
75
76
77
78
79
# File 'lib/ozy.rb', line 73

def initialize(attributes = {}, options = {})
  @options = options
  if @options[:key]
    @persisted = true
  end
  merge! attributes unless attributes.empty?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ozy.rb', line 115

def method_missing(method, *args)
  if @options[:key] && self.class.connection.respond_to?(method)
    self.class.connection.send(method, *[@options[:key], args].flatten)
  else
    method = method.to_s
    if method.chomp!('=')
      self[method] = args.first
    elsif args.empty?
      self[method.to_s] || self[method.to_sym]
    else
      super
    end
  end
end

Class Method Details

.connect(*args) ⇒ Object



8
9
10
# File 'lib/ozy.rb', line 8

def connect(*args)
  self.connection = Redis.new(*args)
end

.connectionObject



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

def connection
  @connection ||= ::Redis.new
end

.connection=(new_connection) ⇒ Object



16
17
18
# File 'lib/ozy.rb', line 16

def connection=(new_connection)
  @connection = new_connection
end

.create(key, attributes = {}, options = {}) ⇒ Object



20
21
22
23
24
# File 'lib/ozy.rb', line 20

def create(key, attributes = {}, options = {})
  instance = new(attributes, options.merge(:key => key))
  instance.save
  instance
end

.destroy(key) ⇒ Object



26
27
28
# File 'lib/ozy.rb', line 26

def destroy(key)
  connection.del(key)
end

.get(key) ⇒ Object Also known as: find



30
31
32
33
34
# File 'lib/ozy.rb', line 30

def get(key)
  if response = connection.get(key)
    new(Marshal.load(response), :key => key)
  end
end

Instance Method Details

#[]=(key, value) ⇒ Object



38
39
40
41
# File 'lib/ozy.rb', line 38

def []=(key, value)
  super
  update_parent
end

#delete(value) ⇒ Object



47
48
49
50
# File 'lib/ozy.rb', line 47

def delete(value)
  super
  update_parent
end

#destroyObject



43
44
45
# File 'lib/ozy.rb', line 43

def destroy
  self.class.destroy(@options[:key])
end

#expire_in!(time) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/ozy.rb', line 52

def expire_in!(time)
  if persisted?
    save(@options[:key], :expire => time)
  else
    @options[:expire] = time
  end
end

#expires_inObject



60
61
62
# File 'lib/ozy.rb', line 60

def expires_in
  persisted? ? ttl : @options[:expire]
end

#idObject Also known as: key



64
65
66
67
68
69
70
# File 'lib/ozy.rb', line 64

def id
  if persisted?
    @options[:key]
  else
    nil
  end
end

#inspectObject



81
82
83
84
85
86
87
# File 'lib/ozy.rb', line 81

def inspect
  if persisted?
    "#{@options[:key]}: #{super}"
  else
    super
  end
end

#merge!(*args) ⇒ Object



89
90
91
92
# File 'lib/ozy.rb', line 89

def merge!(*args)
  super
  update_parent
end

#persisted?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/ozy.rb', line 94

def persisted?
  !!@persisted
end

#save(key = nil, options = {}) ⇒ Object

Raises:



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ozy.rb', line 98

def save(key = nil, options = {})
  raise KeyError.new("You must supply a key to save a Redis-backed hash!") unless key ||= @options[:key]
  options = @options.merge(options)
  if options[:expire]
    self.class.connection.setex(key, options[:expire], Marshal.dump(to_hash))
  else
    self.class.connection.set(key, Marshal.dump(to_hash))
  end
  @options[:key] = key
  @persisted = true
end

#to_hashObject



110
111
112
# File 'lib/ozy.rb', line 110

def to_hash
  Hash[self]
end