Class: FileModel
- Inherits:
-
Object
show all
- Defined in:
- app/models/file_model.rb
Overview
Base class for simple models that load their content from text files formatted like Toto’s articles
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(file, key, meta, body) ⇒ FileModel
Returns a new instance of FileModel.
5
6
7
8
9
10
|
# File 'app/models/file_model.rb', line 5
def initialize(file, key, meta, body)
@file = file
@key = key
@meta = meta
@body = body
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
12
13
14
15
16
17
18
|
# File 'app/models/file_model.rb', line 12
def method_missing(method, *args)
if meta.has_key?(method.to_s)
meta[method.to_s]
else
super
end
end
|
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
3
4
5
|
# File 'app/models/file_model.rb', line 3
def body
@body
end
|
#file ⇒ Object
Returns the value of attribute file.
3
4
5
|
# File 'app/models/file_model.rb', line 3
def file
@file
end
|
#key ⇒ Object
Returns the value of attribute key.
3
4
5
|
# File 'app/models/file_model.rb', line 3
def key
@key
end
|
Returns the value of attribute meta.
3
4
5
|
# File 'app/models/file_model.rb', line 3
def meta
@meta
end
|
Class Method Details
.all(options = {}) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'app/models/file_model.rb', line 21
def self.all(options = {})
options = {
:offset => 0
}.merge(options)
files = get_filtered_files(options)
if options[:limit]
files = files[options[:offset] .. (options[:offset].to_i + options[:limit].to_i-1)]
end
records = files.to_a.map do |f|
from_file(f)
end
records
end
|
.content_path ⇒ Object
80
81
82
|
# File 'app/models/file_model.rb', line 80
def self.content_path
File.expand_path(File.join(Rails.root, "content", relative_path))
end
|
.count(options = {}) ⇒ Object
43
44
45
|
# File 'app/models/file_model.rb', line 43
def self.count(options = {})
get_filtered_files(options).length
end
|
.files ⇒ Object
51
52
53
|
# File 'app/models/file_model.rb', line 51
def self.files
Dir["#{content_path}/*.txt"].sort_by {|f| File.basename(f) }.reverse
end
|
.find(key) ⇒ Object
39
40
41
|
# File 'app/models/file_model.rb', line 39
def self.find(key)
from_file("#{content_path}/#{key}.txt")
end
|
.from_file(file) ⇒ Object
73
74
75
76
77
78
|
# File 'app/models/file_model.rb', line 73
def self.from_file(file)
meta, body = File.read(file).split(/\n\n/, 2)
meta = YAML.load(meta)
key = File.basename(file).gsub('.txt','')
new(file, key, meta, body)
end
|
.random(options = {}) ⇒ Object
47
48
49
|
# File 'app/models/file_model.rb', line 47
def self.random(options = {})
from_file(files[rand(all.size)])
end
|
.relative_path ⇒ Object
84
85
86
|
# File 'app/models/file_model.rb', line 84
def self.relative_path
self.name.underscore
end
|
Instance Method Details
#index ⇒ Object
55
56
57
|
# File 'app/models/file_model.rb', line 55
def index
@index ||= self.class.files.index(file)
end
|
#next ⇒ Object
59
60
61
62
63
64
|
# File 'app/models/file_model.rb', line 59
def next
return @next if @next.present?
if next_file = self.class.files[index + 1]
@next = self.class.from_file(next_file)
end
end
|
#previous ⇒ Object
66
67
68
69
70
71
|
# File 'app/models/file_model.rb', line 66
def previous
return @previous if @previous.present?
if index > 0 and previous_file = self.class.files[index - 1]
@previous = self.class.from_file(previous_file)
end
end
|