Skip to content
Browse files

Implementing `properties` method in addition to `attributes`

  • Loading branch information...
1 parent 81eb4c8 commit 6eb1bf07752dd6d754956022a4fc02e01b6e6a4d @serebryakov serebryakov committed
Showing with 24 additions and 4 deletions.
  1. +13 −1 lib/lib/model/base.rb
  2. +11 −3 lib/lib/scope/model.rb
View
14 lib/lib/model/base.rb
@@ -486,7 +486,19 @@ def []=(name, value)
end
name.nil? ? nil : set_attribute(name, value)
end
-
+
+ ##
+ # Returns a hash of all the properties (i.e. attributes without
+ # associations).
+ #
+ # @return [Hash] all properties of an instance (name => value)
+ ##
+ def properties(*args)
+ Hash[self.class.properties.keys.map do |name|
+ [ name, @attributes[name] ]
+ end].merge(@arbitrary)
+ end
+
##
# Returns a hash of all the attributes with their names as keys and
# the attributes' values as values.
View
14 lib/lib/scope/model.rb
@@ -50,9 +50,17 @@ def [](name)
end
##
- # Returns a hash of all the attributes with their names as keys and
- # the attributes' values as values. If model is unloaded, it will
- # preload it before returning its attributes.
+ # In addition to the original method, preloads the model.
+ #
+ # @return [Hash] all attributes of an instance (name => value)
+ ##
+ def properties
+ check_for_nil_error
+ @loaded ? super : load.properties
+ end
+
+ ##
+ # In addition to the original method, preloads the model.
#
# @return [Hash] all attributes of an instance (name => value)
##

0 comments on commit 6eb1bf0

Please sign in to comment.
Something went wrong with that request. Please try again.