Class: Marty::ScriptSet

Inherits:
Delorean::AbstractContainer
  • Object
show all
Defined in:
app/helpers/marty/script_set.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag = nil) ⇒ ScriptSet

Returns a new instance of ScriptSet.



13
14
15
16
# File 'app/helpers/marty/script_set.rb', line 13

def initialize(tag = nil)
  @tag = Marty::Tag.map_to_tag(tag)
  super()
end

Instance Attribute Details

#tagObject (readonly)

ScriptSet acts as a process-wide cache for Delorean engines. FIXME: rewrite as Singleton.



5
6
7
# File 'app/helpers/marty/script_set.rb', line 5

def tag
  @tag
end

Class Method Details

.clear_cacheObject



7
8
9
# File 'app/helpers/marty/script_set.rb', line 7

def self.clear_cache
  @@engines, @@dengines, @@dengines_dt = {}, {}, nil
end

Instance Method Details

#get_engine(sname) ⇒ Object



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
# File 'app/helpers/marty/script_set.rb', line 24

def get_engine(sname)
  raise "bad sname #{sname}" unless sname.is_a?(String)

  if tag.isdev?
    # FIXME: there are race conditions here if a script changes in
    # the middle of a DEV import sequence. But, DEV imports are
    # hacky/rare anyway.  So, don't bother for now.

    max_dt = Marty::Script.
      order('created_dt DESC').limit(1).pluck(:created_dt).first

    @@dengines_dt ||= max_dt

    # reset dengine cache if a script has changed
    @@dengines = {} if max_dt > @@dengines_dt

    engine = @@dengines[sname]

    return engine if engine

    script = Marty::Script.find_script(sname, tag) || raise('No such script')

    @@dengines[sname] = parse_check(sname, script.body)
  else
    engine = @@engines[[tag.id, sname]]

    return engine if engine

    script = Marty::Script.find_script(sname, tag) || raise('No such script')

    @@engines[[tag.id, sname]] = parse_check(sname, script.body)
  end
end

#parse_check(sname, body) ⇒ Object



18
19
20
21
22
# File 'app/helpers/marty/script_set.rb', line 18

def parse_check(sname, body)
  engine = Delorean::Engine.new(sname, self)
  engine.parse(body)
  engine
end