class Eggshell::Bundles::Basic::DataLoaderMacro

Allows you to parse and load various data formats into a variable. Data loaders are registered through class methods.

Usage:

pre. @dataload('json', 'varname') {/ {“jsonkey”: value} /} \ @dataload('yaml', 'varname', 'include_file')

If a file is specified for inclusion, it will be restricted to any path constraints defined by the `@include` macro.

Public Class Methods

add_loader(type, handler) click to toggle source

Adds a data loading handler. If it's a class, should accept a null constructor.

# File lib/eggshell/bundles/basics.rb, line 1059
def self.add_loader(type, handler)
        @@handlers[type] = handler
end

Public Instance Methods

collection_type(macro) click to toggle source
# File lib/eggshell/bundles/basics.rb, line 1080
def collection_type(macro)
        MH::COLLECT_RAW_MACRO
end
process(name, args, lines, out, call_depth = 0) click to toggle source
# File lib/eggshell/bundles/basics.rb, line 1084
def process(name, args, lines, out, call_depth = 0)
        type = args[0]
        varname = args[1]
        src = args[2]
        if !varname
                @eggshell._warn("@dataload: no varname specified for #{type}")
                return
        end

        handler = @handlers[type]
        if !handler
                @eggshell._warn("@dataload: no handler specified for #{type}")
                return
        end

        # @todo support external protocols (e.g. http)?
        if src
                paths = CoreMacros.make_include_paths(src, @eggshell.vars)
                paths.each do |path|
                        if File.exists?(path)
                                @eggshell.vars[varname] = handler.parse_io(File.new(path))
                                break
                        end
                end
        else
                raw = lines.join("\n")
                @eggshell.vars[varname] = handler.parse(raw)
        end
end
set_processor(proc, opts = nil) click to toggle source
# File lib/eggshell/bundles/basics.rb, line 1063
def set_processor(proc, opts = nil)
        opts = {} if !opts
        @opts = opts
        @eggshell = proc
        @eggshell.add_macro_handler(self, 'dataload')
        @vars = @eggshell.vars

        @handlers = {}
        @@handlers.each do |type, handler|
                if handler.is_a?(Class)
                        @handlers[type] = handler.new
                else
                        @handlers[type] = handler
                end
        end
end