Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Commit

Permalink
Updated code to use Property#instance_variable_name when possible
Browse files Browse the repository at this point in the history
* Removed unecessary String#ensure_starts_with and
  String#ensure_ends_with
  • Loading branch information
Dan Kubb committed Apr 5, 2008
1 parent 6db24c7 commit bf99a21
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/data_mapper/adapters/data_objects_adapter.rb
Expand Up @@ -69,7 +69,7 @@ def create_with_returning?
# Methods dealing with a single instance object
def create(repository, instance)
properties = instance.dirty_attributes
values = properties.map { |property| instance.instance_variable_get(property.name.to_s.ensure_starts_with('@')) }
values = properties.map { |property| instance.instance_variable_get(property.instance_variable_name) }

sql = send(create_with_returning? ? :create_statement_with_returning : :create_statement, instance.class, properties)

Expand Down Expand Up @@ -117,7 +117,7 @@ def read(repository, resource, key)

def update(repository, instance)
properties = instance.dirty_attributes
values = properties.map { |property| instance.instance_variable_get(property.name.to_s.ensure_starts_with('@')) }
values = properties.map { |property| instance.instance_variable_get(property.instance_variable_name) }

sql = update_statement(instance.class, properties)
parameters = (values + instance.key)
Expand All @@ -138,7 +138,7 @@ def delete(repository, instance)
connection = create_connection
command = connection.create_command(delete_statement(instance.class))

key = instance.class.key(name).map { |property| instance.instance_variable_get(property.name.to_s.ensure_starts_with('@')) }
key = instance.class.key(name).map { |property| instance.instance_variable_get(property.instance_variable_name) }
affected_rows = command.execute_non_query(*key).to_i

close_connection(connection)
Expand Down
4 changes: 2 additions & 2 deletions lib/data_mapper/property.rb
Expand Up @@ -225,7 +225,7 @@ def initialize(target, name, type, options)

@instance_variable_name = "@#{@name}"

@getter = @type.is_a?(TrueClass) ? @name.to_s.ensure_ends_with('?').to_sym : @name
@getter = @type.is_a?(TrueClass) ? "#{@name}?".to_sym : @name


# if it has a lazy key it is lazy. :lazy is now an array of contexts not bool
Expand Down Expand Up @@ -280,7 +280,7 @@ def #{name}
if type == TrueClass
@target.class_eval <<-EOS, __FILE__, __LINE__
#{reader_visibility}
alias #{name.to_s.ensure_ends_with('?')} #{name}
alias #{name}? #{name}
EOS
end
rescue SyntaxError
Expand Down
9 changes: 5 additions & 4 deletions lib/data_mapper/resource.rb
Expand Up @@ -37,10 +37,10 @@ def self.dependencies
attr_accessor :loaded_set

def [](name)
ivar_name = "@#{name}"
property = self.class.properties(repository.name)[name]
ivar_name = property.instance_variable_name

unless new_record? || attribute_loaded?(name)
unless new_record? || instance_variable_defined?(ivar_name)
lazy_load(name)
end

Expand All @@ -49,8 +49,8 @@ def [](name)
end

def []=(name, value)
ivar_name = "@#{name}"
property = self.class.properties(repository.name)[name]
ivar_name = property.instance_variable_name

if property && property.lock?
instance_variable_set("@shadow_#{name}", instance_variable_get(ivar_name))
Expand Down Expand Up @@ -98,7 +98,8 @@ def destroy
end

def attribute_loaded?(name)
instance_variable_defined?("@#{name}")
property = self.class.properties(repository.name)[name]
instance_variable_defined?(property.instance_variable_name)
end

def dirty_attributes
Expand Down
2 changes: 1 addition & 1 deletion spec/resource_spec.rb
Expand Up @@ -100,7 +100,7 @@ class Planet
# This means #attribute_loaded?'s implementation could be very
# similar (if not identical) to:
# def attribute_loaded?(name)
# instance_variables.include?(name.to_s.ensure_starts_with('@'))
# instance_variable_defined?("@#{name}")
# end
mars.attribute_loaded?(:name).should be_true
mars.attribute_dirty?(:name).should be_true
Expand Down

0 comments on commit bf99a21

Please sign in to comment.