module Cumuliform::DSL::Fragments
DSL
methods for creating and reusing template fragments
Public Instance Methods
def_fragment(name, &block)
click to toggle source
Define a fragment for later use.
Essentially stores a block under the name given for later use.
@param name [Symbol] name of the fragment to define @yieldparam opts [Hash] will yield the options hash passed to
<tt>#fragment()</tt> when called
@raise [Error::FragmentAlreadyDefined] if the name
is not
unique in this template
# 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)
click to toggle source
@api private
# 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)
click to toggle source
Use an already-defined fragment
Retrieves the block stored under name
and calls it, passing any options.
@param name [Symbol] The name of the fragment to use @param opts [Hash] Options to be passed to the fragment @return [Object<JSON-serialisable>] the return value of the called
block
# 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
Private Instance Methods
fragments()
click to toggle source
# File lib/cumuliform/dsl/fragments.rb, line 58 def fragments @fragments ||= {} end
has_fragment?(name)
click to toggle source
# File lib/cumuliform/dsl/fragments.rb, line 62 def has_fragment?(name) !find_fragment(name).nil? end
use_fragment(name, opts = {})
click to toggle source
# File lib/cumuliform/dsl/fragments.rb, line 51 def use_fragment(name, opts = {}) unless has_fragment?(name) raise Error::FragmentNotFound, name end instance_exec(opts, &find_fragment(name)) end