Skip to content

Instantly share code, notes, and snippets.

@DNA
Created April 19, 2021 21:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DNA/6f610683e5bf6e763c4795dfc2c9125d to your computer and use it in GitHub Desktop.
Save DNA/6f610683e5bf6e763c4795dfc2c9125d to your computer and use it in GitHub Desktop.
Testing hash refinement on jsonapi-serializable
# frozen_string_literal: true
class SerializableReport < JSONAPI::Serializable::Resource
using Refinements::SerializableHash
id { @object['id'] }
attributes :foo, :bar
end
# frozen_string_literal: true
module Refinements
module SerializableHash
refine JSONAPI::Serializable::Resource::DSL do
def attributes(*attributes)
attributes.each { |name| attribute(name) }
end
def attribute(name)
attribute_blocks[name.to_sym] = lookup_data(name)
end
def lookup_data(name)
proc do
@object[name.to_s] ||
@object[name.to_sym] ||
@object.public_send(name.to_sym)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment