Class: Wings::Model::FileModel
Class Method Summary collapse
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
-
#initialize(filename) ⇒ FileModel
constructor
A new instance of FileModel.
Constructor Details
#initialize(filename) ⇒ FileModel
Returns a new instance of FileModel.
6 7 8 9 10 11 12 |
# File 'lib/wings/file_model.rb', line 6 def initialize(filename) @filename = filename # if filename is 'dir/3.json', @id would be 3 @id = File.basename(filename, '.json').to_i @data = MultiJson.load(File.read(filename)) end |
Class Method Details
.all ⇒ Object
30 31 32 |
# File 'lib/wings/file_model.rb', line 30 def self.all Dir['db/quotes/*.json'].map { |f| FileModel.new(f) } end |
.create(**attrs) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/wings/file_model.rb', line 34 def self.create(**attrs) data = { quote: attrs[:quote], submitter: attrs[:submitter], attribution: attrs[:attribution], } files = Dir['db/quotes/*.json'] existing_ids = files.map { |f| File.basename(f, '.json').to_i } new_id = existing_ids.max + 1 File.open("db/quotes/#{new_id}.json", 'w') do |f| f.write formatted_data(data) end FileModel.new "db/quotes/#{new_id}.json" end |