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
# 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|
      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

#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.



84
85
86
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 84

def value_holder
  @value_holder
end

Instance Method Details

#_matcher(accessor, matcher) ⇒ Object



100
101
102
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 100

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

#append_child_modifications(hash) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 75

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



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

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

#expand(clazz, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 60

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



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

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/roundtrip_xml/base_cleanroom.rb', line 85

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