Class: Sepa::Base

Inherits:
Object
  • Object
show all
Includes:
Aduki::Initializer
Defined in:
lib/sepa/base.rb

Constant Summary collapse

@@attribute_defs =
Hash.new { |h,k| h[k] = { } }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.array_attribute(name, tag, member_type = nil, options = { }) ⇒ Object



76
77
78
79
80
# File 'lib/sepa/base.rb', line 76

def self.array_attribute name, tag, member_type=nil, options={ }
  attribute_defs[name] = { :tag => tag, :type => :[], :member_type => member_type, :options => options }
  attr_accessor name
  aduki(name => member_type) if member_type
end

.attribute(name, tag, type = :string, member_type = nil, options = { }) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/sepa/base.rb', line 59

def self.attribute name, tag, type=:string, member_type=nil, options={ }
  if type == :[]
    array_attribute name, tag, member_type, options
  elsif type.is_a?(Class) && type != Time && type != Date
    typed_attribute name, tag, type, options
  else
    attr_accessor name
    attribute_defs[name] = { :tag => tag, :type => type, :options => options }
  end
end

.attribute_defsObject



43
44
45
# File 'lib/sepa/base.rb', line 43

def self.attribute_defs
  @@attribute_defs[self]
end

.attribute_defs=(arg) ⇒ Object



47
48
49
# File 'lib/sepa/base.rb', line 47

def self.attribute_defs= arg
  @@attribute_defs[self]= arg
end

.code_or_proprietaryObject



54
55
56
57
# File 'lib/sepa/base.rb', line 54

def self.code_or_proprietary
  attribute :code       , "Cd"
  attribute :proprietary, "Prtry"
end

.definition(txt) ⇒ Object



51
52
# File 'lib/sepa/base.rb', line 51

def self.definition txt
end

.typed_attribute(name, tag, type, options) ⇒ Object



70
71
72
73
74
# File 'lib/sepa/base.rb', line 70

def self.typed_attribute name, tag, type, options
  attribute_defs[name] = { :tag => tag, :type => type, :options => options }
  attr_accessor name
  aduki name => type
end

Instance Method Details

#build_xml_attributes(names) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/sepa/base.rb', line 7

def build_xml_attributes names
  result = { }
  (names || { }).each { |k,v|
    result[k] = self.send v
  }
  result
end

#to_xml(builder) ⇒ Object



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/sepa/base.rb', line 15

def to_xml builder
  self.class.attribute_defs.each do |name, meta|
    item = self.send(name)
    options = meta[:options] || { }
    attributes = build_xml_attributes options[:attributes]
    next if item == nil
    if meta[:type] == :string
      builder.__send__(meta[:tag], item, attributes)
    elsif meta[:type] == :[]
      if meta[:member_type] == nil
        item.each { |obj| builder.__send__(meta[:tag], obj, attributes) }
      else
        item.each do |obj|
          builder.__send__(meta[:tag], attributes) { obj.to_xml builder }
        end
      end
    elsif meta[:type] == Time
      v = item.is_a?(String) ? item : item.strftime("%Y-%m-%dT%H:%M:%S")
      builder.__send__(meta[:tag], v, attributes)
    elsif meta[:type] == Date
      v = item.is_a?(String) ? item : item.strftime("%Y-%m-%d")
      builder.__send__(meta[:tag], v, attributes)
    elsif meta[:type].is_a? Class
      builder.__send__(meta[:tag], attributes) { item.to_xml builder }
    end
  end
end