Permalink
Browse files

Added build and create methods for setting child values

  • Loading branch information...
1 parent 65ad426 commit 73070eb7cb9f898d528c93821fc3d3476a70942d @wagenet committed Apr 5, 2009
Showing with 22 additions and 1 deletion.
  1. +12 −0 lib/health_vault/wc_data/child.rb
  2. +9 −1 lib/health_vault/wc_data/complex_type.rb
  3. +1 −0 rails/init.rb
@@ -78,6 +78,16 @@ def value=(val, force = false)
@value = val
end
+ # Make new instance of klass but don't associate
+ def build(attrs = {}, &block)
+ klass.new(attrs, &block)
+ end
+
+ # Make new instance of klass and associate
+ def create(attrs = {}, &block)
+ self.add_value(build(attrs, &block))
+ end
+
# Note, this does not prevent value from being modified directly
def add_value(val, force = false)
if singleton?
@@ -86,6 +96,7 @@ def add_value(val, force = false)
val = typecast_value(val) unless force
value << val
end
+ val
end
def remove_value(val)
@@ -94,6 +105,7 @@ def remove_value(val)
else
value.delete(val)
end
+ val
end
def to_s
@@ -74,6 +74,14 @@ def #{method_name}=(value, force = false)
def #{method_name}
@children['#{name}'].value
end
+
+ def build_#{method_name}(attrs = {}, &block)
+ @children['#{name}'].build(attrs, &block)
+ end
+
+ def create_#{method_name}(attrs = {}, &block)
+ @children['#{name}'].create(attrs, &block)
+ end
}, __FILE__, __LINE__)
add_child_method(method_name)
@@ -86,7 +94,7 @@ def define_child_multiple_accessors(name)
def add_#{method_name}(value, force = false)
@children['#{name}'].add_value(value, force)
end
-
+
def remove_#{method_name}(value)
@children['#{name}'].remove_value(value)
end
View
@@ -5,6 +5,7 @@
$LOAD_PATH << File.join(HEALTHVAULT_ROOT, 'lib')
$LOAD_PATH << File.join(HEALTHVAULT_ROOT, 'lib', 'generated')
+$LOAD_PATH.uniq!
Dependencies = ActiveSupport::Dependencies unless defined?(Dependencies)
Dependencies.load_paths << File.join(HEALTHVAULT_ROOT, 'lib')

0 comments on commit 73070eb

Please sign in to comment.