lib/yaml_master.rb in yaml_master-0.4.2 vs lib/yaml_master.rb in yaml_master-1.0.0.beta
- old
+ new
@@ -1,52 +1,49 @@
require "yaml_master/version"
require "yaml"
require "erb"
require "pathname"
-require "pp"
-YAML.add_domain_type(nil, "include") do |type, val|
- YAML.load_file(val)
-end
+require "yaml_master/yaml_tree_builder"
class YamlMaster
class KeyFetchError < StandardError
def initialize(data, key)
super("cannot fetch key \"#{key}\" from\n#{data.pretty_inspect}")
end
end
- class PropertyParseError < StandardError; end
-
attr_reader :master, :master_path, :properties
def initialize(io_or_filename, property_strings = [])
- parse_properties(property_strings)
+ case io_or_filename
+ when String
+ @master_path = Pathname(io_or_filename).expand_path
+ when File
+ @master_path = Pathname(io_or_filename.absolute_path)
+ end
- embedded_methods_binding = EmbeddedMethods.new(self).get_binding
- data =
- if io_or_filename.is_a?(IO)
- ERB.new(io_or_filename.read).result(embedded_methods_binding)
- else
- @master_path = File.expand_path(io_or_filename)
- ERB.new(File.read(io_or_filename)).result(embedded_methods_binding)
- end
+ @properties = PropertyParser.parse_properties(property_strings)
+ yaml = Context.new(master_path, @properties).render_master
- @master = YAML.load(data)
+ parser = YAML::Parser.new
+ parser.handler = YamlMaster::YAMLTreeBuilder.new(@master_path, @properties, parser)
+ @tree = parser.parse(yaml).handler.root
+ @master = @tree.to_ruby[0]
+
raise "yaml_master key is necessary on toplevel" unless @master["yaml_master"]
raise "data key is necessary on toplevel" unless @master["data"]
end
def generate(key, output = nil, options = {})
- puts "gen: #{output}" if options[:verbose]
yaml = YAML.dump(fetch_data_from_master(key))
+ write_to_output(yaml, output, options[:verbose])
+ end
- return yaml unless output
-
- File.open(output, 'w') do |f|
- f.write(yaml)
- end
+ def dump(output = nil, options = {})
+ yaml = @tree.to_yaml
+ write_to_output(yaml, output, options[:verbose])
end
def generate_all(options = {})
@master["yaml_master"].each do |key, output|
generate(key, output, options)
@@ -74,34 +71,47 @@
k
end
end
end
- def parse_properties(property_strings)
- @properties = {}
- if property_strings.is_a?(Hash)
- @properties = property_strings
- else
- property_strings.each_with_object(@properties) do |str, hash|
- key, value = str.split("=")
- raise PropertyParseError.new("#{str} is invalid format") unless key && value
- hash[key] = value
- end
+ def write_to_output(yaml, output, verbose)
+ if output && verbose
+ puts <<~VERBOSE
+ gen: #{output}
+ #{yaml}
+ VERBOSE
end
- end
- class EmbeddedMethods
- def initialize(yaml_master)
- @yaml_master = yaml_master
+ return yaml unless output
+
+ File.open(output, 'w') do |f|
+ f.write(yaml)
end
+ end
- def master_path
- Pathname(@yaml_master.master_path)
+ module PropertyParser
+ class ParseError < StandardError; end
+
+ def self.parse_properties(property_strings_or_hash)
+ if property_strings_or_hash.is_a?(Hash)
+ property_strings_or_hash
+ else
+ property_strings_or_hash.each_with_object({}) do |str, hash|
+ key, value = str.split("=")
+ raise ParseError.new("#{str} is invalid format") unless key && value
+ hash[key] = value
+ end
+ end
end
+ end
- def properties
- @yaml_master.properties
+ class Context
+ attr_reader :master_path, :properties
+
+ def initialize(master_path, properties)
+ @master_path = master_path
+ @properties = properties
end
def user_home
Pathname(ENV["HOME"])
end
@@ -109,10 +119,10 @@
def read_file_if_exist(path)
return nil unless File.exist?(path)
File.read(path)
end
- def get_binding
- binding
+ def render_master
+ ERB.new(File.read(@master_path)).result(binding)
end
end
end