Module: Cog::Embeds

Extended by:
Embeds
Included in:
Embeds
Defined in:
lib/cog/embeds.rb

Overview

Methods for querying and manipulating project files

Instance Method Summary collapse

Instance Method Details

#copy_keeps(original, scratch) ⇒ nil

Copy keep bodies from the original file to the scratch file

Parameters:

  • original (String)

    file in which to search for keep statements. If the original does not exist, then scratch will serve as the original (we do this so that the keeps will get expanded in any case)

  • scratch (String)

    file to which keep bodies will be copied

Returns:

  • (nil)


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

Parameters:

  • hook (String)

    embed hook for which to find directive occurrences

Yield Parameters:

  • context (EmbedContext)

    describes the context in which the embed statement was found



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.

Parameters:

  • filename (String)

    file from which to gather statements

  • opt (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opt):

  • :hash (Hash) — default: {}

    object in which to gather the mapping

  • :type (String) — default: 'cog'

    one of 'cog' or 'keep'

Returns:

  • (Hash)

    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_projectObject

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.

Parameters:

  • original (String)

    file in which to search for keep statements

  • scratch (String)

    file to which keep bodies will be copied (used to create the Cog::EmbedContext objects)

Returns:



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.

Parameters:

  • c (EmbedContext)

    describes the context in which the embed statement was found

  • opt (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opt):

  • :type (String) — default: 'cog'

    one of 'cog' or 'keep'

Yield Parameters:

  • context (EmbedContext)

    describes the context in which the embed statement was found

Yield Returns:

  • (String)

    the value to substitute into the embed expansion

Returns:

  • (Boolean, nil)

    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
      expand_body c, s, opt, &block
    end
  end
end