Class: Torque::PostgreSQL::Attributes::Builder::Enum

Inherits:
Object
  • Object
show all
Defined in:
lib/torque/postgresql/attributes/builder/enum.rb

Constant Summary collapse

VALID_TYPES =
%i[enum enum_set].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, attribute, options) ⇒ Enum

Start a new builder of methods for enum values on ActiveRecord::Base

Raises:

  • (Interrupt)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 12

def initialize(klass, attribute, options)
  @klass     = klass
  @attribute = attribute.to_s
  @subtype   = klass.attribute_types[@attribute]
  @options   = options

  raise Interrupt unless subtype.respond_to?(:klass)
  @values    = subtype.klass.values

  if @options[:only]
    @values &= Array(@options[:only]).map(&:to_s)
  end

  if @options[:except]
    @values -= Array(@options[:except]).map(&:to_s)
  end
end

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute.



8
9
10
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 8

def attribute
  @attribute
end

#instance_moduleObject

Returns the value of attribute instance_module.



8
9
10
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 8

def instance_module
  @instance_module
end

#klassObject

Returns the value of attribute klass.



8
9
10
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 8

def klass
  @klass
end

#klass_moduleObject

Returns the value of attribute klass_module.



8
9
10
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 8

def klass_module
  @klass_module
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 8

def options
  @options
end

#subtypeObject

Returns the value of attribute subtype.



8
9
10
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 8

def subtype
  @subtype
end

#valuesObject

Returns the value of attribute values.



8
9
10
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 8

def values
  @values
end

Instance Method Details

#buildObject

Create all methods needed



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 89

def build
  @klass_module = Module.new
  @instance_module = Module.new

  plural
  stringify
  all_values
  set_scopes if set_features?

  klass.extend klass_module
  klass.include instance_module
end

#conflicting?Boolean

Check if any of the methods that will be created get in conflict with the base class methods

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 60

def conflicting?
  return if options[:force] == true
  attributes = attribute.pluralize

  dangerous?(attributes, true)
  dangerous?("#{attributes}_keys", true)
  dangerous?("#{attributes}_texts", true)
  dangerous?("#{attributes}_options", true)
  dangerous?("#{attribute}_text")

  if set_features?
    dangerous?("has_#{attributes}", true)
    dangerous?("has_any_#{attributes}", true)
  end

  values_methods.each do |attr, (scope, ask, bang, *)|
    dangerous?(scope, true)
    dangerous?(bang)
    dangerous?(ask)
  end
rescue Interrupt => err
  raise ArgumentError, <<-MSG.squish
    Enum #{subtype.name} was not able to generate requested
    methods because the method #{err} already exists in
    #{klass.name}.
  MSG
end

#set_features?Boolean

Check if it’s building the methods for sets

Returns:

  • (Boolean)


54
55
56
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 54

def set_features?
  options[:set_features].present?
end

#values_methodsObject

Get the list of methods based on enum values



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 31

def values_methods
  return @values_methods if defined?(@values_methods)

  prefix = options.fetch(:prefix, nil).try(:<<, '_')
  suffix = options.fetch(:suffix, nil).try(:prepend, '_')

  prefix = attribute + '_' if prefix == true
  suffix = '_' + attribute if suffix == true

  base   = "#{prefix}%s#{suffix}"

  @values_methods = begin
    values.map do |val|
      key   = val.downcase.tr('- ', '__')
      scope = base % key
      ask   = scope + '?'
      bang  = scope + '!'
      [key, [scope, ask, bang, val]]
    end.to_h
  end
end