Sha256: fb002579855955a410ac9aeeb1cf206c61605c29010d88a4035c355e13cdd3e4

Contents?: true

Size: 1.26 KB

Versions: 7

Compression:

Stored size: 1.26 KB

Contents

# Implements as_json as returning a Hash
# containing the return values of all the
# reader methods of an object that have
# associated pair writer methods.
#
#   class Foo
#     include AttributesJSON
#     attr_accessor :number_of_bars
#   end
#   the_foo = Foo.new
#   the_foo.number_of_bars = 42
#   the_foo.as_json #=> {:number_of_bars => 42}
module FormatParser::AttributesJSON

  # Implements a sane default `as_json` for an object
  # that accessors defined
  def as_json(root: false)
    h = {}
    h['nature'] = nature if respond_to?(:nature) # Needed for file info structs
    methods.grep(/\w\=$/).each_with_object(h) do |attr_writer_method_name, h|
      reader_method_name = attr_writer_method_name.to_s.gsub(/\=$/, '')
      value = public_send(reader_method_name)
      value = nil if value == Float::INFINITY
      # When calling as_json on our members there is no need to pass the root: option given to us
      # by the caller
      h[reader_method_name] = value.respond_to?(:as_json) ? value.as_json : value
    end
    if root
      {'format_parser_file_info' => h}
    else
      h
    end
  end

  # Implements to_json with sane defaults, with or without arguments
  def to_json(*maybe_generator_state)
    as_json(root: false).to_json(*maybe_generator_state)
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
format_parser-0.12.2 lib/attributes_json.rb
format_parser-0.12.1 lib/attributes_json.rb
format_parser-0.12.0 lib/attributes_json.rb
format_parser-0.11.0 lib/attributes_json.rb
format_parser-0.10.0 lib/attributes_json.rb
format_parser-0.9.4 lib/attributes_json.rb
format_parser-0.9.3 lib/attributes_json.rb