Class: YamlMaster

Inherits:
Object
  • Object
show all
Defined in:
lib/yaml_master.rb,
lib/yaml_master/version.rb

Defined Under Namespace

Classes: EmbeddedMethods, KeyFetchError, PropertyParseError

Constant Summary collapse

VERSION =
"0.4.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io_or_filename, property_strings = []) ⇒ YamlMaster

Returns a new instance of YamlMaster.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/yaml_master.rb', line 22

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)
      ERB.new(File.read(io_or_filename)).result(embedded_methods_binding)
    end

  @master = YAML.load(data)
  raise "yaml_master key is necessary on toplevel" unless @master["yaml_master"]
  raise "data key is necessary on toplevel" unless @master["data"]
end

Instance Attribute Details

#masterObject (readonly)

Returns the value of attribute master.



20
21
22
# File 'lib/yaml_master.rb', line 20

def master
  @master
end

#master_pathObject (readonly)

Returns the value of attribute master_path.



20
21
22
# File 'lib/yaml_master.rb', line 20

def master_path
  @master_path
end

#propertiesObject (readonly)

Returns the value of attribute properties.



20
21
22
# File 'lib/yaml_master.rb', line 20

def properties
  @properties
end

Instance Method Details

#generate(key, output = nil, options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/yaml_master.rb', line 39

def generate(key, output = nil, options = {})
  puts "gen: #{output}" if options[:verbose]
  yaml = YAML.dump(fetch_data_from_master(key))

  return yaml unless output

  File.open(output, 'w') do |f|
    f.write(yaml)
  end
end

#generate_all(options = {}) ⇒ Object



50
51
52
53
54
# File 'lib/yaml_master.rb', line 50

def generate_all(options = {})
  @master["yaml_master"].each do |key, output|
    generate(key, output, options)
  end
end