Module: Oos4ruby::Bean::ClassMethods

Defined in:
lib/oos4ruby/bean.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#define_attr_reader(opts = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
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
79
80
81
82
83
84
85
86
87
88
# File 'lib/oos4ruby/bean.rb', line 36

def define_attr_reader(opts = {})
  if !opts.empty?
    opts.each_pair { |namespace, value|
      if value.is_a?Array
        value.each { |field|            
            define_method(field) do
              name = field.to_s.gsub(/_/, '')
              begin
                attr = @xml.attribute(name, namespace) if @xml.instance_of? REXML::Element
                return attr.value
              rescue
                #ATTRIBUTE NOT FOUND
              end                  
            end              
        }
      elsif value.is_a?Hash
        value.each_pair { |el, attr|
          define_method("#{el}_#{attr}") do
            el = el.to_s.gsub(/_/, '')
            
            prefix = 'atom'
            XmlNamespaces.each_pair { |name_key, name_value|
              prefix = name_key if name_value.to_s == namespace.to_s
            }
            
            elem = @xml.child(el, namespace) if @xml.instance_of?Oos4ruby::Entry
            elem = REXML::XPath.first(@xml, "./#{prefix}:#{el}", namespace) if @xml.instance_of?REXML::Element
              
            if (attr.is_a?Symbol)
              attr = attr.to_s.gsub(/_/, '')
              begin
                attr_value = elem.attribute(attr, namespace)
                return attr_value.value
              rescue
                #ATTRIBUTE NOT FOUND
              end
            elsif attr.is_a?Hash
              attr.each_pair { |attr_name, attr_namespace|
              attr_name = attr_name.to_s.gsub(/_/, '')
              begin
                attr_value = elem.attribute(attr_name, attr_namespace)
                return attr_value.value
              rescue
                #ATTRIBUTE NOT FOUND
              end
              }
            end
          end
        }
      end
    }
  end
end

#define_el_reader(opts = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/oos4ruby/bean.rb', line 9

def define_el_reader(opts = {})
  if !opts.empty?
    opts.each_pair { |key, value|
      value.each { |field_name|
        define_method(field_name) do            
          name = field_name.to_s.gsub(/_/, '')
          begin
            child = @xml.child(name, key) if @xml.instance_of?Oos4ruby::Entry
            child = REXML::XPath.first(@xml, name, key) if @xml.instance_of? REXML::Element
            return child.text if child
          rescue => e                
            puts e.backtrace.join("\n")
          end
        end
      }
    }
  end
end

#set_variable(options = {}) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/oos4ruby/bean.rb', line 28

def set_variable(options = {})
  unless options.empty?
    options.each_pair { |key, value|
      class_variable_set("@@#{key.to_s}".to_sym, value) 
    }
  end
end