Sha256: d89f1a529e232b2ff899d49374f9a9c2bcfb6e09c40c4f5f975673c0344f5c97

Contents?: true

Size: 1.22 KB

Versions: 8

Compression:

Stored size: 1.22 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)
      # 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

8 entries across 8 versions & 1 rubygems

Version Path
format_parser-0.9.0 lib/attributes_json.rb
format_parser-0.8.0 lib/attributes_json.rb
format_parser-0.7.0 lib/attributes_json.rb
format_parser-0.6.0 lib/attributes_json.rb
format_parser-0.5.2 lib/attributes_json.rb
format_parser-0.5.1 lib/attributes_json.rb
format_parser-0.5.0 lib/attributes_json.rb
format_parser-0.4.0 lib/attributes_json.rb