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

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



26
27
28
29
30
31
32
# File 'app/models/marty/script.rb', line 26

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

.delete_scriptsObject



115
116
117
118
119
120
121
# File 'app/models/marty/script.rb', line 115

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

find script by name/tag (not cached)



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

def self.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_filenames(paths = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/marty/script.rb', line 86

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/marty/script.rb', line 100

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",
      # FIXME: HACKY, wouldn't it be better to use
      # Gem::Specification.find_by_name("marty").gem_dir??
      File.expand_path('../../../../delorean', __FILE__),
    ]
  end
end

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/marty/script.rb', line 34

def self.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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/marty/script.rb', line 50

def self.load_script_bodies(bodies, dt=nil)
  bodies.each {
    |sname, body|
    load_a_script(sname, body, dt)
  }

  # 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.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



71
72
73
74
75
76
77
# File 'app/models/marty/script.rb', line 71

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



79
80
81
82
83
84
# File 'app/models/marty/script.rb', line 79

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_tagObject



21
22
23
24
# File 'app/models/marty/script.rb', line 21

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