module Sequel::Plugins::EnumValues

Plugin for getting enum values from PostgreSQL by field name

Constants

VERSION

Public Class Methods

apply(model, _options = {}) click to toggle source

Initialize model state for this plugin @param model [Sequel::Model] model for which plugin applying @param _options [Hash] options (don't affect anything in this moment)

# File lib/sequel/plugins/enum_values.rb, line 12
def self.apply(model, _options = {})
        model.instance_exec do
                @enum_values_cache = {}
                @enum_values_caching = true
        end
end
configure(model, options = {}) click to toggle source

Configure model for this plugin @param model [Sequel::Model] model in which plugin enabled @param options [Hash] options for plugin @option options [Boolean] caching (true) cache enum values @option options [Boolean, Symbol, Array<Symbol>]

predicate_methods (false)
enum fields for which predicate methods will be defined

@example Disable caching

Item.plugin :enum_values, caching: false

@example Define predicate methods for all enum fields

Item.plugin :enum_values, predicate_methods: true

@example Define predicate methods for specific enum fields

Item.plugin :enum_values, predicate_methods: %[status type]

@example Define predicate methods for specific enum field

Item.plugin :enum_values, predicate_methods: :status
# File lib/sequel/plugins/enum_values.rb, line 34
def self.configure(model, options = {})
        model.instance_exec do
                @enum_values_caching = options.fetch(:caching, @enum_values_caching)

                predicate_methods = options.fetch(:predicate_methods, false)

                transform_predicate_methods_to_enum_fields(predicate_methods)
                        .each do |field|
                                all_enum_fields[field][:enum_values].each do |enum_value|
                                        define_predicate_method field, enum_value
                                end
                        end
        end
end