Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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