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)
  • 相关阅读:
    用react的ReactCSSTransitionGroup插件实现简单的弹幕动画
    composer安装yii2问题总结
    记阿里笔试经历
    JVM, JRE,JDK 的区别
    HTML
    Http协议
    操作系统和网络基础知识
    网络基础之网络协议
    计算机硬件知识
    计算机硬件历史
  • 原文地址:https://www.cnblogs.com/angelfan/p/5256498.html
Copyright © 2011-2022 走看看