Class: Xampl::RubyPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/xamplr/to-ruby.rb

Instance Method Summary collapse

Constructor Details

#initialize(mentions = nil) ⇒ RubyPrinter

Returns a new instance of RubyPrinter.



8
9
10
11
12
13
# File 'lib/xamplr/to-ruby.rb', line 8

def initialize(mentions=nil)
  @obj_count = 0
  @map = {}
  @lookup_map={}
  @mentions = mentions
end

Instance Method Details

#show_attributes(thing, name, depth) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/xamplr/to-ruby.rb', line 15

def show_attributes(thing, name, depth)
  return "" unless thing.attributes

  out = ""
  indent = "    " + ("  " * depth)

  if thing.persist_required and (1 < depth)
    accessor = thing.indexed_by
    value = thing.get_the_index
    if value then
      out << indent << "#{name}.#{accessor} = #{value.inspect}\n"
    end
  else
    thing.attributes.each do |attribute|
      value = thing.instance_variable_get(attribute[0])

      if value then
        if value.kind_of?(XamplObject) then
          vname = "root_#{@obj_count += 1}"
          out << to_ruby_as_attr(value, depth, vname)
          out << indent << "#{name}.instance_variable_set(:#{attribute[0]}, #{vname})\n"
        else
          out << indent << "#{name}.instance_variable_set(:#{attribute[0]}, #{value.inspect})\n"
        end
      end
    end
  end
  return out
end

#show_attributes_flat(thing, depth) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/xamplr/to-ruby.rb', line 80

def show_attributes_flat(thing, depth)
  return "" unless thing.attributes

  out = ""

  if thing.persist_required and (1 < depth)
    accessor = thing.indexed_by

    value = thing.get_the_index
    if value then
      out << "      " << "xampl.#{accessor} = #{value.inspect}\n"
    end
  else
    thing.attributes.each do |attribute|
      value = thing.instance_variable_get(attribute[0])

      if value then
        out << "      " << "xampl.instance_variable_set(:#{attribute[0]}, #{value.inspect})\n"
      end
    end
  end
  return out
end

#show_children_flat(thing, name, depth) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/xamplr/to-ruby.rb', line 104

def show_children_flat(thing, name, depth)
  out = ""

  thing.children.each do |child|
    if child.kind_of? XamplObject and (nil == @map[child]) then
      cname = "v_#{child.safe_name}_#{@obj_count += 1}"

      @map[child] = cname

      cout = ""
      cout << show_attributes_flat(child, 1 + depth)

      if 0 < cout.size then
        if child.persist_required then
          out << "    " << "#{cname} = #{child.class.to_s}['#{child.get_the_index}']\n"
          @mentions << child if @mentions
        else
          out << "    " << "#{cname} = #{child.class.to_s}.new { | xampl |\n"
          out << cout
          out << "    " << "}\n"
        end
      else
        out << "    " << "#{cname} = #{child.class.to_s}.new\n"
      end
      out << show_children_flat(child, cname, 1 + depth) unless child.persist_required
    end
  end
  return out
end

#show_children_stitch(thing, depth) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/xamplr/to-ruby.rb', line 139

def show_children_stitch(thing, depth)
  out = ""

  return out if thing.persist_required and (0 < depth)

  name = @map[thing]
  if name then
    @map.delete(thing)
  else
    return out
  end

  if (!thing.kind_of? XamplWithMixedContent) and
          (!thing.kind_of? XamplWithoutContent) and
          thing._content then
    out << "    " << "#{name} << #{thing._content.inspect}\n"
  end
  thing.children.each do |child|
    if child.kind_of? XamplObject then
      out << "    " << "#{name} << #{@lookup_map[child]}\n"
    else
      out << "    " << "#{name} << #{child.inspect}\n"
    end
  end
  thing.children.each do |child|
    if child.kind_of? XamplObject then
      out << show_children_stitch(child, 1 + depth)
    end
  end
  return out
end

#show_children_stitch_start(thing, depth) ⇒ Object



134
135
136
137
# File 'lib/xamplr/to-ruby.rb', line 134

def show_children_stitch_start(thing, depth)
  @lookup_map.merge!(@map)
  return show_children_stitch(thing, depth)
end

#show_children_tree(thing, name, depth) ⇒ Object



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
# File 'lib/xamplr/to-ruby.rb', line 45

def show_children_tree(thing, name, depth)
  out = ""
  indent = "    " + ("  " * depth)

  if thing.persist_required and (1 < depth) then
    return out << indent << "#{name}.load_needed = true\n"
  end

  if (!thing.kind_of? XamplWithMixedContent) and
          (!thing.kind_of? XamplWithoutContent) and
          thing._content then
    out << indent << "#{name} << #{thing._content.inspect}\n"
  end
  thing.children.each do |child|
    if child.kind_of? XamplObject then
      cname = "v_#{child.safe_name}_#{@obj_count += 1}"

      cout = ""
      cout << show_attributes(child, cname, 1 + depth)
      cout << show_children_tree(child, cname, 1 + depth)

      if 0 < cout.size then
        out << indent << "#{name} << #{child.class.to_s}.new { | #{cname} |\n"
        out << cout
        out << indent << "}\n"
      else
        out << indent << "#{name} << #{child.class.to_s}.new\n"
      end
    else
      out << indent << "#{name} << #{child.inspect}\n"
    end
  end
  return out
end

#to_ruby(thing, depth = 0, name = "root") ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/xamplr/to-ruby.rb', line 171

def to_ruby(thing, depth=0, name="root")
  thing.accessed

  @obj_count = 0
  @map = {}
  @lookup_map={}

  @map[thing] = name

  if $USE_A_PROC then
    return %Q{
module XamplRubyDefinition
  @@proc = Proc.new { | target |
#{name} = target ? target : #{thing.class.to_s}.new
#{show_attributes(thing, name, depth)}
    #{show_children_flat(thing, name, depth)}
    #{show_children_stitch_start(thing, depth)}
    #{name}
  }
end
}
  else
    return %Q{
module XamplRubyDefinition
  def XamplRubyDefinition.build_it(target)
#{name} = target ? target : #{thing.class.to_s}.new
#{show_attributes(thing, name, depth)}
    #{show_children_flat(thing, name, depth)}
    #{show_children_stitch_start(thing, depth)}
    #{name}
  end
end
}
  end
end

#to_ruby_as_attr(thing, depth, name) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/xamplr/to-ruby.rb', line 207

def to_ruby_as_attr(thing, depth, name)
  thing.accessed

  @map[thing] = name

  return %Q{
  #{name} = #{thing.class.name}.new
#{show_attributes(thing, name, depth)}
  #{show_children_flat(thing, name, depth)}
  #{show_children_stitch_start(thing, depth)}
  }
end