Skip to content

Commit

Permalink
Merge pull request langalex#115 from thilo/performance_fixes
Browse files Browse the repository at this point in the history
Performance fixes
  • Loading branch information
langalex committed Jan 21, 2016
2 parents 9dde152 + d8531b5 commit 55ec97e
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions lib/couch_potato/persistence/simple_property.rb
Expand Up @@ -4,7 +4,7 @@ module PropertyMethods
private

def load_attribute_from_document(name)
if _document.has_key?(name.to_sym) || _document.has_key?(name.to_s)
if _document.has_key?(name)
property = self.class.properties.find{|property| property.name == name}
@skip_dirty_tracking = true
value = property.build(self, _document)
Expand All @@ -19,6 +19,7 @@ class SimpleProperty #:nodoc:

def initialize(owner_clazz, name, options = {})
self.name = name
@setter_name = "#{name}="
self.type = options[:type]
@type_caster = TypeCaster.new
owner_clazz.send :include, PropertyMethods unless owner_clazz.ancestors.include?(PropertyMethods)
Expand All @@ -27,8 +28,8 @@ def initialize(owner_clazz, name, options = {})
end

def build(object, json)
value = json[name.to_s].nil? ? json[name.to_sym] : json[name.to_s]
object.send "#{name}=", value
value = json[name]
object.send @setter_name, value
end

def dirty?(object)
Expand Down Expand Up @@ -56,10 +57,11 @@ def accessors_module_for(clazz)
end

def define_accessors(base, name, options)
ivar_name = "@#{name}".freeze
base.class_eval do
define_method "#{name}" do
load_attribute_from_document(name) unless instance_variable_defined?("@#{name}")
value = instance_variable_get("@#{name}")
define_method name do
load_attribute_from_document(name) unless instance_variable_defined?(ivar_name)
value = instance_variable_get(ivar_name)
if value.nil? && !options[:default].nil?
default = if options[:default].respond_to?(:call)
if options[:default].arity == 1
Expand All @@ -70,7 +72,7 @@ def define_accessors(base, name, options)
else
clone_attribute(options[:default])
end
self.instance_variable_set("@#{name}", default)
self.instance_variable_set(ivar_name, default)
default
else
value
Expand All @@ -80,7 +82,7 @@ def define_accessors(base, name, options)
define_method "#{name}=" do |value|
typecasted_value = type_caster.cast(value, options[:type])
send("#{name}_will_change!") unless @skip_dirty_tracking || typecasted_value == send(name)
self.instance_variable_set("@#{name}", typecasted_value)
self.instance_variable_set(ivar_name, typecasted_value)
end

define_method "#{name}?" do
Expand Down

0 comments on commit 55ec97e

Please sign in to comment.