Class: Cubbyhole::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cubbyhole/base.rb

Constant Summary collapse

METHOD_BLACKLIST =
[:marshal_load, :marshal_dump, :_dump, :_load]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



51
52
53
54
55
56
# File 'lib/cubbyhole/base.rb', line 51

def initialize(params={})
  @id = self.class.next_id
  @params = params
  @persisted = false
  normalize_params!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cubbyhole/base.rb', line 75

def method_missing(meth, *args, &blk)
  return super if METHOD_BLACKLIST.include?(meth.to_sym)

  key = meth.to_s

  if key =~ /=$/
    raise ArgumentError unless args.size == 1
    @params[key.gsub(/=$/, "")] = args.first
  else
    return super if args.size != 0
    @params[key]
  end
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



58
59
60
# File 'lib/cubbyhole/base.rb', line 58

def id
  @id
end

Class Method Details

.all(atts = nil) ⇒ Object



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

def self.all
  sdbm.values.map{|x| Marshal.load(x) }
end

.backendObject



42
43
44
45
# File 'lib/cubbyhole/base.rb', line 42

def self.backend
  require 'cubbyhole/sdbm'
  @backend ||= SDBM.new(self.to_s)
end

.create(params = {}) ⇒ Object



7
8
9
# File 'lib/cubbyhole/base.rb', line 7

def self.create(params={})
  new(params).save
end

.first(atts = nil) ⇒ Object



30
31
32
# File 'lib/cubbyhole/base.rb', line 30

def self.first(atts = nil)
  all(atts).first
end

.get(id) ⇒ Object



11
12
13
# File 'lib/cubbyhole/base.rb', line 11

def self.get(id)
  backend[id.to_s]
end

.get!(id) ⇒ Object



15
16
17
# File 'lib/cubbyhole/base.rb', line 15

def self.get!(id)
  get(id) or raise "#{self.class} not found for id #{id}"
end

.last(atts = nil) ⇒ Object



34
35
36
# File 'lib/cubbyhole/base.rb', line 34

def self.last(atts = nil)
  all(atts).last
end

.next_idObject



19
20
21
22
23
24
# File 'lib/cubbyhole/base.rb', line 19

def self.next_id
  @next_id ||= 0
  id = @next_id
  @next_id += 1
  id
end

.nukeObject



47
48
49
# File 'lib/cubbyhole/base.rb', line 47

def self.nuke
  backend.clear
end

Instance Method Details

#attributesObject



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

def attributes
  @params.dup
end

#destroyObject



71
72
73
# File 'lib/cubbyhole/base.rb', line 71

def destroy
  self.class.backend.delete(@id.to_s)
end

#normalize_params!Object



104
105
106
107
108
109
110
111
# File 'lib/cubbyhole/base.rb', line 104

def normalize_params!
  @params.default = nil

  @params.keys.dup.each do |key|
    @params[key.to_s] = @params.delete(key)
  end
  @params
end

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  @persisted
end

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to?(meth)
  return super if METHOD_BLACKLIST.include?(meth.to_sym)
  true
end

#saveObject



64
65
66
67
68
69
# File 'lib/cubbyhole/base.rb', line 64

def save
  @persisted = true
  normalize_params!
  self.class.backend[id.to_s] = self
  self
end

#update_attributes(params) ⇒ Object



98
99
100
101
102
# File 'lib/cubbyhole/base.rb', line 98

def update_attributes(params)
  @params.merge!(params)
  normalize_params!
  save
end