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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, attribute, subtype, initial, options) ⇒ Enum

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



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

def initialize(klass, attribute, subtype, initial, options)
  @klass     = klass
  @attribute = attribute.to_s
  @subtype   = subtype
  @initial   = initial
  @options   = options

  @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

#attribute ⇒ Object

Returns the value of attribute attribute.



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

def attribute
  @attribute
end

#initial ⇒ Object

Returns the value of attribute initial.



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

def initial
  @initial
end

#klass ⇒ Object

Returns the value of attribute klass.



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

def klass
  @klass
end

#options ⇒ Object

Returns the value of attribute options.



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

def options
  @options
end

#subtype ⇒ Object

Returns the value of attribute subtype.



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

def subtype
  @subtype
end

#values ⇒ Object

Returns the value of attribute values.



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

def values
  @values
end

Instance Method Details

#build ⇒ Object

Create all methods needed



82
83
84
85
86
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 82

def build
  plural
  text
  all_values
end

#conflicting? ⇒ Boolean

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

Returns:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/torque/postgresql/attributes/builder/enum.rb', line 54

def conflicting?
  return false if options[:force] == true

  dangerous?(attribute.pluralize, true)
  dangerous?(attribute + '_text')

  values_methods.each do |attr, list|
    list.map(&method(:dangerous?))
  end

  return false
rescue Interrupt => err
  if !initial
    raise ArgumentError, <<-MSG.strip.gsub(/\n +/, ' ')
      #{subtype.class.name} was not able to generate requested
      methods because the method #{err} already exists in
      #{klass.name}.
    MSG
  else
    warn <<-MSG.strip.gsub(/\n +/, ' ')
      #{subtype.class.name} was not able to autoload on
      #{klass.name} because the method #{err} already exists.
    MSG
    return true
  end
end

#values_methods ⇒ Object

Get the list of methods based on enum values



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

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|
      val   = val.tr('-', '_')
      scope = base % val
      ask   = scope + '?'
      bang  = scope + '!'
      [val, [scope, ask, bang]]
    end.to_h
  end
end