Class: SexpDslBuilder
- Inherits:
-
Object
- Object
- SexpDslBuilder
- Defined in:
- lib/roundtrip_xml/sexp_dsl_builder.rb
Instance Attribute Summary collapse
-
#roxml_objs ⇒ Object
Returns the value of attribute roxml_objs.
-
#runtime ⇒ Object
Returns the value of attribute runtime.
Instance Method Summary collapse
- #create_hash_sexp(hash) ⇒ Object
- #create_sexp_for_roxml_obj(obj, root_method = nil) ⇒ Object
- #exp ⇒ Object
-
#initialize(roxml_obj, runtime) ⇒ SexpDslBuilder
constructor
A new instance of SexpDslBuilder.
- #write_full_dsl(root_method) ⇒ Object
- #write_roxml_obj(obj) ⇒ Object
Constructor Details
#initialize(roxml_obj, runtime) ⇒ SexpDslBuilder
Returns a new instance of SexpDslBuilder.
34 35 36 37 38 |
# File 'lib/roundtrip_xml/sexp_dsl_builder.rb', line 34 def initialize(roxml_obj, runtime) @roxml_obj = roxml_obj self.runtime = runtime @parser = RubyParser.new end |
Instance Attribute Details
#roxml_objs ⇒ Object
Returns the value of attribute roxml_objs.
33 34 35 |
# File 'lib/roundtrip_xml/sexp_dsl_builder.rb', line 33 def roxml_objs @roxml_objs end |
#runtime ⇒ Object
Returns the value of attribute runtime.
33 34 35 |
# File 'lib/roundtrip_xml/sexp_dsl_builder.rb', line 33 def runtime @runtime end |
Instance Method Details
#create_hash_sexp(hash) ⇒ Object
40 41 42 |
# File 'lib/roundtrip_xml/sexp_dsl_builder.rb', line 40 def create_hash_sexp(hash) @parser.process(hash.to_s).to_a end |
#create_sexp_for_roxml_obj(obj, root_method = nil) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/roundtrip_xml/sexp_dsl_builder.rb', line 44 def create_sexp_for_roxml_obj(obj, root_method = nil) is_subclass = obj.class.subclass? subclass_value = is_subclass ? [:lit, obj.class.class_name] : nil accessors = [] # metadata if root_method && obj. && !obj..empty? accessors << [:call, nil, :_metadata, create_hash_sexp(obj.)] end obj.attributes.each do |attr| val = obj.send attr.accessor next unless val if attr.sought_type.class == Symbol if val.is_a? Array val.each {|v| accessors << [:call, nil, attr.accessor, [:str, v]]} else accessors << [:call, nil, attr.accessor, [:str, val]] end elsif val.class == Array val.each { |v| accessors << create_sexp_for_roxml_obj(v, attr.accessor) } else accessors << create_sexp_for_roxml_obj(val, attr.accessor) if val end end.compact # plain accessors obj.class.plain_accessors.each do |a| val = obj.send a next unless val accessors << [:call, nil, a, [:str, val]] end root_call = [:call, nil, root_method] root_call << subclass_value if subclass_value if root_method [:iter, root_call, 0, [:block, *accessors] ] else [:block, *accessors] end end |
#exp ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/roundtrip_xml/sexp_dsl_builder.rb', line 8 def exp rub = <<EOF bar 'adf' foo do a "a" do a1 1 a2 2 end a "a" do a1 3 a2 4 end b "b" c "c" end EOF # s = Sexp.from_array arr # processor = Ruby2Ruby.new # processor.process(s) parser = RubyParser.new s = parser.process(rub) s end |
#write_full_dsl(root_method) ⇒ Object
96 97 98 |
# File 'lib/roundtrip_xml/sexp_dsl_builder.rb', line 96 def write_full_dsl(root_method) write_roxml_obj @roxml_obj end |
#write_roxml_obj(obj) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/roundtrip_xml/sexp_dsl_builder.rb', line 87 def write_roxml_obj(obj) s = create_sexp_for_roxml_obj obj sexp = Sexp.from_array s processor = Ruby2Ruby.new str = processor.process(sexp) str.gsub(/([^"])\(([^\(\)]*|".*")\)([^"])([^{]|$)/, '\\1 \\2\\3\\4').gsub(/"([^'\n]+)"/, "'\\1'").gsub(/# do nothing/, '') end |