Class: YAMLAdapter
- Inherits:
-
Object
- Object
- YAMLAdapter
- Defined in:
- lib/less_active_record/yaml_adapter.rb
Instance Attribute Summary collapse
-
#file_name ⇒ Object
readonly
Returns the value of attribute file_name.
Instance Method Summary collapse
- #destroy(id) ⇒ Object
-
#initialize(storage_name) ⇒ YAMLAdapter
constructor
A new instance of YAMLAdapter.
- #insert(attributes) ⇒ Object
- #search(attributes = {}) ⇒ Object
- #update(id, attributes) ⇒ Object
Constructor Details
#initialize(storage_name) ⇒ YAMLAdapter
Returns a new instance of YAMLAdapter.
6 7 8 9 |
# File 'lib/less_active_record/yaml_adapter.rb', line 6 def initialize(storage_name) @file_name = "#{ storage_name }.yml" @_mapper = YAMLObjectMapper.new(file_name) end |
Instance Attribute Details
#file_name ⇒ Object (readonly)
Returns the value of attribute file_name.
4 5 6 |
# File 'lib/less_active_record/yaml_adapter.rb', line 4 def file_name @file_name end |
Instance Method Details
#destroy(id) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/less_active_record/yaml_adapter.rb', line 31 def destroy(id) item = search(id: id).first unless item.nil? _items.delete(item) dump_all_items! item end end |
#insert(attributes) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/less_active_record/yaml_adapter.rb', line 24 def insert(attributes) calculate_id.tap do |id| _items << attributes.merge(id: id) dump_all_items! end end |
#search(attributes = {}) ⇒ Object
41 42 43 44 45 |
# File 'lib/less_active_record/yaml_adapter.rb', line 41 def search(attributes = {}) _items.map(&:clone).reject do |item| attributes.keys.any? { |key| attributes[key] != item[key] } end end |
#update(id, attributes) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/less_active_record/yaml_adapter.rb', line 11 def update(id, attributes) item = search(id: id).first if item.nil? false else attributes.delete(:id) index = _items.index(item) _items[index].update(attributes) true end end |