Created
April 19, 2021 21:00
-
-
Save DNA/6f610683e5bf6e763c4795dfc2c9125d to your computer and use it in GitHub Desktop.
Testing hash refinement on jsonapi-serializable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
class SerializableReport < JSONAPI::Serializable::Resource | |
using Refinements::SerializableHash | |
id { @object['id'] } | |
attributes :foo, :bar | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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