Class: BaseCleanroom

Inherits:
Object
  • Object
show all
Includes:
Cleanroom
Defined in:
lib/roundtrip_xml/base_cleanroom.rb

Overview

Generates cleanroom methods corresponding to all the xml_accessors and plain_accessors of @el

Direct Known Subclasses

RootCleanroom

Instance Method Summary collapse

Constructor Details

#initialize(el, runtime) ⇒ BaseCleanroom

Returns a new instance of BaseCleanroom.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 9

def initialize(el, runtime)
  @runtime = runtime
  @el = el
  get_el.attributes.each do |attr|
    method_name = attr.accessor.to_sym
    create_method(method_name) do |name = nil, &block|
      if !block.nil?
        clazz = name ? @runtime.fetch(name) : attr.sought_type
        value = expand(clazz, &block)
      elsif name
        value = name
      else
        return get_el.send(attr.accessor.to_sym)
      end

      if attr.array?
        array_attr = get_el.send(attr.accessor.to_sym)
        array_attr ||= attr.default
        array_attr << value
        get_el.send(attr.setter.to_sym, array_attr)
      else
        get_el.send(attr.setter.to_sym, value) unless get_el.send(attr.accessor.to_sym)
      end
    end
    self.class.send(:expose, method_name)
  end

  expose_attr_accessors
end

Instance Method Details

#create_method(name, &block) ⇒ Object



48
49
50
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 48

def create_method(name, &block)
  self.class.send(:define_method, name, &block)
end

#expand(clazz, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 52

def expand(clazz, &block)
  plain_accessors = @el.class.plain_accessors
  hash = {}
  @value_holder ||= {}
  if plain_accessors != 0
    hash = plain_accessors.inject({}) {|h, a| h[a] = @el.send(a); h}
  end
  child = @runtime.create_cleanroom(clazz)
  child.inherit_properties @value_holder.merge(hash)
  # child.inherit_properties(@args, @value_holder) if instance_variable_defined?(:@args)
  child.evaluate &block
  child.evaluate &child.get_el.process if child.get_el.respond_to? :process
  # evaluate the ROXML object's proc, which further modifies the element
  # child.evaluate &child.get_el.class.proc if child.respond_to? :proc
  child.get_el
end

#expose_attr_accessorsObject



39
40
41
42
43
44
45
46
47
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 39

def expose_attr_accessors()
  get_el.class.plain_accessors.each do |a|
    create_method(a) do |value = nil|
      return get_el.send(a) unless value
      get_el.send("#{a}=".to_sym, value) if value
    end
    self.class.send(:expose, a)
  end
end

#get_elObject



5
6
7
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 5

def get_el
  @el
end

#inherit_properties(props) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 69

def inherit_properties(props)
  @value_holder = props
  props.each do |name, val|
    # @value_holder.class.send(:attr_accessor, arg)
    # @value_holder.send("#{arg}=".to_sym, value_holder.send(arg))
    # create_method(arg) do
    #   @value_holder.send(arg)
    # end
    self.class.send(:attr_reader, name)
    self.instance_variable_set("@#{name}".to_sym, val)
    self.class.send(:expose, name)
  end
end