Class: Cecil::BlockContext

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/cecil/block_context.rb

Overview

The BlockContext contains methods available to you inside a Cecil block.

Methods available in the scope of a Cecil block are:

  • Methods & variables from local scope
  • BlockContext instance methods for emitting code (listed below)
  • Helper methods in your Code subclass' Helpers. See Code for defining your own helper methods.

Examples:

Methods available in a Cecil block's scope

def has_data?(first_name) = File.exist?("data/#{first_name}.json")

name = "Bob"
last_names = ["McTesterson", "Rickenbacker"]

Cecil::Lang::TypeScript.generate_string do
  content_for :imports # `content_for` is a BlockContext instance method

  `let firstName = "$username"`[name] # `name` is a local variable

  `let lastNames = $lastNames`[j last_names] # `j` helper via Cecil::Lang::TypeScript::Helpers

  if has_data?(name) # has_data? comes from local scope
    content_for :imports do
      `import userData from './data/$first_name.json`[s name]
    end
  end

end

Instance Method Summary collapse

Instance Method Details

#`(source_string) ⇒ Object

Alias for #src



60
# File 'lib/cecil/block_context.rb', line 60

def `(source_string) = @builder.src(source_string)

#content_for(key) ⇒ nil #content_for(key) { ... } ⇒ nil

Stores content for the given key to be insert at a different location in the document.

If a block is passed, it will be executed and the result stored. If no block is passed but the key already has content, it will be retrieved. Otherwise, content rendering will be deferred until later.

Examples:

Storing content for earlier insertion

content_for :imports # inserts `import { Component } from 'react'` here
# ...
content_for :imports do # store
  `import { Component } from 'react'`
end

Storing content for later insertion

`job1 = new Job()`
content_for :run_jobs do # store
  `job1.run()`
end

`job2 = new Job()`
content_for :run_jobs do # store
  `job2.run()`
end
# ...
content_for :run_jobs # adds `job1.run()` and `job2.run()`

Storing multiple lines

content_for :functions

content_for :functions do
  `function $fnName() {`[fn_name] do
    `api.fetch('$fnName', $fn_arg)`[fn_name, fn_arg.to_json]
  end
  `function undo$fnName() {`[fn_name] do
    `api.fetch('undo$fnName', $fn_arg)`[fn_name, fn_arg.to_json]
  end
end

Using different types for keys

content_for :imports
content_for "imports"
content_for ["imports", :secion1]

user = User.find(1)
content_for user

Overloads:

  • #content_for(key) ⇒ nil

    Insert the stored content for the given key

    Returns:

    • (nil)

      A node of stored content for the given key

  • #content_for(key) { ... } ⇒ nil

    Store content to be be inserted at a different position in the file

    Yields:

    • The content in the block is evaluated immediately and stored for later insertion

    Returns:

    • (nil)

Parameters:

  • key (#hash)

    Any hashable object to identify the content but can be anything that works as a hash key

Returns:

  • (nil)


128
# File 'lib/cecil/block_context.rb', line 128

def_delegator :@builder, :content_for

#content_for!(key) ⇒ Array<Node::Detached>

Returns the content stored for the given key, and raises an exception if there is no content stored. Calling #content_for! is evaluated immeditately and will raise an exception even if #content_for(key) { ... } is called later.

Parameters:

  • key (#hash)

    Any hashable object to identify the content

Returns:

  • (Array<Node::Detached>)

    A node of stored content for the given key

Raises:

  • (Exception)

    Throws an execption if there is no content stored at the given key



148
# File 'lib/cecil/block_context.rb', line 148

def_delegator :@builder, :content_for!

#content_for?(key) ⇒ Boolean

Returns whether there is any content stored for the given key.

This method returns immediately and will return false even if #content_for(key) { ... } is called later.

Parameters:

  • key (#hash)

    Any hashable object to identify the content

Returns:

  • (Boolean)

    whether any content is stored for the given key



138
# File 'lib/cecil/block_context.rb', line 138

def_delegator :@builder, :content_for?

#defer(&) ⇒ Node::Deferred

Defer execution of the the given block until the rest of the document is evaluated and insert any content in the document where this method was called.

Returns:



67
# File 'lib/cecil/block_context.rb', line 67

def_delegator :@builder, :defer

#src(source_string) ⇒ Node #`(source_string) ⇒ Node

Inserts a node with the given source string.

The inserted node can be modified by calling Node#with/Node#[]

Returns:

  • (Node)

    the inserted node



57
# File 'lib/cecil/block_context.rb', line 57

def_delegator :@builder, :src