acdc 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/acdc.rb +1 -1
  2. data/lib/acdc/body.rb +32 -14
  3. metadata +2 -2
@@ -9,7 +9,7 @@ require File.join(File.dirname(__FILE__),"acdc","body")
9
9
 
10
10
  module AcDc
11
11
 
12
- VERSION = [0,2,2] unless defined?(AcDc::VERSION)
12
+ VERSION = [0,2,3] unless defined?(AcDc::VERSION)
13
13
 
14
14
  if defined?(JAIL_BREAK)
15
15
  Element.class_eval{ alias :to_s :acdc }
@@ -33,17 +33,11 @@ module AcDc
33
33
  values.each do |key,val|
34
34
  if self.class.declared_elements.has_key?(key)
35
35
  type = options_for(key)[:type]
36
- if type
37
- if val.respond_to?(:each)
38
- val.each {|v| raise ArgumentError.new("Type is invalid") unless v.is_a?(type)}
39
- else
40
- raise ArgumentError.new("Type is invalid") unless val.is_a?(type)
41
- end
42
- if type.ancestors.include?(Body)
43
- elements.update(key => type.new(val.to_hash.merge(:sequence => options_for(key)[:sequence])))
44
- else
45
- elements.update(key => type.new(val, options_for(key)))
46
- end
36
+ validate(type,key,val)
37
+ if type and type.ancestors.include?(Body)
38
+ elements.update(key => type.new(val.to_hash.merge(:sequence => options_for(key)[:sequence])))
39
+ elsif type
40
+ elements.update(key => type.new(val, options_for(key)))
47
41
  else
48
42
  elements.update(key => Element(val, options_for(key)))
49
43
  end
@@ -97,7 +91,7 @@ module AcDc
97
91
  # Sequence of elements (XML Schema sequence) is taken care of by the order
98
92
  # the elements are declared in the class definition.
99
93
  #@param [Symbol] name A name to assign the Element (tag name)
100
- #@param [Class] type A type to use for the element (enforcement)
94
+ #@param [Class] type A type to use for the element (use this for type enforcement)
101
95
  #@option options [Boolean] :single False if object is a collection
102
96
  #@option options [String] :tag String determining the name to use in the XML tag
103
97
  def element(*options)
@@ -165,13 +159,30 @@ module AcDc
165
159
 
166
160
  private
167
161
 
162
+ def validate(type, key, val)
163
+ if type
164
+ if val.respond_to?(:each)
165
+ val.each {|v| raise ArgumentError.new("#{val.class} type is invalid. #{self.class} #{key} requires #{type}.") unless v.is_a?(type)}
166
+ else
167
+ unless val.is_a?(type)
168
+ raise ArgumentError.new("#{val.class} type is invalid. #{self.class} #{key} requires #{type}.")
169
+ end
170
+ end
171
+ end
172
+ end
173
+
168
174
  def options_for(key)
169
175
  self.class.declared_elements[key]
170
176
  end
171
177
 
172
178
  def read(method_id)
173
179
  if elements.has_key?(method_id)
174
- elements[method_id].value
180
+ obj = elements[method_id]
181
+ if obj.is_a?(Body)
182
+ elements[method_id]
183
+ else
184
+ elements[method_id].value
185
+ end
175
186
  else
176
187
  attributes[method_id].value
177
188
  end
@@ -180,7 +191,14 @@ module AcDc
180
191
  def write(method_id, *args, &block)
181
192
  key = method_id.to_s.gsub(/\=$/,"").to_sym
182
193
  if elements.has_key?(key)
183
- elements.update(key => Element(args.first, options_for(key)))
194
+ type = options_for(key)[:type]
195
+ if type and type.ancestors.include?(Body)
196
+ elements.update(key => type.new(args.first.to_hash, options_for(key)))
197
+ elsif type
198
+ elements.update(key => type.new(args.first, options_for(key)))
199
+ else
200
+ elements.update(key => Element(args.first, options_for(key)))
201
+ end
184
202
  else
185
203
  attributes.update(key => Attribute(key,args.first))
186
204
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acdc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Clint Hill
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-02 00:00:00 -07:00
12
+ date: 2009-10-03 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency