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 Attribute Summary collapse

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 Attribute Details

#value_holderObject (readonly)

Returns the value of attribute value_holder.



77
78
79
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 77

def value_holder
  @value_holder
end

Instance Method Details

#append_child_modifications(hash) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 68

def append_child_modifications(hash)
  hash.each do |k, v|
    setter = "#{k}="
    get_el.send(setter, v) if get_el.respond_to? setter
  end

  @value_holder.merge! hash
end

#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
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 52

def expand(clazz, &block)
  plain_accessors = @el.class.plain_accessors
  hash = {}
  @value_holder ||= {}
  hash = plain_accessors.inject({}) {|h, a| h[a] = @el.send(a); h}
  child = @runtime.create_cleanroom(clazz)
  child.inherit_properties hash.merge(@value_holder)

  child.evaluate &block
  new_values = child.value_holder
  append_child_modifications new_values
  child.get_el.process.each {|proc| child.evaluate &proc } if child.get_el.respond_to? :process

  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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 78

def inherit_properties(props)
  @value_holder = props
  props.each do |name, val|

    self.instance_variable_set("@#{name}", val)
    create_method name do |value = nil|
      # self.instance_variable_set("@#{name}", value) if value
      # instance_variable_get "@#{name}"
      @value_holder[name] = value if value
      @value_holder[name]
    end
    self.class.send(:expose, name)
  end
end