lib/yaml_master.rb in yaml_master-0.3.0 vs lib/yaml_master.rb in yaml_master-0.4.0

- old
+ new

@@ -13,16 +13,18 @@ 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 + attr_reader :master, :master_path, :properties - def initialize(io_or_filename) - embedded_methods = EmbeddedMethods.new(self) - embedded_methods_binding = embedded_methods.instance_eval { binding } + def initialize(io_or_filename, property_strings = []) + parse_properties(property_strings) + + 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) @@ -72,24 +74,41 @@ k end end end + def parse_properties(property_strings) + @properties = {} + 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 + end + class EmbeddedMethods def initialize(yaml_master) @yaml_master = yaml_master end def master_path Pathname(@yaml_master.master_path) end + def properties + @yaml_master.properties + end + def user_home Pathname(ENV["HOME"]) end def read_file_if_exist(path) return nil unless File.exist?(path) File.read(path) + end + + def get_binding + binding end end end