class Processor

Attributes

file_content[W]
source_file[RW]

Public Class Methods

new(source_file:, file_content: nil) click to toggle source
# File lib/dy.rb, line 20
def initialize(source_file:, file_content: nil)
  self.source_file = source_file
  self.file_content = file_content
end

Public Instance Methods

hash() click to toggle source
# File lib/dy.rb, line 25
def hash
  hash = file_content
  process! hash
  hash
end

Private Instance Methods

file_content() click to toggle source
# File lib/dy.rb, line 33
def file_content
  @file_content ||= YAML.load_file(source_file)
end
maybe_processed!(o) click to toggle source
# File lib/dy.rb, line 41
def maybe_processed!(o)
  data = process!(o)
  if data == NO_CHANGE
    [o, false]
  else
    [data, true]
  end
end
process!(o) click to toggle source
# File lib/dy.rb, line 50
def process!(o)
  if o.is_a?(Hash)
    if o.has_key?("__dy__")
      process_dy!(o)
    else
      process_hash!(o)
    end
  elsif o.is_a?(Array)
    process_array!(o)
  else
    NO_CHANGE
  end
end
process_array!(array) click to toggle source
# File lib/dy.rb, line 89
def process_array!(array)
  any_splice = false
  changed = false
  array.each_with_index do |value, index|
    array[index], processed = maybe_processed!(value)
    any_splice ||= array[index].is_a?(Splice)
    changed ||= processed
  end

  splice_array!(array) if any_splice

  changed ? array : NO_CHANGE
end
process_dy!(dy) click to toggle source
# File lib/dy.rb, line 64
def process_dy!(dy)
  case dy["__dy__"]
  when "escape" then o["value"]
  when "text_file"
    read_file(relative_path(dy["file"]), erb: dy["erb"], args: dy["args"])
  when "data_file"
    file = relative_path(dy["file"])
    content = read_file(file, erb: dy["erb"], args: dy["args"])
    data = YAML.load(content)
    data = Processor.new(source_file: file).hash
    dy["splice"] ? Splice.new(data) : data
  else
    raise "Unknown __dy__ #{dy["__dy__"].inspect}"
  end
end
process_hash!(hash) click to toggle source
# File lib/dy.rb, line 80
def process_hash!(hash)
  changed = false
  hash.each do |key, value|
    hash[key], processed = maybe_processed!(value)
    changed ||= processed
  end
  changed ? hash : NO_CHANGE
end
relative_path(path) click to toggle source
# File lib/dy.rb, line 37
def relative_path(path)
  File.expand_path(path, File.dirname(source_file))
end