-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathmodels.rb
89 lines (71 loc) · 2.14 KB
/
models.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require 'faker'
require 'uri'
require 'hashie'
require 'yaml'
require './utils'
module Wecheat
module Models
class << self
def store_dir
File.expand_path('../db', __FILE__)
end
def purge
Dir[File.join(Models.store_dir, "**/*.yml")].each{|f| FileUtils.rm_rf(f) }
end
def setup
app = App.new.tap do |app|
(rand(3)+1).times do
app.users << User.new
end
(rand(3)+1).times do
app.medias << Media.new(type: 'image', path: '/medias/sample.jpg')
app.medias << Media.new(type: 'thumb', path: '/medias/sample.jpg')
app.medias << Media.new(type: 'video', path: '/medias/sample.mp4')
app.medias << Media.new(type: 'voice', path: '/medias/sample.mp3')
end
end.save
QRCode.new(appid: app.id, action_name: 'QR_SCENE', scene_id: 2014).save
end
end
module Concerns
module Persistable
def self.included base
base.extend ClassMethods
end
def write
FileUtils.mkdir_p(self.class.store_dir) unless Dir.exist?(self.class.store_dir)
File.open(file_path, 'w'){|f| f.puts self.to_yaml }
self
end
alias :save :write
def delete
FileUtils.rm_rf(file_path)
end
alias :remove :delete
def filename;raise NotImplementedError.new; end
private
def file_path
File.join(self.class.store_dir, "#{filename}.yml")
end
module ClassMethods
def find id
file = File.join(self.store_dir, "#{id}.yml")
self.new(YAML.load_file(file)) if File.exist?(file)
end
def count
files_collection.size
end
def all
files_collection.collect{|f| self.new(YAML.load_file(f)) }.compact
end
def store_dir; raise NotImplementedError.new; end
private
def files_collection
Dir[File.join(self.store_dir, "*.yml")]
end
end
end
end
Dir[File.expand_path('./models/*.rb')].each{|f| require f }
end
end