Class: Marty::Script

Inherits:
Base show all
Defined in:
app/models/marty/script.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

get_final_attrs, get_struct_attrs, make_hash, make_openstruct, mcfly_pt

Methods inherited from ActiveRecord::Base

joins, old_joins

Class Method Details

.create_script(name, body) ⇒ Object



66
67
68
69
70
71
72
# File 'app/models/marty/script.rb', line 66

def create_script(name, body)
  script      = new
  script.name = name
  script.body = body
  script.save
  script
end

.delete_scriptsObject



159
160
161
162
163
164
165
# File 'app/models/marty/script.rb', line 159

def 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



61
62
63
64
# File 'app/models/marty/script.rb', line 61

def find_script(sname, tag = nil)
  tag = Marty::Tag.map_to_tag(tag)
  Marty::Script.mcfly_pt(tag.created_dt).find_by(name: sname)
end

.get_script_file_paths(paths = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/marty/script.rb', line 129

def get_script_file_paths(paths = nil)
  paths = get_script_paths(paths)

  paths.each_with_object({}) do |path, filenames|
    Dir.glob("#{path}/**/*.dl").each do |filename|
      base_pathname = Pathname.new(path)
      pathname = Pathname.new(filename).relative_path_from(base_pathname)
      relative_file_name = pathname.sub_ext('').to_s

      next if filenames.key?(relative_file_name)

      filenames[relative_file_name] = filename
    end
  end
end

.get_script_filenames(paths = nil) ⇒ Object



125
126
127
# File 'app/models/marty/script.rb', line 125

def get_script_filenames(paths = nil)
  get_script_file_paths(paths).values
end

.get_script_paths(paths) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/models/marty/script.rb', line 145

def get_script_paths(paths)
  paths_from_config = Rails.configuration.marty.delorean_scripts_path

  return Array(paths) if paths
  return paths_from_config if paths_from_config.present?

  [
    Rails.root.join('delorean'),
    # FIXME: HACKY, wouldn't it be better to use
    # Gem::Specification.find_by_name("marty").gem_dir??
    File.expand_path('../../../../delorean', __FILE__),
  ]
end

.load_a_script(sname, body, dt = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/marty/script.rb', line 74

def load_a_script(sname, body, dt = nil)
  s = Marty::Script.find_by(obsoleted_dt: 'infinity', name: 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



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/models/marty/script.rb', line 90

def load_script_bodies(bodies, dt = nil)
  bodies.each do |sname, body|
    load_a_script(sname, body, dt)
  end

  # Create a new tag if scripts were modified after the last tag
  tag = Marty::Tag.get_latest1
  latest = Marty::Script.order('created_dt DESC').first

  tag_time = (dt || [latest.try(:created_dt), Time.zone.now].compact.max) +
    1.second

  # If no tag_time is provided, the tag created_dt will be the same
  # as the scripts.
  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



110
111
112
113
114
115
116
# File 'app/models/marty/script.rb', line 110

def load_scripts(path = nil, dt = nil)
  files = get_script_file_paths(path)

  bodies = read_script_files(files)

  load_script_bodies(bodies, dt)
end

.read_script_files(files) ⇒ Object



118
119
120
121
122
123
# File 'app/models/marty/script.rb', line 118

def read_script_files(files)
  files.map do |fname, fpath|
    script_name = fname.camelize
    [script_name, File.read(fpath)]
  end
end

Instance Method Details

#find_tagObject

find script by name/tag (not cached)



16
17
18
19
# File 'app/models/marty/script.rb', line 16

def find_tag
  # find the first tag created after this script.
  Marty::Tag.where('created_dt >= ?', created_dt).order(:created_dt).first
end