Sha256: 026b9cf63d22cada686b51e3391505cecd3a09bbb611b34dea25c74a9241cce6

Contents?: true

Size: 956 Bytes

Versions: 1

Compression:

Stored size: 956 Bytes

Contents

$LOAD_PATH.unshift '../lib'
require 'memory_record'

if true
  require "active_model"

  class Person
    include ActiveModel::Model
    include ActiveModel::Serializers::JSON

    attr_accessor :a

    def attributes
      {'a' => a}
    end
  end
end

class ColorInfo
  include MemoryRecord
  memory_record [
    { key: :blue, rgb: [  0, 0, 255], a: 1},
    { key: :red,  rgb: [255, 0,   0], a: 2},
  ]

  def hex
    "#" + rgb.collect { |e| "%02X" % e }.join
  end

  def children
    [
      {a: 1, b: 2},
      Person.new(a: 1),
      ColorInfo[:blue],
    ]
  end
end

ColorInfo.first.as_json(only: :key)                                 # => {:key=>:blue}
ColorInfo.first.as_json(except: [:rgb, :code, :a])                  # => {:key=>:blue}
ColorInfo.first.as_json(only: [], methods: :hex)                    # => {:hex=>"#0000FF"}
ColorInfo.first.as_json(only: [], include: {children: {only: :a}} ) # => {:children=>[{"a"=>1}, {"a"=>1}, {:a=>1}]}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
memory_record-0.0.13 examples/0320_as_json.rb