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

Version Path
umbrellio-sequel-plugins-0.10.0.101 lib/sequel/plugins/store_accessors.rb
umbrellio-sequel-plugins-0.10.0.86 lib/sequel/plugins/store_accessors.rb
umbrellio-sequel-plugins-0.9.0.79 lib/sequel/plugins/store_accessors.rb
umbrellio-sequel-plugins-0.9.0.76 lib/sequel/plugins/store_accessors.rb
umbrellio-sequel-plugins-0.8.0.73 lib/sequel/plugins/store_accessors.rb
umbrellio-sequel-plugins-0.7.0.52 lib/sequel/plugins/store_accessors.rb
umbrellio-sequel-plugins-0.6.0.46 lib/sequel/plugins/store_accessors.rb
umbrellio-sequel-plugins-0.6.0 lib/sequel/plugins/store_accessors.rb
umbrellio-sequel-plugins-0.6.0.36 lib/sequel/plugins/store_accessors.rb
umbrellio-sequel-plugins-0.5.1.27 lib/sequel/plugins/store_accessors.rb
umbrellio-sequel-plugins-0.5.1.26 lib/sequel/plugins/store_accessors.rb
umbrellio-sequel-plugins-0.5.1.25 lib/sequel/plugins/store_accessors.rb
umbrellio-sequel-plugins-0.5.1.18 lib/sequel/plugins/store_accessors.rb
umbrellio-sequel-plugins-0.5.1.17 lib/sequel/plugins/store_accessors.rb
umbrellio-sequel-plugins-0.5.1 lib/sequel/plugins/store_accessors.rb
umbrellio-sequel-plugins-0.5.0 lib/sequel/plugins/store_accessors.rb
umbrellio-sequel-plugins-0.4.0.164 lib/sequel/plugins/store_accessors.rb
umbrellio-sequel-plugins-0.4.0.152 lib/sequel/plugins/store_accessors.rb
umbrellio-sequel-plugins-0.4.0.145 lib/sequel/plugins/store_accessors.rb
umbrellio-sequel-plugins-0.4.0.134 lib/sequel/plugins/store_accessors.rb