Class: Jobit::Storage
- Inherits:
-
Object
- Object
- Jobit::Storage
- Defined in:
- lib/jobit/storage.rb
Class Method Summary collapse
- .all ⇒ Object
- .all_files ⇒ Object
- .create(file, content) ⇒ Object
- .destroy(file) ⇒ Object
- .destroy_all ⇒ Object
- .find(id) ⇒ Object
- .find_by_name(name) ⇒ Object
- .update(file, content) ⇒ Object
- .where(search) ⇒ Object
Class Method Details
.all ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/jobit/storage.rb', line 17 def self.all result = [] for file_name in self.all_files obj = get_data_from_file(file_name) result << Jobby.new(obj) end result end |
.all_files ⇒ Object
5 6 7 8 9 |
# File 'lib/jobit/storage.rb', line 5 def self.all_files Dir.mkdir(Jobit::Job.jobs_path) unless Dir.exist?(Jobit::Job.jobs_path) arr = Dir.glob("#{Jobit::Job.jobs_path}/*").find_all { |x| File.file? x } arr.sort end |
.create(file, content) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/jobit/storage.rb', line 76 def self.create(file, content) Dir.mkdir(Jobit::Job.jobs_path) unless Dir.exist?(Jobit::Job.jobs_path) file_name = self.make_file_name(file) data = Marshal.dump(content.to_hash) File.open(file_name, 'w+b') { |f| f.write(data) } true end |
.destroy(file) ⇒ Object
70 71 72 73 74 |
# File 'lib/jobit/storage.rb', line 70 def self.destroy(file) file_name = self.make_file_name(file) return false unless File.exist?(file_name) File.delete(file_name) end |
.destroy_all ⇒ Object
11 12 13 14 15 |
# File 'lib/jobit/storage.rb', line 11 def self.destroy_all for file_name in self.all_files File.delete(file_name) end end |
.find(id) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/jobit/storage.rb', line 28 def self.find(id) file_name = self.make_file_name(id) return nil unless File.exist?(file_name) obj = get_data_from_file(file_name) Jobby.new(obj) end |
.find_by_name(name) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/jobit/storage.rb', line 37 def self.find_by_name(name) result = nil for file_name in self.all_files obj = get_data_from_file(file_name) next if obj.nil? if obj.name == name result = Jobby.new(obj) break end end result end |
.update(file, content) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/jobit/storage.rb', line 85 def self.update(file, content) file_name = self.make_file_name(file) return false unless File.exist?(file_name) File.open(file_name, 'w+b') { |f| f.write(Marshal.dump(content.to_hash)) } true end |
.where(search) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/jobit/storage.rb', line 53 def self.where(search) search_hash = {:name => search} search_hash = search if search.is_a?(Hash) result = [] for file_name in self.all_files obj = get_data_from_file(file_name) for key, val in search_hash next unless obj.respond_to?(key) result << Jobby.new(obj) if obj[key] == val end end result end |