Skip to content

Commit

Permalink
Refactoring self.create parameters in a more intuitive way
Browse files Browse the repository at this point in the history
  • Loading branch information
serebryakov committed May 27, 2010
1 parent d9f1428 commit 37aaad3
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions lib/lib/model/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,11 @@ def #{name}=(value)
# successfully to the database or not.
#
# @param [Hash] attributes attributes of the new object
# @param [RDF::URI, String] id object's ID
#
# @return [Object] instance of RDFMapper::Model
# @return [nil] if save was unsuccessful
##
def create(attributes, id)
new(attributes).save(id)
def create(attributes)
new(attributes).save(attributes[:id])
end

##
Expand Down Expand Up @@ -310,15 +308,13 @@ def find(*args)
##
# Either finds or creates an object with the specified ID.
#
# @param [RDF::URI, String] id object's ID
# @param [Hash] attributes attributes of the new object
#
# @return [Object] instance of RDFMapper::Model
# @return [nil] if save was unsuccessful
##
def find_or_create(id, attributes = {})
instance = find(id)
instance.nil? ? create(id, attributes) : instance
def find_or_create(atts = {})
instance = atts[:id].nil? ? nil : find(atts[:id])
instance.nil? ? create(atts) : instance
end

##
Expand Down Expand Up @@ -490,7 +486,7 @@ def []=(name, value)
end
name.nil? ? nil : set_attribute(name, value)
end

##
# Returns a hash of all the attributes with their names as keys and
# the attributes' values as values.
Expand Down Expand Up @@ -537,7 +533,7 @@ def new?
def save(id = nil)
# Raise error if adapter is unspecified
check_for_adapter

if new? and id.nil?
raise RuntimeError, 'Save failed. ID must be specified'
end
Expand Down

0 comments on commit 37aaad3

Please sign in to comment.