Class: Rulers::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.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/rulers/file_model.rb', line 5 def initialize(filename) @filename = filename # If filename is "dir/37.json", @id is 37 basename = File.split(filename)[-1] @id = File.basename(basename, ".json").to_i obj = File.read(filename) @hash = MultiJson.decode(obj) end |
Class Method Details
.all ⇒ Object
32 33 34 35 |
# File 'lib/rulers/file_model.rb', line 32 def self.all files = Dir["db/quotes/*.json"] files.map { |f| FileModel.new f } end |
.create(attrs) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rulers/file_model.rb', line 37 def self.create(attrs) hash = {} hash["submitter"] = attrs["submitter"] || "" hash["quote"] = attrs["quote"] || "" hash["attribution"] = attrs["attribution"] || "" files = Dir["db/quotes/*.json"] names = files.map { |f| f.split("/")[-1] } highest = names.map { |b| b[0...-5].to_i }.max id = highest + 1 File.open("db/quotes/#{id}.json", "w") do |f| f.write <<TEMPLATE { "submitter": "#{hash["submitter"]}", "quote": "#{hash["quote"]}", "attribution": "#{hash["attribution"]}" } TEMPLATE end FileModel.new "db/quotes/#{id}.json" end |