class DslRuntime

Class which evaluates DSL and read XML files to populate the namespace with classes

Public Class Methods

load(path) click to toggle source
# File lib/roundtrip_xml/dsl_runtime.rb, line 202
def self.load(path)
  file = File.open path, 'r'
  obj = Marshal.load file
  file.close
  obj
end
new() click to toggle source
Calls superclass method LifecycleCallbacks::new
# File lib/roundtrip_xml/dsl_runtime.rb, line 16
def initialize()
  super
  @classes = {}
  @root_classes = Set.new
end

Public Instance Methods

add_class(name, clazz) click to toggle source
# File lib/roundtrip_xml/dsl_runtime.rb, line 77
def add_class(name, clazz)
  @classes[name] = clazz
end
add_unprocessed_attr(attr_config, clazz) click to toggle source
# File lib/roundtrip_xml/dsl_runtime.rb, line 144
def add_unprocessed_attr(attr_config, clazz)
  @unprocessed_attrs ||= []
  @unprocessed_attrs << AttrJob.new(attr_config, clazz)
end
child_classes(hash, config) click to toggle source
# File lib/roundtrip_xml/dsl_runtime.rb, line 187
def child_classes(hash, config)
  config[:attrs].map do |attr|
    attr_type = attr[:opts][:as]
    type = attr_type.class == Array ? attr_type.first : attr_type
    type if hash.key? type
  end.compact
end
create_cleanroom(root_class, show_undefined_params = false) click to toggle source
# File lib/roundtrip_xml/dsl_runtime.rb, line 103
def create_cleanroom(root_class, show_undefined_params = false)
  BaseCleanroom.new(root_class.new, self, show_undefined_params)
end
deserialize_class(node, config) click to toggle source
# File lib/roundtrip_xml/dsl_runtime.rb, line 131
def deserialize_class(node, config)
  clazz = fetch(node.name) || new_roxml_class(config[:xml_name])
  config[:attrs].each do |attr|
    type_is_parent = node.parentage && (node.parentage << node).any? {|n| n.name == attr[:opts][:as]}
    if type_is_parent
      add_unprocessed_attr attr, clazz
    else
      clazz.xml_accessor attr[:name], transform_accessor_opts(attr[:opts])
    end
  end
  clazz
end
evaluate_file(path, root_class, templates = []) click to toggle source
# File lib/roundtrip_xml/dsl_runtime.rb, line 81
def evaluate_file(path, root_class, templates = [])
  cleanroom = RootCleanroom.new(fetch(root_class).new, self)
  templates.each { |t| cleanroom.evaluate_file t }
  cleanroom.evaluate_file path

end
evaluate_raw(dsl, root_class, templates = [], &block) click to toggle source
# File lib/roundtrip_xml/dsl_runtime.rb, line 88
def evaluate_raw(dsl, root_class, templates = [], &block)
  cleanroom = RootCleanroom.new(fetch(root_class).new, self)
    templates.each { |t| cleanroom.evaluate t }
    if block_given?
      cleanroom.evaluate &block
    else
    cleanroom.evaluate dsl
  end

end
fetch(class_name) click to toggle source
# File lib/roundtrip_xml/dsl_runtime.rb, line 73
def fetch(class_name)
  @classes[class_name]
end
fetch_cleanroom(root_class) click to toggle source
# File lib/roundtrip_xml/dsl_runtime.rb, line 99
def fetch_cleanroom(root_class)
  BaseCleanroom.new(fetch(root_class).new, self)
end
hash_to_tree(hash, root_name, processed = []) click to toggle source
# File lib/roundtrip_xml/dsl_runtime.rb, line 177
def hash_to_tree(hash, root_name, processed = [])
  node = Tree::TreeNode.new(root_name, hash[root_name])
  processed << root_name
  children = child_classes(hash, hash[root_name])
  children.each do |name|
    node << hash_to_tree(hash, name, processed) unless processed.include? name
  end
  node
end
marshal_dump() click to toggle source
# File lib/roundtrip_xml/dsl_runtime.rb, line 107
def marshal_dump
  classes = @classes.inject({}) do |hash, (name, clazz)|
    hash[name] = {xml_name: clazz.tag_name }
    hash[name][:attrs] = clazz.roxml_attrs.map do |accessor|
      type = accessor.sought_type.class == Class ?
        accessor.sought_type.class_name : accessor.sought_type
      from = accessor.sought_type == :attr ? "@#{accessor.name}" : accessor.name
      {
        name: accessor.accessor,
        opts: {
          as: accessor.array? ? [type] : type,
          from: from
        }
      }
    end
    hash
  end

  {
    root_classes: @root_classes,
    classes: classes
  }
end
marshal_load(data) click to toggle source
# File lib/roundtrip_xml/dsl_runtime.rb, line 161
def marshal_load(data)
  initialize
  @root_classes.merge data[:root_classes]
  trees = @root_classes.map {|clazz| hash_to_tree data[:classes], clazz}
  trees.each do |tree|
    tree.postordered_each do |node|
      @classes[node.name] = deserialize_class(node, node.content)
    end
  end
  @unprocessed_attrs.each do |job|
    clazz = job.class
    config = job.config
    clazz.xml_accessor config[:name], transform_accessor_opts(config[:opts])
  end if @unprocessed_attrs
end
populate(files) click to toggle source
# File lib/roundtrip_xml/dsl_runtime.rb, line 21
def populate(files)
  files.map {|f| populate_from_file f }
end
populate_from_file(file) click to toggle source
# File lib/roundtrip_xml/dsl_runtime.rb, line 25
def populate_from_file (file)
  populate_raw File.read(file)

end
populate_raw(raw) click to toggle source

@param root_method Symbol The method of the ROXML object to access its children

# File lib/roundtrip_xml/dsl_runtime.rb, line 32
def populate_raw (raw)
  builder = RoxmlBuilder.new (Nokogiri::XML(raw).root), @classes
  new_classes = builder.build_classes
  @root_classes << builder.root_class_name
  @classes.merge! new_classes
end
serialize(path) click to toggle source
# File lib/roundtrip_xml/dsl_runtime.rb, line 196
def serialize(path)
  file = File.new path, 'w'
  Marshal.dump self, file
  file.close
end
transform_accessor_opts(opts) click to toggle source
# File lib/roundtrip_xml/dsl_runtime.rb, line 149
def transform_accessor_opts(opts)
  attr_type = opts[:as]
  is_array = attr_type.class == Array
  type = is_array ? attr_type.first : attr_type
  if is_array
    opts[:as] = [fetch(type)]
  else
    opts[:as] = fetch type
  end
  opts
end
write_dsl(xml, root_class_name, root_method, helpers = nil) { |obj| ... } click to toggle source

@param block Proc This proc is passed each health rule and must return a string value which specifies the partition for it

# File lib/roundtrip_xml/dsl_runtime.rb, line 40
def write_dsl(xml, root_class_name, root_method, helpers = nil, &block)
  roxml_root = fetch(root_class_name).from_xml xml

  trigger :after_roxml_created, roxml_root
  extractor = Extractor.new roxml_root.send(root_method), self, root_class_name, helpers

  new_objs = extractor.convert_roxml_objs
  if block_given?
    partitions = {}
    new_objs.each do |obj|
      partition = yield obj
      partitions[partition] ||= []
      partitions[partition] << obj
    end
    partitions
    dsl = partitions.inject({}) do |out, (partition, objs)|
      out[partition] = write_dsl_helper roxml_root, root_method, objs
      out
    end
  else
    dsl = write_dsl_helper roxml_root, root_method, new_objs
  end

  dsl
end
write_dsl_helper(root, root_method, objs) click to toggle source
# File lib/roundtrip_xml/dsl_runtime.rb, line 66
def write_dsl_helper(root, root_method, objs)
  root.send("#{root_method}=", objs)
  builder = SexpDslBuilder.new root, self

  builder.write_full_dsl root_method
end