Class: LazyRecord

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

Constant Summary collapse

STORE_NAME =
"lazyrecord"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject

Returns the value of attribute id.



87
88
89
# File 'lib/lazyrecord.rb', line 87

def id
  @id
end

Class Method Details

.allObject



102
103
104
# File 'lib/lazyrecord.rb', line 102

def all
  store.all(entity_name).values
end

.create(*args) ⇒ Object



128
129
130
131
132
# File 'lib/lazyrecord.rb', line 128

def create(*args)
  record = new(*args)
  record.save
  record
end

.delete(id) ⇒ Object



124
125
126
# File 'lib/lazyrecord.rb', line 124

def delete(id)
  store.delete(entity_name, id)
end

.entity_nameObject



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

def entity_name
  name.downcase
end

.find(id = nil, &block) ⇒ Object



106
107
108
109
110
111
112
113
114
# File 'lib/lazyrecord.rb', line 106

def find(id = nil, &block)
  if block_given?
    all.find(&block)
  elsif id
    store.find(entity_name, id)
  else
    raise ArgumentError.new("find requires and id or a block to be provided")
  end
end

.inherited(child_class) ⇒ Object



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

def inherited(child_class)
  store.create_entity(child_class.name.downcase)
end

.next_idObject



98
99
100
# File 'lib/lazyrecord.rb', line 98

def next_id
  store.next_id(entity_name)
end

.save(record) ⇒ Object



120
121
122
# File 'lib/lazyrecord.rb', line 120

def save(record)
  store.save(entity_name, record)
end

.storeObject



116
117
118
# File 'lib/lazyrecord.rb', line 116

def store
  @@store ||= Store.new(STORE_NAME)
end

Instance Method Details

#saveObject



135
136
137
138
# File 'lib/lazyrecord.rb', line 135

def save
  @id ||= self.class.next_id
  self.class.save(self)
end