class Wings::Model::FileModel
Public Class Methods
all()
click to toggle source
# File lib/wings/file_model.rb, line 30 def self.all Dir['db/quotes/*.json'].map { |f| FileModel.new(f) } end
create(**attrs)
click to toggle source
# 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
find(id)
click to toggle source
# File lib/wings/file_model.rb, line 22 def self.find(id) begin FileModel.new("db/quotes/#{id}.json") # FIX rescue nil end end
new(filename)
click to toggle source
# 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
Private Class Methods
formatted_data(data)
click to toggle source
# File lib/wings/file_model.rb, line 54 def self.formatted_data data <<TEMPLATE { "quote": "#{data[:quote]}", "submitter": "#{data[:submitter]}", "attribution": "#{data[:attribution]}" } TEMPLATE end
Public Instance Methods
[](name)
click to toggle source
# File lib/wings/file_model.rb, line 14 def [](name) @data[name.to_s] end
[]=(name, value)
click to toggle source
# File lib/wings/file_model.rb, line 18 def []=(name, value) @data[name.to_s] = value end