Class: Marty::Script
- Inherits:
-
Base
show all
- Defined in:
- app/models/marty/script.rb
Constant Summary
Constants inherited
from Base
Base::COUNT_SIG, Base::DISTINCT_SIG, Base::FIRST_SIG, Base::GROUP_SIG, Base::JOINS_SIG, Base::LAST_SIG, Base::LIMIT_SIG, Base::MCFLY_PT_SIG, Base::NOT_SIG, Base::ORDER_SIG, Base::PLUCK_SIG, Base::SELECT_SIG, Base::WHERE_SIG
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
mcfly_pt
joins, old_joins
Class Method Details
.create_script(name, body) ⇒ Object
27
28
29
30
31
32
33
|
# File 'app/models/marty/script.rb', line 27
def self.create_script(name, body)
script = new
script.name = name
script.body = body
script.save
script
end
|
.delete_scripts ⇒ Object
116
117
118
119
120
121
122
|
# File 'app/models/marty/script.rb', line 116
def self.delete_scripts
ActiveRecord::Base.connection.
execute("ALTER TABLE marty_scripts DISABLE TRIGGER USER;")
Marty::Script.delete_all
ActiveRecord::Base.connection.
execute("ALTER TABLE marty_scripts ENABLE TRIGGER USER;")
end
|
.find_script(sname, tag = nil) ⇒ Object
17
18
19
20
|
# File 'app/models/marty/script.rb', line 17
def self.find_script(sname, tag=nil)
tag = Marty::Tag.map_to_tag(tag)
Marty::Script.lookup(tag.created_dt, sname)
end
|
.get_script_filenames(paths = nil) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'app/models/marty/script.rb', line 87
def self.get_script_filenames(paths = nil)
paths = get_script_paths(paths)
filenames = {}
paths.each do |path|
Dir.glob("#{path}/*.dl").each do |filename|
basename = File.basename(filename)
filenames[basename] = filename unless filenames.has_key?(basename)
end
end
filenames.values
end
|
.get_script_paths(paths) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'app/models/marty/script.rb', line 101
def self.get_script_paths(paths)
if paths
paths = Array(paths)
elsif Rails.configuration.marty.delorean_scripts_path
paths = Rails.configuration.marty.delorean_scripts_path
else
paths = [
"#{Rails.root}/delorean",
File.expand_path('../../../../delorean', __FILE__),
]
end
end
|
.load_a_script(sname, body, dt = nil) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'app/models/marty/script.rb', line 35
def self.load_a_script(sname, body, dt=nil)
s = Marty::Script.lookup('infinity', sname)
if !s
s = Marty::Script.new
s.body = body
s.name = sname
s.created_dt = dt if dt
s.save!
elsif s.body != body
s.body = body
s.created_dt = dt if dt
s.save!
end
end
|
.load_script_bodies(bodies, dt = nil) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'app/models/marty/script.rb', line 51
def self.load_script_bodies(bodies, dt=nil)
bodies.each {
|sname, body|
load_a_script(sname, body, dt)
}
tag = Marty::Tag.get_latest1
latest = Marty::Script.order("created_dt DESC").first
tag_time = (dt || [latest.try(:created_dt), Time.now].compact.max) +
1.second
tag = Marty::Tag.do_create(tag_time, "tagged from load scripts") if
!(tag && latest) || tag.created_dt <= latest.created_dt
tag
end
|
.load_scripts(path = nil, dt = nil) ⇒ Object
72
73
74
75
76
77
78
|
# File 'app/models/marty/script.rb', line 72
def self.load_scripts(path=nil, dt=nil)
files = get_script_filenames(path)
bodies = read_script_files(files)
load_script_bodies(bodies, dt)
end
|
.read_script_files(files) ⇒ Object
80
81
82
83
84
85
|
# File 'app/models/marty/script.rb', line 80
def self.read_script_files(files)
files.collect { |fpath|
fname = File.basename(fpath)[0..-4].camelize
[fname, File.read(fpath)]
}
end
|
Instance Method Details
#find_tag ⇒ Object
22
23
24
25
|
# File 'app/models/marty/script.rb', line 22
def find_tag
Marty::Tag.where("created_dt >= ?", created_dt).order(:created_dt).first
end
|