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)
  • 相关阅读:
    批量修改同一种控件属性
    线程池的使用
    金蝶K3常用数据表
    安装更新
    ApexSQL
    c# 计算一个整型数组的平均
    sqlServer基础知识
    c# 获取字符串数组中最长的的字符串并输出最长的字符串
    c# 获取数组中最大数的值
    c# 计算1-100之间的所有质数(素数)的和
  • 原文地址:https://www.cnblogs.com/angelfan/p/5256498.html
Copyright © 2011-2022 走看看