Class: Cog::EmbedContext
- Inherits:
-
Object
- Object
- Cog::EmbedContext
- 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
-
#body ⇒ String
For expanded embed statements, the body of text which occurs between the curly braces.
-
#count ⇒ Fixnum
readonly
If multiple embeds with the same #hook occurred more than once in the same file, this value indicates the total number of occurrences.
-
#hook ⇒ String
readonly
Hook name used in the embed statement.
-
#index ⇒ 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.
- #keep_body ⇒ Object
-
#lineno ⇒ Fixnum
Line number at which the embed statement was found, with 1 being the first line in the file.
-
#path ⇒ String
readonly
Full file system path to the file.
Instance Method Summary collapse
-
#actual_index ⇒ Fixnum
Takes into account the number of once statements that have been eaten.
-
#args ⇒ Array<String>
Arguments provided with the embed statement.
- #args=(value) ⇒ Object
- #eaten=(value) ⇒ Object
-
#extension ⇒ String
The filename extension (without the period).
-
#filename ⇒ String
Basename of the file in which the embed statement was found.
-
#first? ⇒ Boolean
Whether or not this was the first occurrence of the embed #hook in the file.
-
#initialize(hook, path, count) ⇒ EmbedContext
constructor
A new instance of EmbedContext.
-
#last? ⇒ Boolean
Whether or not this was the last occurrence of the embed #hook in the file.
- #once=(value) ⇒ Object
-
#once? ⇒ Boolean
Was the ‘once’ switch used? Embed statements using this switch will be removed after the statement is expanded.
- #to_statement(type = 'cog') ⇒ String
Constructor Details
#initialize(hook, path, count) ⇒ EmbedContext
Returns a new instance of EmbedContext.
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
#body ⇒ String
Returns 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 |
#count ⇒ Fixnum (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.
27 28 29 |
# File 'lib/cog/embed_context.rb', line 27 def count @count end |
#hook ⇒ String (readonly)
Returns hook name used in the embed statement.
18 19 20 |
# File 'lib/cog/embed_context.rb', line 18 def hook @hook end |
#index ⇒ Fixnum
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…
24 25 26 |
# File 'lib/cog/embed_context.rb', line 24 def index @index end |
#keep_body ⇒ Object
79 80 81 |
# File 'lib/cog/embed_context.rb', line 79 def keep_body @keep_body end |
#lineno ⇒ Fixnum
Returns 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 |
#path ⇒ String (readonly)
Returns 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_index ⇒ Fixnum
Returns 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 |
#args ⇒ Array<String>
Returns arguments provided with the embed statement.
47 48 49 |
# File 'lib/cog/embed_context.rb', line 47 def args @args end |
#args=(value) ⇒ Object
101 102 103 |
# File 'lib/cog/embed_context.rb', line 101 def args=(value) @args = value end |
#eaten=(value) ⇒ Object
113 114 115 |
# File 'lib/cog/embed_context.rb', line 113 def eaten=(value) @eaten = value end |
#extension ⇒ String
Returns 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 |
#filename ⇒ String
Returns 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.
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.
67 68 69 |
# File 'lib/cog/embed_context.rb', line 67 def last? @index == @count - 1 end |
#once=(value) ⇒ Object
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.
74 75 76 |
# File 'lib/cog/embed_context.rb', line 74 def once? @once end |
#to_statement(type = 'cog') ⇒ String
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 |