Class: Luca::Collection::FileBackend
- Inherits:
-
Object
- Object
- Luca::Collection::FileBackend
- Defined in:
- lib/luca/collection/file_backend.rb
Instance Attribute Summary collapse
-
#data_dir ⇒ Object
Returns the value of attribute data_dir.
-
#file_storage ⇒ Object
Returns the value of attribute file_storage.
-
#namespace ⇒ Object
Returns the value of attribute namespace.
-
#record_storage ⇒ Object
Returns the value of attribute record_storage.
-
#required_attributes ⇒ Object
Returns the value of attribute required_attributes.
Instance Method Summary collapse
- #allocate_id ⇒ Object
- #create(attributes = {}) ⇒ Object
- #destroy(id) ⇒ Object
- #file_storage_location ⇒ Object
- #flush_storage_to_disk ⇒ Object
- #index ⇒ Object
-
#initialize(options = {}) ⇒ FileBackend
constructor
A new instance of FileBackend.
- #read_storage_from_disk ⇒ Object
- #records ⇒ Object
- #show(id) ⇒ Object
- #sync(method, hash = {}, options = {}) ⇒ Object (also: #backbone_sync)
- #update(attributes = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ FileBackend
Returns a new instance of FileBackend.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/luca/collection/file_backend.rb', line 11 def initialize ={} @options = .dup @namespace = [:namespace] @required_attributes = [:required_attributes] @data_dir = [:data_dir] || File.join(::Rails.root, "db", "collections") unless File.exists?( file_storage_location ) FileUtils.mkdir_p(data_dir) flush_storage_to_disk end read_storage_from_disk end |
Instance Attribute Details
#data_dir ⇒ Object
Returns the value of attribute data_dir.
5 6 7 |
# File 'lib/luca/collection/file_backend.rb', line 5 def data_dir @data_dir end |
#file_storage ⇒ Object
Returns the value of attribute file_storage.
5 6 7 |
# File 'lib/luca/collection/file_backend.rb', line 5 def file_storage @file_storage end |
#namespace ⇒ Object
Returns the value of attribute namespace.
5 6 7 |
# File 'lib/luca/collection/file_backend.rb', line 5 def namespace @namespace end |
#record_storage ⇒ Object
Returns the value of attribute record_storage.
5 6 7 |
# File 'lib/luca/collection/file_backend.rb', line 5 def record_storage @record_storage end |
#required_attributes ⇒ Object
Returns the value of attribute required_attributes.
5 6 7 |
# File 'lib/luca/collection/file_backend.rb', line 5 def required_attributes @required_attributes end |
Instance Method Details
#allocate_id ⇒ Object
91 92 93 |
# File 'lib/luca/collection/file_backend.rb', line 91 def allocate_id record_storage[:id_counter] += 1 end |
#create(attributes = {}) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/luca/collection/file_backend.rb', line 95 def create attributes={} attributes.symbolize_keys! attributes[:id] ||= allocate_id records << attributes flush_storage_to_disk {success:true,id:attributes[:id],record:attributes} end |
#destroy(id) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/luca/collection/file_backend.rb', line 111 def destroy id records.reject! do |record| record[:id] == id end flush_storage_to_disk {success:true} end |
#file_storage_location ⇒ Object
38 39 40 |
# File 'lib/luca/collection/file_backend.rb', line 38 def file_storage_location File.join(data_dir, "#{ namespace }.json") end |
#flush_storage_to_disk ⇒ Object
26 27 28 29 30 31 |
# File 'lib/luca/collection/file_backend.rb', line 26 def flush_storage_to_disk File.open(file_storage_location, 'w+') do |fh| payload = JSON.generate(record_storage) fh.puts(payload) end end |
#index ⇒ Object
81 82 83 |
# File 'lib/luca/collection/file_backend.rb', line 81 def index records end |
#read_storage_from_disk ⇒ Object
33 34 35 36 |
# File 'lib/luca/collection/file_backend.rb', line 33 def read_storage_from_disk data = IO.read( file_storage_location ) @record_storage = JSON.parse(data) end |
#records ⇒ Object
77 78 79 |
# File 'lib/luca/collection/file_backend.rb', line 77 def records Array(record_storage[:records]) end |
#show(id) ⇒ Object
85 86 87 88 89 |
# File 'lib/luca/collection/file_backend.rb', line 85 def show id record = records.detect {|r| r[:id].to_s == id.to_s } record.symbolize_keys! record end |
#sync(method, hash = {}, options = {}) ⇒ Object Also known as: backbone_sync
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/luca/collection/file_backend.rb', line 42 def sync method, hash={}, ={} if method == "read" and hash[:id].nil? return index() end if method == "read" and !hash[:id].nil? return show( hash[:id] ) end if method == "create" return create( hash ) end if method == "update" return update( hash ) end if method == "delete" return destroy( hash ) end end |
#update(attributes = {}) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/luca/collection/file_backend.rb', line 103 def update attributes={} attributes.symbolize_keys! record = show(attributes[:id]) record.merge! attributes flush_storage_to_disk {success:true,record:record} end |