Class: Torque::PostgreSQL::Attributes::EnumSet

Inherits:
Set
  • Object
show all
Extended by:
Enumerable
Includes:
Comparable
Defined in:
lib/torque/postgresql/attributes/enum_set.rb,
sig/torque/postgresql/attributes/enum_set.rbs

Defined Under Namespace

Classes: EnumSetError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*values) ⇒ EnumSet

Override string initializer to check for a valid value

Parameters:

  • values (Object)


99
100
101
102
103
104
105
106
107
108
# File 'lib/torque/postgresql/attributes/enum_set.rb', line 99

def initialize(*values)
  items =
    if values.size === 1 && values.first.is_a?(Numeric)
      transform_power(values.first)
    else
      transform_values(values)
    end

  @hash = items.zip(Array.new(items.size, true)).to_h
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 '-'

Parameters:

  • method_name (Symbol)
  • arguments (Object)

Returns:

  • (Object)


217
218
219
220
221
222
223
224
225
226
227
# File 'lib/torque/postgresql/attributes/enum_set.rb', line 217

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

  if name.chomp!('?')
    include?(name)
  elsif name.chomp!('!')
    add(name) unless include?(name)
  else
    super
  end
end

Class Method Details

.connection_specification_name ⇒ Object

Parameters:

  • (Object)

Returns:

  • (Object)


23
# File 'sig/torque/postgresql/attributes/enum_set.rbs', line 23

def self.connection_specification_name: (*untyped) -> untyped

.each {|arg0| ... } ⇒ Object

Parameters:

  • (Object)

Yields:

Yield Parameters:

Yield Returns:

  • (Object)

Returns:

  • (Object)


14
# File 'sig/torque/postgresql/attributes/enum_set.rbs', line 14

def self.each: (*untyped) ?{ (Enum) -> untyped } -> untyped

.enum_source ⇒ singleton(Enum)

The original Enum implementation, for individual values

Returns:



40
41
42
# File 'lib/torque/postgresql/attributes/enum_set.rb', line 40

def enum_source
  const_get('EnumSource')
end

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

Parameters:

  • value (String, Symbol)
  • (Object)

Returns:

  • (Object)


63
64
65
# File 'lib/torque/postgresql/attributes/enum_set.rb', line 63

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 enum sets will be manually initialized

Parameters:

  • klass (model)
  • method_name (Symbol, nil) (defaults to: nil)

Returns:

  • (Object)


32
33
34
35
36
37
# File 'lib/torque/postgresql/attributes/enum_set.rb', line 32

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

.keys ⇒ Array[Symbol]

Returns:

  • (Array[Symbol])


16
# File 'sig/torque/postgresql/attributes/enum_set.rbs', line 16

def self.keys: () -> Array[Symbol]

.length ⇒ Integer

Returns:

  • (Integer)


22
# File 'sig/torque/postgresql/attributes/enum_set.rbs', line 22

def self.length: () -> Integer

.lookup(name, enum_klass) ⇒ singleton(EnumSet)

Find or create the class that will handle the value

Parameters:

  • name (table_ref)
  • enum_klass (singleton(Enum))

Returns:



19
20
21
22
23
24
25
26
27
28
# File 'lib/torque/postgresql/attributes/enum_set.rb', line 19

def lookup(name, enum_klass)
  const     = name.to_s.camelize + 'Set'
  namespace = PostgreSQL.config.enum.namespace

  return namespace.const_get(const) if namespace.const_defined?(const)

  klass = Class.new(EnumSet)
  klass.const_set('EnumSource', enum_klass)
  namespace.const_set(const, klass)
end

.members ⇒ Array[Enum]

Returns:



17
# File 'sig/torque/postgresql/attributes/enum_set.rbs', line 17

def self.members: () -> Array[Enum]

.new(*values) ⇒ Object

Overpass new so blank values return only nil

Parameters:

  • values (Object)

Returns:

  • (Object)


50
51
52
53
# File 'lib/torque/postgresql/attributes/enum_set.rb', line 50

def new(*values)
  return Lazy.new(self, []) if values.compact.blank?
  super
end

.power(*values) ⇒ Integer?

Get the power, 2 ** index, of each element

Parameters:

  • values (Object)

Returns:

  • (Integer, nil)


69
70
71
72
73
74
75
76
77
# File 'lib/torque/postgresql/attributes/enum_set.rb', line 69

def power(*values)
  values.flatten.map do |item|
    item = item.to_i if item.is_a?(Enum)
    item = values.index(item) unless item.is_a?(Numeric)

    next 0 if item.nil? || item >= size
    2 ** item
  end.reduce(:+)
end

.sample ⇒ Object

Use the power to get a sample of the value

Returns:

  • (Object)


45
46
47
# File 'lib/torque/postgresql/attributes/enum_set.rb', line 45

def sample
  new(rand(0..((2 ** size) - 1)))
end

.scope(attribute, value) ⇒ ::Arel::Nodes::InfixOperation

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

Parameters:

  • attribute (arel_attribute)
  • value (Object)

Returns:

  • (::Arel::Nodes::InfixOperation)


80
81
82
# File 'lib/torque/postgresql/attributes/enum_set.rb', line 80

def scope(attribute, value)
  attribute.contains(FN.bind_with(attribute, value).pg_cast(type_name))
end

.size ⇒ Integer

Returns:

  • (Integer)


21
# File 'sig/torque/postgresql/attributes/enum_set.rbs', line 21

def self.size: () -> Integer

.texts ⇒ Array[String]

Returns:

  • (Array[String])


18
# File 'sig/torque/postgresql/attributes/enum_set.rbs', line 18

def self.texts: () -> Array[String]

.to_options ⇒ Array[[String, String]]

Returns:

  • (Array[[String, String]])


19
# File 'sig/torque/postgresql/attributes/enum_set.rbs', line 19

def self.to_options: () -> Array[[String, String]]

.type_name ⇒ String

Get the type name from its class name

Returns:

  • (String)


56
57
58
# File 'lib/torque/postgresql/attributes/enum_set.rb', line 56

def type_name
  @type_name ||= enum_source.type_name + '[]'
end

.valid? ⇒ Boolean

Parameters:

  • value (Object)

Returns:

  • (Boolean)


20
# File 'sig/torque/postgresql/attributes/enum_set.rbs', line 20

def self.valid?: (untyped value) -> bool

.values ⇒ Array[String]?

Returns:

  • (Array[String], nil)


15
# File 'sig/torque/postgresql/attributes/enum_set.rbs', line 15

def self.values: () -> Array[String]?

Instance Method Details

#&(other) ⇒ Object

Override bitwise & operator to ensure formatted values

Parameters:

  • other (Enumerable[untyped])

Returns:

  • (Object)


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

def &(other)
  other = other.entries.map(&method(:instantiate))
  values = @hash.keys.select { |k| other.include?(k) }
  self.class.new(values)
end

#<=>(other) ⇒ Integer?

Allow comparison between values of the same enum

Parameters:

  • other (Object)

Returns:

  • (Integer, nil)


111
112
113
114
115
116
117
118
119
120
121
# File 'lib/torque/postgresql/attributes/enum_set.rb', line 111

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

  to_i <=>
    case other
    when Numeric, EnumSet then other.to_i
    when String, Symbol   then self.class.power(other.to_s)
    when Array, Set       then self.class.power(*other)
    else raise_comparison(other)
    end
end

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

Only allow value comparison with values of the same class

Parameters:

  • other (Object)

Returns:

  • (Boolean)


124
125
126
127
128
# File 'lib/torque/postgresql/attributes/enum_set.rb', line 124

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

#[]=(key, value) ⇒ Object

Replace the setter by instantiating the value

Parameters:

  • key (Object)
  • value (Object)

Returns:

  • (Object)


153
154
155
# File 'lib/torque/postgresql/attributes/enum_set.rb', line 153

def []=(key, value)
  super(key, instantiate(value))
end

#add ⇒ self

Parameters:

  • other (Object)

Returns:

  • (self)


54
# File 'sig/torque/postgresql/attributes/enum_set.rbs', line 54

def add: (untyped other) -> self

#delete ⇒ self

Parameters:

  • other (Object)

Returns:

  • (self)


55
# File 'sig/torque/postgresql/attributes/enum_set.rbs', line 55

def delete: (untyped other) -> self

#include? ⇒ Boolean?

Parameters:

  • other (Object)

Returns:

  • (Boolean, nil)


56
# File 'sig/torque/postgresql/attributes/enum_set.rbs', line 56

def include?: (untyped other) -> bool?

#inspect ⇒ String

Change the inspection to show the enum name

Returns:

  • (String)


148
149
150
# File 'lib/torque/postgresql/attributes/enum_set.rb', line 148

def inspect
  "[#{map(&:inspect).join(', ')}]"
end

#merge(other) ⇒ self

Override the merge method to ensure formatted values

Parameters:

  • other (Enumerable[untyped])

Returns:

  • (self)


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

def merge(other)
  super other.map(&method(:instantiate))
end

#replace(*values) ⇒ self

It only accepts if the other value is valid

Parameters:

  • values (Object)

Returns:

  • (self)


132
133
134
# File 'lib/torque/postgresql/attributes/enum_set.rb', line 132

def replace(*values)
  super(transform_values(values))
end

#subtract ⇒ self

Parameters:

  • other (Object)

Returns:

  • (self)


57
# File 'sig/torque/postgresql/attributes/enum_set.rbs', line 57

def subtract: (untyped other) -> self

#text(attr = nil, model = nil) ⇒ String Also known as: to_s

Get a translated version of the value

Parameters:

  • attr (column_ref, nil) (defaults to: nil)
  • model (Object) (defaults to: nil)

Returns:

  • (String)


137
138
139
# File 'lib/torque/postgresql/attributes/enum_set.rb', line 137

def text(attr = nil, model = nil)
  map { |item| item.text(attr, model) }.to_sentence
end

#to_i ⇒ Integer?

Get the index of the value

Returns:

  • (Integer, nil)


143
144
145
# File 'lib/torque/postgresql/attributes/enum_set.rb', line 143

def to_i
  self.class.power(@hash.keys)
end