Skip to content

Commit

Permalink
Cleaned up the code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
estei committed Oct 21, 2011
1 parent c709a73 commit 2571791
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
21 changes: 9 additions & 12 deletions lib/couchrest/model/properties.rb
Expand Up @@ -120,7 +120,7 @@ def directly_set_attributes(hash, mass_assign = false)
end end
end end


assign_multiparameter_attributes(multi_parameter_attributes, hash) unless multi_parameter_attributes.length == 0 assign_multiparameter_attributes(multi_parameter_attributes, hash) unless multi_parameter_attributes.empty?
end end


def directly_set_read_only_attributes(hash) def directly_set_read_only_attributes(hash)
Expand All @@ -133,25 +133,22 @@ def directly_set_read_only_attributes(hash)
end end
end end


def assign_multiparameter_attributes(pairs, attrib_hash) def assign_multiparameter_attributes(pairs, hash)
execute_callstack_for_multiparameter_attributes( execute_callstack_for_multiparameter_attributes(
extract_callstack_for_multiparameter_attributes(pairs), extract_callstack_for_multiparameter_attributes(pairs), hash
attrib_hash )
)


end end
def execute_callstack_for_multiparameter_attributes(callstack, attrib_hash) def execute_callstack_for_multiparameter_attributes(callstack, hash)
callstack.each do |name, values_with_empty_parameters| callstack.each do |name, values_with_empty_parameters|
if self.respond_to?("#{name}=") if self.respond_to?("#{name}=")
s = send("#{name}=", values_with_empty_parameters) casted_attrib = send("#{name}=", values_with_empty_parameters)
unless s.is_a?(Hash) unless casted_attrib.is_a?(Hash)
attrib_hash.reject! do |key, value| hash.reject { |key, value| key.include?(name.to_s)}
key.include?(name.to_s)
end
end end
end end
end end
attrib_hash hash
end end


def extract_callstack_for_multiparameter_attributes(pairs) def extract_callstack_for_multiparameter_attributes(pairs)
Expand Down
9 changes: 2 additions & 7 deletions spec/unit/base_spec.rb
Expand Up @@ -116,17 +116,12 @@ def set_name; self.name = "foobar"; end
it "should still create a model if there are invalid attributes" do it "should still create a model if there are invalid attributes" do
@obj = WithDateAndTime.new @invalid_date_params @obj = WithDateAndTime.new @invalid_date_params
@obj.should_not be_nil @obj.should_not be_nil
@obj.should be_kind_of(WithDateAndTime)
end end
it "should not crash because of an empty value" do it "should not crash because of an empty value" do
@invalid_date_params["exec_date(2i)"] = "" @invalid_date_params["exec_date(2i)"] = ""
@obj = WithDateAndTime.new @invalid_date_params @obj = WithDateAndTime.new @invalid_date_params
@obj.exec_date.should_not be_nil @obj.should_not be_nil
@obj.exec_date.should_not be_kind_of(Date)
@obj.should be_kind_of(WithDateAndTime)
end
it "should not crash because of an illegal value" do
@obj = WithDateAndTime.new @invalid_date_params
@obj.exec_date.should_not be_nil
@obj.exec_date.should_not be_kind_of(Date) @obj.exec_date.should_not be_kind_of(Date)
@obj.should be_kind_of(WithDateAndTime) @obj.should be_kind_of(WithDateAndTime)
end end
Expand Down

0 comments on commit 2571791

Please sign in to comment.