zoukankan      html  css  js  c++  java
  • ActiveRecord serialize json column

    # https://github.com/angelfan/DayDayUp/blob/master/railsdemo/json_column.rb
    #
    serialize :json_column, JsonColumn class JsonColumn attr_reader :title, :name def initialize(hash) @title = hash['title'] @name = hash['name'] end def hello "hello #{name}" end def dump(obj) MultiJson.dump(obj) end def load(json) return @default.call if json.nil? MultiJson.load(json) end # def self.dump(object) # object.as_json # end # # def self.load(hash) # self.new(hash.with_indifferent_access) if hash # end # def self.dump(object) # Hashie::Mash.new object.as_json # end # # def self.load(hash) # self.new(Hashie::Mash.new hash) if hash # end end # serialize :json_column_array, JsonColumnArray class JsonColumnArray def self.dump(object) object.as_json end def self.load(hash) JsonColumnArray.new(hash.with_indifferent_access) if hash end module ArraySerializer def self.dump(object) object.as_json end def self.load(array_of_hash) array_of_hash.map { |hash| JsonColumnArray.new(hash.with_indifferent_access) } if array_of_hash end end end module HashieMashSerializers class PGJSONSerializer include Singleton def dump(mash) mash end def load(hash) Hashie::Mash.new(hash) end end # 產生一個可以存到 pg json 欄位的 Hashie::Mash, 以推翻 pg text 與 OpenStruct 的獨裁霸權 # # 用法: # # serialize :image_meta, Hashie::Mash.pg_json_serializer def pg_json_serializer PGJSONSerializer.instance end end Hashie::Mash.extend(HashieMashSerializers)
  • 相关阅读:
    软件测试:Homework 3
    软件测试:Lab 1
    软件测试:Homework 2
    软件测试:Homework 1
    JAVA的回忆
    Java 操作符
    Java 笔录
    经典C#编程理解,概要,经典
    网络精灵
    签到计时
  • 原文地址:https://www.cnblogs.com/angelfan/p/5256498.html
Copyright © 2011-2022 走看看