Class: FolderModel
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of FolderModel.
42
43
44
45
46
47
|
# File 'lib/common/folder_model.rb', line 42
def initialize(key)
@key = key
@storage = Pathname.new("db/#{self.class.to_s.tableize}/#{key}.json")
@data = JSON.parse @storage.read
@data = @data.h
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/common/folder_model.rb', line 25
def method_missing(name, *args)
func = name.to_s.split('=')[0].to_sym
if name.to_s.index('=')
@data[func] = args[0]
else
@data[func]
end
end
|
Class Method Details
10
11
12
|
# File 'lib/common/folder_model.rb', line 10
def all
Dir["db/#{self.to_s.tableize}/*.json"].map{|file| new file.split('/').last.sub('.json') }
end
|
.find(key) ⇒ Object
6
7
8
|
# File 'lib/common/folder_model.rb', line 6
def find(key)
new(key)
end
|
Instance Method Details
17
18
19
|
# File 'lib/common/folder_model.rb', line 17
def [](key)
@data[key]
end
|
#[]=(key, value) ⇒ Object
21
22
23
|
# File 'lib/common/folder_model.rb', line 21
def []=(key, value)
@data[key] = value
end
|
#save ⇒ Object
Also known as:
save!
36
37
38
39
|
# File 'lib/common/folder_model.rb', line 36
def save
@storage.write JSON.pretty_generate(@data)
@data
end
|