Class: Shameless::Index

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

Constant Summary collapse

PRIMARY =
:primary
DataTypes =
{integer: Integer, string: String}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, model, &block) ⇒ Index

Returns a new instance of Index.



9
10
11
12
13
# File 'lib/shameless/index.rb', line 9

def initialize(name, model, &block)
  @name = name || PRIMARY
  @model = model
  instance_eval(&block)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/shameless/index.rb', line 7

def name
  @name
end

Instance Method Details

#column(name, type) ⇒ Object



23
24
25
26
# File 'lib/shameless/index.rb', line 23

def column(name, type)
  @columns ||= {}
  @columns[name] = type
end

#column?(key) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/shameless/index.rb', line 75

def column?(key)
  @columns.keys.any? {|c| c.to_s == key.to_s }
end

#create_tables!Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/shameless/index.rb', line 63

def create_tables!
  @model.store.create_table!(table_name) do |t, sharded_table_name|
    @columns.each do |name, type|
      t.column name, type, null: false
    end

    t.varchar :uuid, size: 36

    t.index @columns.keys, name: "#{sharded_table_name}_index", unique: true
  end
end

#full_nameObject



49
50
51
# File 'lib/shameless/index.rb', line 49

def full_name
  "#{@name}_index"
end

#index_values(values, all_required) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/shameless/index.rb', line 53

def index_values(values, all_required)
  (@columns.keys + [:uuid]).each_with_object({}) do |column, o|
    if all_required
      o[column] = values.fetch(column)
    else
      o[column] = values[column] if values.key?(column)
    end
  end
end

#prevent_readonly_attribute_mutation!(key) ⇒ Object



79
80
81
82
83
# File 'lib/shameless/index.rb', line 79

def prevent_readonly_attribute_mutation!(key)
  if column?(key)
    raise ReadonlyAttributeMutation, "The attribute #{key} cannot be modified because it's part of the #{@name} index"
  end
end

#put(values) ⇒ Object



32
33
34
35
36
37
# File 'lib/shameless/index.rb', line 32

def put(values)
  shardable_value = values.fetch(@shard_on).to_i
  index_values = index_values(values, true)

  @model.store.put(table_name, shardable_value, index_values)
end

#shard_on(shard_on) ⇒ Object



28
29
30
# File 'lib/shameless/index.rb', line 28

def shard_on(shard_on)
  @shard_on = shard_on
end

#table_nameObject



45
46
47
# File 'lib/shameless/index.rb', line 45

def table_name
  "#{@model.table_name}_#{full_name}"
end

#where(query, &block) ⇒ Object



39
40
41
42
43
# File 'lib/shameless/index.rb', line 39

def where(query, &block)
  shardable_value = query.fetch(@shard_on).to_i
  query = index_values(query, false)
  @model.store.where(table_name, shardable_value, query, &block).map {|r| @model.new(r[:uuid]) }
end