Module: Sequel::Plugins::EnumValues

Defined in:
lib/sequel/plugins/enum_values.rb

Overview

Plugin for getting enum values from PostgreSQL by field name

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
'1.2.1'

Class Method Summary collapse

Class Method Details

.apply(model, _options = {}) ⇒ Object

Initialize model state for this plugin

Parameters:

  • model (Sequel::Model)

    model for which plugin applying

  • _options (Hash) (defaults to: {})

    options (don’t affect anything in this moment)



12
13
14
15
16
17
# 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 = {}) ⇒ Object

Configure model for this plugin

Examples:

Disable caching

Item.plugin :enum_values, caching: false

Define predicate methods for all enum fields

Item.plugin :enum_values, predicate_methods: true

Define predicate methods for specific enum fields

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

Define predicate methods for specific enum field

Item.plugin :enum_values, predicate_methods: :status

Parameters:

  • model (Sequel::Model)

    model in which plugin enabled

  • options (Hash) (defaults to: {})

    options for plugin

Options Hash (options):

  • caching (Boolean) — default: true

    cache enum values

  • predicate_methods (Boolean, Symbol, Array<Symbol>) — default: false

    enum fields for which predicate methods will be defined



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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