Skip to content

Commit

Permalink
optimise dynamic attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Giallombardo Nathan committed Aug 14, 2024
1 parent 4e83484 commit cead1a5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
11 changes: 7 additions & 4 deletions lib/couchbase-orm/attributes/dynamic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ def respond_to?(name, include_private = false)
#
# @return [ Object ] value of attribute
def _assign_attribute(name, value)
responds = name.reader == 'id' || respond_to?(name.writer)
setter = name.writer
responds = setter == 'id=' || respond_to?(setter)
if responds
public_send(name.writer, value)
public_send(setter, value)
else
type = define_attribute_type(value)
type = if type == :array
Expand Down Expand Up @@ -114,9 +115,11 @@ def define_dynamic_writer(name)
# @return [ Object ] The result of the method call.
def method_missing(name, *args)
attr = name.to_s
return super unless attr.reader != 'id' && has_attribute?(attr.reader)

getter = attr.reader

return super if getter == 'id'
return super if has_attribute?(getter)

if attr.writer?
define_dynamic_writer(getter)
@attributes.write_from_user(getter, args.first)
Expand Down
4 changes: 2 additions & 2 deletions lib/couchbase-orm/extensions/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ module CouchbaseOrm
module Extensions
module String
def reader
delete('=').sub(/_before_type_cast\z/, '')
delete_suffix('_before_type_cast').delete('=')
end

def writer
sub(/_before_type_cast\z/, '') + '='
delete_suffix('_before_type_cast') + '='
end

def writer?
Expand Down

0 comments on commit cead1a5

Please sign in to comment.