class Cog::EmbedContext
Describes the environment of an embed statement including the file in which it was found, the line number, the language, an more.
Attributes
@return [String] for expanded embed statements, the body of text which occurs between the curly braces @example
// cog: my-hook { this is the body // cog: }
@return [Fixnum] if multiple embeds with the same {#hook} occurred more than once in the same file, this value indicates the total number of occurrences
@return [String] hook name used in the embed statement @example
// cog: this-is-the-hook-name
@return [Fixnum] if multiple embeds with the same {#hook} occurred more than once in the same file, this value indicates to which occurrence this context belongs. 0 for the first, 1 for the second, and so on…
@api developer
@return [Fixnum] line number at which the embed statement was found, with 1 being the first line in the file
@return [String] full file system path to the file
Public Class Methods
@api developer @param hook [String] hook name used in the embed statement @param path [String] path to the file in which the cog directive was found @param count [Fixnum] total occurrences in file
# File lib/cog/embed_context.rb, line 36 def initialize(hook, path, count) @hook = hook.to_s @path = path.to_s @count = count @eaten = 0 @index = 0 end
Public Instance Methods
@api developer @return [Fixnum] takes into account the number of once statements that have been eaten
# File lib/cog/embed_context.rb, line 119 def actual_index @index - @eaten end
@return [Array<String>] arguments provided with the embed statement @example
// cog: my-hook (these are the args)
# File lib/cog/embed_context.rb, line 47 def args @args end
@api developer @param value [Array<String>] arguments used with the directive
# File lib/cog/embed_context.rb, line 101 def args=(value) @args = value end
@api developer @param value [String, nil] set the body to this value
# File lib/cog/embed_context.rb, line 83 def body=(value) @body = value end
@api developer @param value [Fixnum] the number of once statements before this one that were eaten. Used to adjust the actual_index
so that the file_scanner looks for the right statement
# File lib/cog/embed_context.rb, line 113 def eaten=(value) @eaten = value end
@return [String] the filename extension (without the period)
# File lib/cog/embed_context.rb, line 52 def extension @ext ||= File.extname(filename).slice(1..-1) end
@return [String] basename of the file in which the embed statement was found
# File lib/cog/embed_context.rb, line 57 def filename @filename ||= File.basename @path end
@return [Boolean] whether or not this was the first occurrence of the embed {#hook} in the file
# File lib/cog/embed_context.rb, line 62 def first? @index == 0 end
@api developer @param index [Fixnum] occurrence in file, 0 for first
# File lib/cog/embed_context.rb, line 107 def index=(index) @index = index end
@return [Boolean] whether or not this was the last occurrence of the embed {#hook} in the file
# File lib/cog/embed_context.rb, line 67 def last? @index == @count - 1 end
@api developer @param value [Fixnum] set the line number
# File lib/cog/embed_context.rb, line 89 def lineno=(value) @lineno = value end
@api developer @param value [Boolean] was the once switch used?
# File lib/cog/embed_context.rb, line 95 def once=(value) @once = !!value end
@return [Boolean] was the 'once' switch used? Embed statements using this switch will be removed after the statement is expanded @example
// cog: my-hook once
# File lib/cog/embed_context.rb, line 74 def once? @once end
@api developer @return [String]
# File lib/cog/embed_context.rb, line 125 def to_statement(type='cog') x = "#{type}: #{hook}" x += "(#{args.join ' '})" if args x += " once" if once? x end