Skip to content

Instantly share code, notes, and snippets.

@godfat
Created December 8, 2015 10:02
Show Gist options
  • Save godfat/9aa0d693cba3a47aff6e to your computer and use it in GitHub Desktop.
Save godfat/9aa0d693cba3a47aff6e to your computer and use it in GitHub Desktop.
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