Class: Eggshell::Bundles::Basic::DataLoaderMacro

Inherits:
Object
  • Object
show all
Includes:
MacroHandler
Defined in:
lib/eggshell/bundles/basics.rb

Overview

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.

Defined Under Namespace

Modules: Loader Classes: JsonLoader, YamlLoader

Constant Summary collapse

@@handlers =
{}

Constants included from MacroHandler

MacroHandler::CHAIN_CONTINUE, MacroHandler::CHAIN_END, MacroHandler::CHAIN_NONE, MacroHandler::CHAIN_START, MacroHandler::COLLECT_NORMAL, MacroHandler::COLLECT_RAW, MacroHandler::COLLECT_RAW_MACRO

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MacroHandler

#chain_type

Class Method Details

.add_loader(type, handler) ⇒ Object

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



1059
1060
1061
# File 'lib/eggshell/bundles/basics.rb', line 1059

def self.add_loader(type, handler)
	@@handlers[type] = handler
end

Instance Method Details

#collection_type(macro) ⇒ Object



1080
1081
1082
# 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) ⇒ Object



1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
# 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) ⇒ Object



1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
# 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