Class: LazyRecord
- Inherits:
-
Object
- Object
- LazyRecord
- Defined in:
- lib/lazyrecord.rb
Constant Summary collapse
- STORE_NAME =
"lazyrecord"
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
Class Method Summary collapse
- .all ⇒ Object
- .create(*args) ⇒ Object
- .delete(id) ⇒ Object
- .entity_name ⇒ Object
- .find(id = nil, &block) ⇒ Object
- .inherited(child_class) ⇒ Object
- .next_id ⇒ Object
- .save(record) ⇒ Object
- .store ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
87 88 89 |
# File 'lib/lazyrecord.rb', line 87 def id @id end |
Class Method Details
.all ⇒ Object
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_name ⇒ Object
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_id ⇒ Object
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 |
.store ⇒ Object
116 117 118 |
# File 'lib/lazyrecord.rb', line 116 def store @@store ||= Store.new(STORE_NAME) end |
Instance Method Details
#save ⇒ Object
135 136 137 138 |
# File 'lib/lazyrecord.rb', line 135 def save @id ||= self.class.next_id self.class.save(self) end |