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

Inherits:
String
  • Object
show all
Extended by:
Enumerable
Includes:
Comparable
Defined in:
lib/torque/postgresql/attributes/enum.rb

Defined Under Namespace

Classes: EnumError

Constant Summary collapse

LAZY_VALUE =
0.chr

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Enum

Override string initializer to check for a valid value



126
127
128
129
130
# File 'lib/torque/postgresql/attributes/enum.rb', line 126

def initialize(value)
  str_value = value.is_a?(Numeric) ? self.class.values[value.to_i] : value.to_s
  raise_invalid(value) unless self.class.valid?(str_value)
  super(str_value)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments) ⇒ Object (private)

Allow '_' to be associated to '-'



211
212
213
214
215
216
217
218
219
220
221
# File 'lib/torque/postgresql/attributes/enum.rb', line 211

def method_missing(method_name, *arguments)
  name = method_name.to_s

  if name.chomp!('?')
    self == name
  elsif name.chomp!('!')
    replace(name) unless self == name
  else
    super
  end
end

Class Method Details

.fetch(value) ⇒ Object Also known as: []



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

def fetch(value, *)
  new(value.to_s) if values.include?(value)
end

.include_on(klass, method_name = nil) ⇒ Object

Provide a method on the given class to setup which enums will be manually initialized



29
30
31
32
33
34
# File 'lib/torque/postgresql/attributes/enum.rb', line 29

def include_on(klass, method_name = nil)
  method_name ||= PostgreSQL.config.enum.base_method
  Builder.include_on(klass, method_name, Builder::Enum) do |builder|
    defined_enums[builder.attribute.to_s] = builder.subtype.klass
  end
end

.inherited(subclass) ⇒ Object



42
43
44
45
# File 'lib/torque/postgresql/attributes/enum.rb', line 42

def inherited(subclass)
  super
  subclass.instance_variable_set(:@load_values_monitor, ::Monitor.new)
end

.keysObject

List of values as symbols



61
62
63
# File 'lib/torque/postgresql/attributes/enum.rb', line 61

def keys
  values.map(&:to_sym)
end

.lookup(name) ⇒ Object

Find or create the class that will handle the value



19
20
21
22
23
24
25
# File 'lib/torque/postgresql/attributes/enum.rb', line 19

def lookup(name)
  const     = name.to_s.camelize
  namespace = PostgreSQL.config.enum.namespace

  return namespace.const_get(const) if namespace.const_defined?(const)
  namespace.const_set(const, Class.new(Enum))
end

.membersObject

Different from values, it returns the list of items already casted



66
67
68
# File 'lib/torque/postgresql/attributes/enum.rb', line 66

def members
  values.map(&method(:new))
end

.new(value) ⇒ Object

Overpass new so blank values return only nil



37
38
39
40
# File 'lib/torque/postgresql/attributes/enum.rb', line 37

def new(value)
  return Lazy.new(self, LAZY_VALUE) if value.blank?
  super
end

.scope(attribute, value) ⇒ Object

Build an active record scope for a given attribute against a value



101
102
103
# File 'lib/torque/postgresql/attributes/enum.rb', line 101

def scope(attribute, value)
  attribute.eq(value)
end

.textsObject

Get the list of the values translated by I18n



71
72
73
# File 'lib/torque/postgresql/attributes/enum.rb', line 71

def texts
  members.map(&:text)
end

.to_optionsObject

Get a list of values translated and ready for select



76
77
78
# File 'lib/torque/postgresql/attributes/enum.rb', line 76

def to_options
  texts.zip(values)
end

.type_nameObject

Get the type name from its class name



89
90
91
# File 'lib/torque/postgresql/attributes/enum.rb', line 89

def type_name
  @type_name ||= self.name.demodulize.underscore
end

.valid?(value) ⇒ Boolean

Check if the value is valid

Returns:

  • (Boolean)


94
95
96
97
98
# File 'lib/torque/postgresql/attributes/enum.rb', line 94

def valid?(value)
  return false if self == Enum
  return true if value.equal?(LAZY_VALUE)
  self.values.include?(value.to_s)
end

.valuesObject

Load the list of values in a lazy way



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/torque/postgresql/attributes/enum.rb', line 48

def values
  return if self == Enum
  return @values if defined?(@values)

  @load_values_monitor.synchronize do
    next if defined?(@values)
    @values = connection.enum_values(type_name).freeze
  end

  @values
end

Instance Method Details

#<=>(other) ⇒ Object

Allow comparison between values of the same enum



133
134
135
136
137
138
139
140
141
# File 'lib/torque/postgresql/attributes/enum.rb', line 133

def <=>(other)
  raise_comparison(other) if other.is_a?(Enum) && other.class != self.class

  case other
  when Numeric, Enum  then to_i <=> other.to_i
  when String, Symbol then to_i <=> self.class.values.index(other.to_s)
  else raise_comparison(other)
  end
end

#==(other) ⇒ Object Also known as: eql?

Only allow value comparison with values of the same class



144
145
146
147
148
# File 'lib/torque/postgresql/attributes/enum.rb', line 144

def ==(other)
  (self <=> other) == 0
rescue EnumError
  false
end

#inspectObject

Change the inspection to show the enum name



180
181
182
# File 'lib/torque/postgresql/attributes/enum.rb', line 180

def inspect
  nil? ? 'nil' : ":#{to_s}"
end

#nil?Boolean Also known as: empty?

Since it can have a lazy value, nil can be true here

Returns:

  • (Boolean)


152
153
154
# File 'lib/torque/postgresql/attributes/enum.rb', line 152

def nil?
  self == LAZY_VALUE
end

#replace(value) ⇒ Object

It only accepts if the other value is valid



158
159
160
161
# File 'lib/torque/postgresql/attributes/enum.rb', line 158

def replace(value)
  raise_invalid(value) unless self.class.valid?(value)
  super
end

#text(attr = nil, model = nil) ⇒ Object

Get a translated version of the value



164
165
166
167
# File 'lib/torque/postgresql/attributes/enum.rb', line 164

def text(attr = nil, model = nil)
  keys = i18n_keys(attr, model) << self.underscore.humanize
  ::I18n.translate(keys.shift, default: keys)
end

#to_iObject

Get the index of the value



175
176
177
# File 'lib/torque/postgresql/attributes/enum.rb', line 175

def to_i
  self.class.values.index(self)
end

#to_sObject

Change the string result for lazy value



170
171
172
# File 'lib/torque/postgresql/attributes/enum.rb', line 170

def to_s
  nil? ? '' : super
end