Sha256: c5b59dd3683a4c5c7daa63cc5fc3935be52bb66bb5cdb740d510cb335abab5cb
Contents?: true
Size: 1.3 KB
Versions: 35
Compression:
Stored size: 1.3 KB
Contents
# frozen_string_literal: true # Creates accessors for json values module Sequel::Plugins::StoreAccessors module ClassMethods # Setup a store # # @param column [Symbol] jsonb column # @param fields [Array<Symbol>] keys in json, which will be accessors # @example # class User < Sequel::Model # store :data, :first_name # end # # user = User.create(first_name: "John") # user.first_name # => "John" # user.data # => {"first_name": "John"} def store(column, *fields) include_accessors_module fields.each do |field| define_store_getter(column, field) define_store_setter(column, field) end end private def include_accessors_module return if defined?(@_store_accessors_module) @_store_accessors_module = Module.new include @_store_accessors_module end def define_store_getter(column, field) @_store_accessors_module.module_eval do define_method(field) do send(column).to_h[field.to_s] end end end def define_store_setter(column, field) @_store_accessors_module.module_eval do define_method("#{field}=") do |value| send("#{column}=", send(column).to_h.merge(field.to_s => value)) end end end end end
Version data entries
35 entries across 35 versions & 1 rubygems