Module: Cog::Embeds
Overview
Methods for querying and manipulating project files
Instance Method Summary collapse
-
#copy_keeps(original, scratch) ⇒ nil
Copy keep bodies from the original file to the scratch file.
- #find(hook) {|context| ... } ⇒ Object
-
#gather_from_file(filename, opt = {}) ⇒ Hash
Mapping from hooks to
{ 'filename' => count }
hashes. -
#gather_from_project ⇒ Object
Search through all project files for cog embeds, and remember them so that generators can refer to them later.
-
#gather_keeps(original, scratch) ⇒ Hash
Mapping from hooks to EmbedContext objects.
-
#update(c, opt = {}) {|context| ... } ⇒ Boolean?
true
if the statement was expanded or updated,false
if the statement was found, but not changed,nil
if it could not be found.
Instance Method Details
#copy_keeps(original, scratch) ⇒ nil
Copy keep bodies from the original file to the scratch file
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/cog/embeds.rb', line 39 def copy_keeps(original, scratch) Cog.activate_language(:filename => original) do original = scratch unless File.exists? original keeps = gather_keeps original, scratch keeps.each_pair do |hook, c| result = update c, :type => 'keep' do |c| c.keep_body end raise Errors::UnrecognizedKeepHook.new :hook => hook, :filename => original if result.nil? end end end |
#find(hook) {|context| ... } ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/cog/embeds.rb', line 71 def find(hook) x = @embeds[hook] unless x.nil? x.keys.sort.each do |filename| c = EmbedContext.new hook, filename, x[filename] Cog.activate_language :ext => c.extension do c.count.times do |index| c.index = index yield c end end end end end |
#gather_from_file(filename, opt = {}) ⇒ Hash
Returns mapping from hooks to { 'filename' => count }
hashes.
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/cog/embeds.rb', line 56 def gather_from_file(filename, opt={}) bucket = opt[:hash] || {} type = opt[:type] || 'cog' lang = Cog.language_for filename File.read(filename).scan(statement type, '[-A-Za-z0-9_.]+', :lang => lang) do |m| hook = m[0] bucket[hook] ||= {} bucket[hook][filename] ||= 0 bucket[hook][filename] += 1 end bucket end |
#gather_from_project ⇒ Object
Search through all project files for cog embeds, and remember them so that generators can refer to them later
8 9 10 11 12 13 |
# File 'lib/cog/embeds.rb', line 8 def gather_from_project @embeds ||= {} Cog.supported_project_files.each do |filename| gather_from_file filename, :hash => @embeds, :type => 'cog' end end |
#gather_keeps(original, scratch) ⇒ Hash
Returns mapping from hooks to Cog::EmbedContext objects.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/cog/embeds.rb', line 18 def gather_keeps(original, scratch) keeps = {} gather_from_file(original, :type => 'keep').each_pair do |hook, count| c = keeps[hook] = EmbedContext.new(hook, scratch, count[original]) raise Errors::DuplicateKeep.new :hook => hook if c.count > 1 Helpers::FileScanner.scan(original, statement('keep', hook)) do |s| c.keep_body = if s.match[4] == '{' s.capture_until end_statement('keep') s.captured_text else '' end end end keeps end |
#update(c, opt = {}) {|context| ... } ⇒ Boolean?
Returns true
if the statement was expanded or updated, false
if the statement was found, but not changed, nil
if it could not be found.
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/cog/embeds.rb', line 91 def update(c, opt={}, &block) type = opt[:type] || 'cog' Helpers::FileScanner.scan(c.path, statement(type, c.hook), :occurrence => c.actual_index) do |s| c.lineno = s.marked_line_number c.args = s.match[2].split if s.match[2] c.once = !s.match[3].nil? if s.match[4] == '{' update_body c, s, opt, &block else c, s, opt, &block end end end |