lib/shamu/attributes.rb in shamu-0.0.21 vs lib/shamu/attributes.rb in shamu-0.0.24
- old
+ new
@@ -74,9 +74,35 @@
# @return [Boolean] true if the attribute has been set.
def set?( attribute )
instance_variable_defined? :"@#{ attribute }"
end
+ # @return [Hash] the assigned attributes as a hash for JSON serialization.
+ def as_json( options = {} ) # rubocop:disable Styles/OptionHash
+ to_attributes( options.slice( :only, :except ) ).as_json
+ end
+
+ # @return [String] JSON encoded version of the assigned attributes.
+ def to_json( options = {} ) # rubocop:disable Styles/OptionHash
+ as_json( options ).to_json
+ end
+
+ def pretty_print( pp )
+ attributes = to_attributes
+
+ pp.object_address_group( self ) do
+ pp.seplist( attributes.keys, -> { pp.text "," } ) do |name|
+ pp.breakable " "
+ pp.group( 1 ) do
+ pp.text name.to_s
+ pp.text ":"
+ pp.breakable " "
+ pp.pp attributes[ name ]
+ end
+ end
+ end
+ end
+
private
def match_attribute?( pattern, name )
Array( pattern ).any? do |matcher|
matcher === name