class Ruyml::Data

Public Class Methods

new(hash) click to toggle source

Create RUYML data from a YAML hash. Underlying hashes will be accessed as instances of this class. Other types are kept untouched and returned as is.

# File lib/ruyml.rb, line 13
def initialize(hash)
  @hash = hash
  @hash.each do |k, v|
    self.define_singleton_method k.to_sym do
      return v.class == Hash ? Data.new(v) : v
    end
  end
end

Public Instance Methods

render(template, output = nil) click to toggle source

Renders RUYML data using the given template. Rendered data is either written to an optional output file path or to stdout.

# File lib/ruyml.rb, line 25
def render(template, output = nil)
  result = ERB.new(File.read(template), 0, '-').result(binding)
  if !output.nil?
    File.open(output, "w") do |file|
      file.write(result)
    end
  else
    puts result
  end
end

Private Instance Methods

method_missing(name, *args, &block) click to toggle source

Access Hash class methods. If a YAML propertie matches the method name, then the target method is called if a ‘!’ terminates the method name.

# File lib/ruyml.rb, line 40
def method_missing(name, *args, &block)
  method = name.to_s
  if method =~ /.+!$/ && self.respond_to?(method.chop)
    method.chop!
  end
  if @hash.respond_to?(method)
    @hash.send(method, *args, &block)
  else
    raise NameError, "Undefined property or method '#{name}'"
  end
end