lib/shamu/attributes.rb in shamu-0.0.3 vs lib/shamu/attributes.rb in shamu-0.0.4
- old
+ new
@@ -133,20 +133,25 @@
# @return [Hash] of attributes and their options defined on the class.
def attributes
@attributes ||= {}
end
+ # @return [Hash] of all association {.attributes} defined on the class.
+ def associations
+ attributes.select { |_, v| v[:association] }
+ end
+
def inherited( subclass )
# Clone the base class's attributes into the subclass
subclass.instance_variable_set :@attributes, attributes.dup
super
end
# Define a new attribute for the class.
#
- # @overload attribute(name, on:, default:, build: )
- # @overload attribute(name, build, on:, default:)
+ # @overload attribute(name, on:, default:, build:, &block )
+ # @overload attribute(name, build, on:, default:, &block)
#
# @param [Symbol] name of the attribute.
# @param [Symbol] as an alias of the attribute.
# @param [Symbol] on another method on the class to delegate the attribute
# to.
@@ -172,11 +177,22 @@
end
private :"fetch_#{ name }"
private :"assign_#{ name }"
-
self
+ end
+
+ # Define an {.attribute} that defines an association to another resource
+ # that also has it's own attributes.
+ #
+ # @param (see .attribute)
+ # @yieldreturn (see .attribute)
+ # @return [self]
+ def association( name, *args, **options, &block )
+ options[:association] = true
+
+ attribute( name, *args, **options, &block )
end
private
# @return [Array<Symbol>] keys used by the {.attribute} method options
\ No newline at end of file