Created
December 8, 2015 10:02
-
-
Save godfat/9aa0d693cba3a47aff6e to your computer and use it in GitHub Desktop.
This file contains 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
require 'representable/json' | |
Representable.deprecations = false | |
class UserRepr < Representable::Decorator | |
include Representable::JSON | |
property :name | |
end | |
class UserFirstNameRepr < UserRepr | |
property :first_name, getter: ->(_){ name[/\S+/] } | |
end | |
class User < Struct.new(:name) | |
end | |
user = User.new('Iron Fist') | |
puts UserRepr.new(user).to_json # {"name":"Iron Fist"} | |
puts UserFirstNameRepr.new(user).to_json # {"name":"Iron Fist","first_name":"Iron"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment