Class: Shameless::Index
- Inherits:
-
Object
- Object
- Shameless::Index
- Defined in:
- lib/shameless/index.rb
Constant Summary collapse
- PRIMARY =
:primary
- DataTypes =
{integer: Integer, string: String}
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #column(name, type) ⇒ Object
- #column?(key) ⇒ Boolean
- #create_tables! ⇒ Object
- #full_name ⇒ Object
- #index_values(values, all_required) ⇒ Object
-
#initialize(name, model, &block) ⇒ Index
constructor
A new instance of Index.
- #prevent_readonly_attribute_mutation!(key) ⇒ Object
- #put(values) ⇒ Object
- #shard_on(shard_on) ⇒ Object
- #table_name ⇒ Object
- #where(query, &block) ⇒ Object
Constructor Details
Instance Attribute Details
#name ⇒ Object (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
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_name ⇒ Object
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_name ⇒ Object
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 |