Class: DslRuntime
- Inherits:
-
Object
- Object
- DslRuntime
- Includes:
- Utils
- Defined in:
- lib/roundtrip_xml/dsl_runtime.rb
Overview
Class which evaluates DSL and read XML files to populate the namespace with classes
Constant Summary
Constants included from Utils
Class Method Summary collapse
Instance Method Summary collapse
- #add_class(name, clazz) ⇒ Object
- #add_unprocessed_attr(attr_config, clazz) ⇒ Object
- #child_classes(hash, config) ⇒ Object
- #create_cleanroom(root_class, show_undefined_params = false) ⇒ Object
- #deserialize_class(node, config) ⇒ Object
- #evaluate_file(path, root_class, templates = []) ⇒ Object
- #evaluate_raw(dsl, root_class, templates = [], &block) ⇒ Object
- #fetch(class_name) ⇒ Object
- #fetch_cleanroom(root_class) ⇒ Object
- #hash_to_tree(hash, root_name, processed = []) ⇒ Object
-
#initialize ⇒ DslRuntime
constructor
A new instance of DslRuntime.
- #marshal_dump ⇒ Object
- #marshal_load(data) ⇒ Object
- #populate(files) ⇒ Object
- #populate_from_file(file) ⇒ Object
- #populate_raw(raw) ⇒ Object
- #serialize(path) ⇒ Object
- #transform_accessor_opts(opts) ⇒ Object
- #write_dsl(xml, root_class_name, root_method, helpers = '', &block) ⇒ Object
- #write_dsl_helper(root, root_method, objs) ⇒ Object
Methods included from Utils
included, #name_to_sym, #new_roxml_class
Constructor Details
#initialize ⇒ DslRuntime
14 15 16 17 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 14 def initialize() @classes = {} @root_classes = Set.new end |
Class Method Details
.load(path) ⇒ Object
198 199 200 201 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 198 def self.load(path) file = File.open path, 'r' Marshal.load file end |
Instance Method Details
#add_class(name, clazz) ⇒ Object
73 74 75 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 73 def add_class(name, clazz) @classes[name] = clazz end |
#add_unprocessed_attr(attr_config, clazz) ⇒ Object
140 141 142 143 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 140 def add_unprocessed_attr(attr_config, clazz) @unprocessed_attrs ||= [] @unprocessed_attrs << AttrJob.new(attr_config, clazz) end |
#child_classes(hash, config) ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 183 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) ⇒ Object
99 100 101 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 99 def create_cleanroom(root_class, show_undefined_params = false) BaseCleanroom.new(root_class.new, self, show_undefined_params) end |
#deserialize_class(node, config) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 127 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 = []) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 77 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) ⇒ Object
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 84 def evaluate_raw(dsl, root_class, templates = [], &block) cleanroom = RootCleanroom.new(fetch(root_class).new, self) templates.each { |t| cleanroom.evaluate_file t } if block_given? cleanroom.evaluate &block else cleanroom.evaluate dsl end end |
#fetch(class_name) ⇒ Object
69 70 71 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 69 def fetch(class_name) @classes[class_name] end |
#fetch_cleanroom(root_class) ⇒ Object
95 96 97 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 95 def fetch_cleanroom(root_class) BaseCleanroom.new(fetch(root_class).new, self) end |
#hash_to_tree(hash, root_name, processed = []) ⇒ Object
173 174 175 176 177 178 179 180 181 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 173 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 ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 103 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) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 157 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) ⇒ Object
18 19 20 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 18 def populate(files) files.map {|f| populate_from_file f } end |
#populate_from_file(file) ⇒ Object
22 23 24 25 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 22 def populate_from_file (file) populate_raw File.read(file) end |
#populate_raw(raw) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 29 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) ⇒ Object
192 193 194 195 196 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 192 def serialize(path) file = File.new path, 'w' Marshal.dump self, file file.close end |
#transform_accessor_opts(opts) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 145 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 = '', &block) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 37 def write_dsl(xml, root_class_name, root_method, helpers = '', &block) roxml_root = fetch(root_class_name).from_xml xml 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) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/roundtrip_xml/dsl_runtime.rb', line 62 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 |