Class: Guard::Jsstaticrequire
- Defined in:
- lib/guard/js-static-require.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ :build_on_start => false, :libs => [], :start_delim => /<!-- START JS_STATIC_REQUIRE -->/, :end_delim => /<!-- END JS_STATIC_REQUIRE -->/ }
Instance Attribute Summary collapse
-
#files ⇒ Object
Returns the value of attribute files.
Instance Method Summary collapse
- #build_load_string(source) ⇒ Object
- #contains_new_path?(paths) ⇒ Boolean
-
#initialize(watchers = [], options = {}) ⇒ Jsstaticrequire
constructor
Initialize a Guard.
- #inject(value, source) ⇒ Object
- #inject_script_load ⇒ Object
- #relative_path(target, path) ⇒ Object
-
#reload ⇒ Object
Called when ‘reload|r|z + enter` is pressed.
- #reset_files ⇒ Object
-
#run_all ⇒ Object
Called when just ‘enter` is pressed This method should be principally used for long action like running all specs/tests/…
-
#run_on_change(paths) ⇒ Object
Called on file(s) modifications that the Guard watches.
-
#run_on_deletion(paths) ⇒ Object
Called on file(s) deletions that the Guard watches.
- #scan_libs(libs) ⇒ Object
- #scan_path(path) ⇒ Object
-
#start ⇒ Object
Call once when Guard starts.
-
#stop ⇒ Object
Called when ‘stop|quit|exit|s|q|e + enter` is pressed (when Guard quits).
- #tabulation(source) ⇒ Object
Constructor Details
#initialize(watchers = [], options = {}) ⇒ Jsstaticrequire
Initialize a Guard.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/guard/js-static-require.rb', line 18 def initialize(watchers = [], = {}) defaults = DEFAULT_OPTIONS.clone @files = [] [:libs].each do |lib| watchers << ::Guard::Watcher.new(%r{^#{ lib }/(.+\.js)$}) end super(watchers, defaults.merge()) end |
Instance Attribute Details
#files ⇒ Object
Returns the value of attribute files.
13 14 15 |
# File 'lib/guard/js-static-require.rb', line 13 def files @files end |
Instance Method Details
#build_load_string(source) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/guard/js-static-require.rb', line 126 def build_load_string(source) tab = tabulation(source) scripts = "\n" @files.each do |file| scripts += %Q{#{tab}<script type="text/javascript" src="#{relative_path([:updates], file)}"></script>\n} end scripts += tab scripts end |
#contains_new_path?(paths) ⇒ Boolean
101 102 103 |
# File 'lib/guard/js-static-require.rb', line 101 def contains_new_path?(paths) (paths - @files).length > 0 end |
#inject(value, source) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/guard/js-static-require.rb', line 105 def inject(value, source) if os = source.match([:start_delim]) and oe = source.match([:end_delim]) os = os.end(0) oe = oe.begin(0) source[os...oe] = value end source end |
#inject_script_load ⇒ Object
150 151 152 153 154 155 156 157 158 |
# File 'lib/guard/js-static-require.rb', line 150 def inject_script_load source = File.read([:updates]) loader = build_load_string(source) result = inject(loader, source) File.open([:updates], "wb") do |file| file << result end end |
#relative_path(target, path) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/guard/js-static-require.rb', line 138 def relative_path(target, path) target_dir = File.(File.dirname(target)).split(File::SEPARATOR) path_dir = File.(File.dirname(path)).split(File::SEPARATOR) while target_dir.length > 0 and target_dir.first == path_dir.first target_dir.shift path_dir.shift end (".." + File::Separator) * (target_dir.length) + path_dir.push(File.basename(path)).join(File::SEPARATOR) end |
#reload ⇒ Object
Called when ‘reload|r|z + enter` is pressed. This method should be mainly used for “reload” (really!) actions like reloading passenger/spork/bundler/…
44 45 |
# File 'lib/guard/js-static-require.rb', line 44 def reload end |
#reset_files ⇒ Object
74 75 76 |
# File 'lib/guard/js-static-require.rb', line 74 def reset_files @files = scan_libs([:libs]) end |
#run_all ⇒ Object
Called when just ‘enter` is pressed This method should be principally used for long action like running all specs/tests/…
50 51 52 53 54 55 56 57 |
# File 'lib/guard/js-static-require.rb', line 50 def run_all UI.info "Injecting scripts on #{[:updates]}" reset_files inject_script_load Notifier.notify("Success injected scripts on #{[:updates]}") end |
#run_on_change(paths) ⇒ Object
Called on file(s) modifications that the Guard watches.
62 63 64 65 |
# File 'lib/guard/js-static-require.rb', line 62 def run_on_change(paths) return unless contains_new_path?(paths) run_all end |
#run_on_deletion(paths) ⇒ Object
Called on file(s) deletions that the Guard watches.
70 71 72 |
# File 'lib/guard/js-static-require.rb', line 70 def run_on_deletion(paths) run_all end |
#scan_libs(libs) ⇒ Object
78 79 80 |
# File 'lib/guard/js-static-require.rb', line 78 def scan_libs(libs) libs.map { |path| scan_path(path) }.flatten.uniq end |
#scan_path(path) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/guard/js-static-require.rb', line 82 def scan_path(path) if File.directory? path Dir.glob(path + "/**/*.js").sort do |a, b| da = a.split(File::SEPARATOR) db = b.split(File::SEPARATOR) comp = da.length <=> db.length if comp == 0 a <=> b else comp end end else [path] end end |
#start ⇒ Object
Call once when Guard starts. Please override initialize method to init stuff.
32 33 34 |
# File 'lib/guard/js-static-require.rb', line 32 def start run_all if [:build_on_start] end |
#stop ⇒ Object
Called when ‘stop|quit|exit|s|q|e + enter` is pressed (when Guard quits).
38 39 |
# File 'lib/guard/js-static-require.rb', line 38 def stop end |
#tabulation(source) ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/guard/js-static-require.rb', line 115 def tabulation(source) if os = source.match([:start_delim]) line = source[0...os.begin(0)].split(/\r\n|\r|\n/).last spaces = line.match(/^([\s\t]*)/) spaces[1] else nil end end |