Class: Cog::EmbedContext

Inherits:
Object
  • Object
show all
Defined in:
lib/cog/embed_context.rb

Overview

Describes the environment of an embed statement including the file in which it was found, the line number, the language, an more.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hook, path, count) ⇒ EmbedContext

Returns a new instance of EmbedContext.

Parameters:

  • hook (String)

    hook name used in the embed statement

  • path (String)

    path to the file in which the cog directive was found

  • count (Fixnum)

    total occurrences in file



36
37
38
39
40
41
42
# 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

Instance Attribute Details

#bodyString

Returns for expanded embed statements, the body of text which occurs between the curly braces.

Examples:

// cog: my-hook {
this is
the
body
// cog: }

Returns:

  • (String)

    for expanded embed statements, the body of text which occurs between the curly braces



13
14
15
# File 'lib/cog/embed_context.rb', line 13

def body
  @body
end

#countFixnum (readonly)

Returns if multiple embeds with the same #hook occurred more than once in the same file, this value indicates the total number of occurrences.

Returns:

  • (Fixnum)

    if multiple embeds with the same #hook occurred more than once in the same file, this value indicates the total number of occurrences



27
28
29
# File 'lib/cog/embed_context.rb', line 27

def count
  @count
end

#hookString (readonly)

Returns hook name used in the embed statement.

Examples:

// cog: this-is-the-hook-name

Returns:

  • (String)

    hook name used in the embed statement



18
19
20
# File 'lib/cog/embed_context.rb', line 18

def hook
  @hook
end

#indexFixnum

Returns 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…

Returns:

  • (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…



24
25
26
# File 'lib/cog/embed_context.rb', line 24

def index
  @index
end

#keep_bodyObject



79
80
81
# File 'lib/cog/embed_context.rb', line 79

def keep_body
  @keep_body
end

#linenoFixnum

Returns line number at which the embed statement was found, with 1 being the first line in the file.

Returns:

  • (Fixnum)

    line number at which the embed statement was found, with 1 being the first line in the file



21
22
23
# File 'lib/cog/embed_context.rb', line 21

def lineno
  @lineno
end

#pathString (readonly)

Returns full file system path to the file.

Returns:

  • (String)

    full file system path to the file



30
31
32
# File 'lib/cog/embed_context.rb', line 30

def path
  @path
end

Instance Method Details

#actual_indexFixnum

Returns takes into account the number of once statements that have been eaten.

Returns:

  • (Fixnum)

    takes into account the number of once statements that have been eaten



119
120
121
# File 'lib/cog/embed_context.rb', line 119

def actual_index
  @index - @eaten
end

#argsArray<String>

Returns arguments provided with the embed statement.

Examples:

// cog: my-hook (these are the args)

Returns:

  • (Array<String>)

    arguments provided with the embed statement



47
48
49
# File 'lib/cog/embed_context.rb', line 47

def args
  @args
end

#args=(value) ⇒ Object

Parameters:

  • value (Array<String>)

    arguments used with the directive



101
102
103
# File 'lib/cog/embed_context.rb', line 101

def args=(value)
  @args = value
end

#eaten=(value) ⇒ Object

Parameters:

  • 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



113
114
115
# File 'lib/cog/embed_context.rb', line 113

def eaten=(value)
  @eaten = value
end

#extensionString

Returns the filename extension (without the period).

Returns:

  • (String)

    the filename extension (without the period)



52
53
54
# File 'lib/cog/embed_context.rb', line 52

def extension
  @ext ||= File.extname(filename).slice(1..-1)
end

#filenameString

Returns basename of the file in which the embed statement was found.

Returns:

  • (String)

    basename of the file in which the embed statement was found



57
58
59
# File 'lib/cog/embed_context.rb', line 57

def filename
  @filename ||= File.basename @path
end

#first?Boolean

Returns whether or not this was the first occurrence of the embed #hook in the file.

Returns:

  • (Boolean)

    whether or not this was the first occurrence of the embed #hook in the file



62
63
64
# File 'lib/cog/embed_context.rb', line 62

def first?
  @index == 0
end

#last?Boolean

Returns whether or not this was the last occurrence of the embed #hook in the file.

Returns:

  • (Boolean)

    whether or not this was the last occurrence of the embed #hook in the file



67
68
69
# File 'lib/cog/embed_context.rb', line 67

def last?
  @index == @count - 1
end

#once=(value) ⇒ Object

Parameters:

  • value (Boolean)

    was the once switch used?



95
96
97
# File 'lib/cog/embed_context.rb', line 95

def once=(value)
  @once = !!value
end

#once?Boolean

Returns was the ‘once’ switch used? Embed statements using this switch will be removed after the statement is expanded.

Examples:

// cog: my-hook once

Returns:

  • (Boolean)

    was the ‘once’ switch used? Embed statements using this switch will be removed after the statement is expanded



74
75
76
# File 'lib/cog/embed_context.rb', line 74

def once?
  @once
end

#to_statement(type = 'cog') ⇒ String

Returns:



125
126
127
128
129
130
# 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