Class: Perpetuity::Postgres::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/perpetuity/postgres/index.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Index

Returns a new instance of Index.



8
9
10
11
12
13
# File 'lib/perpetuity/postgres/index.rb', line 8

def initialize options={}
  @attributes = options.fetch(:attributes)
  @name       = options.fetch(:name)
  @unique     = options.fetch(:unique) { false }
  @active     = options.fetch(:active) { false }
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/perpetuity/postgres/index.rb', line 6

def attributes
  @attributes
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/perpetuity/postgres/index.rb', line 6

def name
  @name
end

Class Method Details

.from_sql(sql_result) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/perpetuity/postgres/index.rb', line 15

def self.from_sql sql_result
  attributes = sql_result['attributes'].gsub(/[{}]/, '').split(',').map do |attr|
    Attribute.new(attr)
  end
  unique = sql_result['unique'] == 't'
  active = sql_result['active'] == 't'
  new(name: sql_result['name'],
      attributes: attributes,
      unique: unique,
      active: active)
end

Instance Method Details

#==(other) ⇒ Object



55
56
57
58
59
60
# File 'lib/perpetuity/postgres/index.rb', line 55

def == other
  other.is_a?(self.class) &&
  attributes.map(&:to_s) == other.attributes.map(&:to_s) &&
  name.to_s == other.name.to_s &&
  unique? == other.unique?
end

#activate!Object



47
48
49
# File 'lib/perpetuity/postgres/index.rb', line 47

def activate!
  @active = true
end

#active?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/perpetuity/postgres/index.rb', line 39

def active?
  !!@active
end

#attributeObject



27
28
29
# File 'lib/perpetuity/postgres/index.rb', line 27

def attribute
  attributes.first
end

#attribute_namesObject



31
32
33
# File 'lib/perpetuity/postgres/index.rb', line 31

def attribute_names
  attributes.map { |attr| attr.name.to_s }
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/perpetuity/postgres/index.rb', line 62

def eql? other
  self == other
end

#hashObject



66
67
68
# File 'lib/perpetuity/postgres/index.rb', line 66

def hash
  name.to_s.hash
end

#inactive?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/perpetuity/postgres/index.rb', line 43

def inactive?
  !active?
end

#tableObject



51
52
53
# File 'lib/perpetuity/postgres/index.rb', line 51

def table
  name.gsub("_#{attributes.map(&:to_s).join('_')}_index", '')
end

#unique?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/perpetuity/postgres/index.rb', line 35

def unique?
  !!@unique
end