Skip to content

Instantly share code, notes, and snippets.

@groyoh
Last active August 29, 2015 14:24
Show Gist options
  • Save groyoh/f4c5a25b2d600adff6fb to your computer and use it in GitHub Desktop.
Save groyoh/f4c5a25b2d600adff6fb to your computer and use it in GitHub Desktop.
Inline bundle + active_model_serializer
#!/usr/bin/env ruby
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'rails'
gem 'active_model_serializers', github: "runtastic/active_model_serializers"
end
class Model
alias :read_attribute_for_serialization :send
def initialize(hash = {})
@attributes = hash
end
def method_missing(method, *args)
if @attributes.key? method
@attributes[method]
else
raise NoMethodError
end
end
end
Organizer = Class.new(Model)
class OrganizerSerializer < ActiveModel::Serializer
attributes :id, :name
end
object = Organizer.new(id: 1, name: "name")
serializer = ActiveModel::Serializer.serializer_for(object).new(object)
adapter = ActiveModel::Serializer::Adapter.create(serializer)
puts adapter.as_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment