lib/fear/struct.rb in fear-1.1.0 vs lib/fear/struct.rb in fear-1.2.0
- old
+ new
@@ -190,10 +190,11 @@
def ==(other)
other.is_a?(other.class) && to_h == other.to_h
end
INSPECT_TEMPLATE = "<#Fear::Struct %{class_name} %{attributes}>"
+ private_constant :INSPECT_TEMPLATE
# @return [String]
#
# @example
# User = Fear::Struct.with_attributes(:id, :email)
@@ -206,20 +207,22 @@
format(INSPECT_TEMPLATE, class_name: self.class.name, attributes: attributes)
end
alias to_s inspect
MISSING_KEYWORDS_ERROR = "missing keywords: %{keywords}"
+ private_constant :MISSING_KEYWORDS_ERROR
private def _check_missing_attributes!(provided_attributes)
missing_attributes = members - provided_attributes.keys
unless missing_attributes.empty?
raise ArgumentError, format(MISSING_KEYWORDS_ERROR, keywords: missing_attributes.join(", "))
end
end
UNKNOWN_KEYWORDS_ERROR = "unknown keywords: %{keywords}"
+ private_constant :UNKNOWN_KEYWORDS_ERROR
private def _check_unknown_attributes!(provided_attributes)
unknown_attributes = provided_attributes.keys - members
unless unknown_attributes.empty?
@@ -228,8 +231,18 @@
end
# @return [void]
private def _set_attribute(name, value)
instance_variable_set(:"@#{name}", value)
+ end
+
+ # @param keys [Hash, nil]
+ # @return [Hash]
+ def deconstruct_keys(keys)
+ if keys
+ to_h.slice(*(self.class.attributes & keys))
+ else
+ to_h
+ end
end
end
end