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, show_undefined_params = false) ⇒ BaseCleanroom

Returns a new instance of BaseCleanroom.



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
38
39
40
41
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 11

def initialize(el, runtime, show_undefined_params = false)
  @show_undefined_params = show_undefined_params
  @runtime = runtime
  @el = el
  get_el.attributes.each do |attr|
    method_name = attr.accessor.to_sym
    create_method(method_name) do |name = nil, &block|
      corrected_attr = get_el.attributes.select{|a| a.accessor == attr.accessor}[0]
      if !block.nil?
        clazz = name ? @runtime.fetch(name) : corrected_attr.sought_type
        value = expand(clazz, &block)
      elsif name
        value = name
      else
        return get_el.send(corrected_attr.accessor.to_sym)
      end

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

  expose_attr_accessors
end

Instance Attribute Details

#show_undefined_paramsObject (readonly)

Returns the value of attribute show_undefined_params.



10
11
12
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 10

def show_undefined_params
  @show_undefined_params
end

#value_holderObject (readonly)

Returns the value of attribute value_holder.



90
91
92
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 90

def value_holder
  @value_holder
end

Instance Method Details

#_matcher(accessor, matcher) ⇒ Object



106
107
108
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 106

def _matcher(accessor, matcher)
  @el.class.plain_accessor accessor, matcher
end

#_metadata(hash) ⇒ Object



61
62
63
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 61

def (hash)
  get_el. = get_el..merge hash
end

#append_child_modifications(hash) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 81

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



57
58
59
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 57

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

#expand(clazz, &block) ⇒ Object



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

def expand(clazz, &block)
  plain_accessors = @el.class.plain_accessors
  @value_holder ||= {}
  hash = plain_accessors.inject({}) {|h, a| h[a] = @el.send(a).nil? ?  Utils::UndefinedParam.new(a) : @el.send(a); h}
  child = @runtime.create_cleanroom(clazz, @show_undefined_params)
  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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 43

def expose_attr_accessors()
  get_el.class.plain_accessors.each do |a|
    create_method(a) do |value = nil|
      if !value.nil?
        get_el.send("#{a}=".to_sym, value)
      elsif value.nil? && show_undefined_params
        return get_el.send(a) || Utils::UndefinedParam.new(a)
      else
        return get_el.send(a)
      end
    end
    self.class.send(:expose, a)
  end
end

#get_elObject



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

def get_el
  @el
end

#inherit_properties(props) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 91

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