Last active
August 29, 2015 14:24
-
-
Save groyoh/f4c5a25b2d600adff6fb to your computer and use it in GitHub Desktop.
Inline bundle + active_model_serializer
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
| #!/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