Module: Cumuliform::DSL::Fragments

Included in:
Template
Defined in:
lib/cumuliform/dsl/fragments.rb

Overview

DSL methods for creating and reusing template fragments

Instance Method Summary collapse

Instance Method Details

#def_fragment(name) {|opts| ... } ⇒ Object

Define a fragment for later use.

Essentially stores a block under the name given for later use.

Parameters:

  • name (Symbol)

    name of the fragment to define

Yield Parameters:

  • opts (Hash)

    will yield the options hash passed to #fragment() when called

Raises:



16
17
18
19
20
21
# File 'lib/cumuliform/dsl/fragments.rb', line 16

def def_fragment(name, &block)
  if fragments.has_key?(name)
    raise Error::FragmentAlreadyDefined, name
  end
  fragments[name] = block
end

#find_fragment(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



42
43
44
45
46
47
# File 'lib/cumuliform/dsl/fragments.rb', line 42

def find_fragment(name)
  local_fragment = fragments[name]
  imports.reverse.reduce(local_fragment) { |fragment, import|
    fragment || import.find_fragment(name)
  }
end

#fragment(name, *args, &block) ⇒ Object<JSON-serialisable>

Use an already-defined fragment

Retrieves the block stored under name and calls it, passing any options.

Parameters:

  • name (Symbol)

    The name of the fragment to use

  • opts (Hash)

    Options to be passed to the fragment

Returns:

  • (Object<JSON-serialisable>)

    the return value of the called block



32
33
34
35
36
37
38
39
# File 'lib/cumuliform/dsl/fragments.rb', line 32

def fragment(name, *args, &block)
  if block_given?
    warn "fragment definition form (with block) is deprecated. Use #def_fragment instead"
    def_fragment(name, *args, &block)
  else
    use_fragment(name, *args)
  end
end