Module: Shameless::Model

Defined in:
lib/shameless/model.rb

Defined Under Namespace

Modules: InstanceMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



6
7
8
# File 'lib/shameless/model.rb', line 6

def store
  @store
end

Instance Method Details

#attach_to(store, name) ⇒ Object



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

def attach_to(store, name)
  @store = store
  @name = name || self.name.downcase # TODO use activesupport?

  include(InstanceMethods)
end

#cell(name) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/shameless/model.rb', line 23

def cell(name)
  name = name.to_s

  define_method(name) do
    @cells ||= {}
    @cells[name] ||= Cell.new(self, name)
  end
end

#create_tables!Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/shameless/model.rb', line 57

def create_tables!
  @store.create_table!(table_name) do |t|
    t.primary_key :id
    t.varchar :uuid, size: 36
    t.varchar :column_name, null: false
    t.integer :ref_key, null: false
    t.mediumblob :body
    t.datetime :created_at, null: false

    t.index i[uuid column_name ref_key], unique: true
  end

  @indices.each(&:create_tables!)
end

#fetch_cell(shardable_value, uuid, cell_name) ⇒ Object



47
48
49
50
51
# File 'lib/shameless/model.rb', line 47

def fetch_cell(shardable_value, uuid, cell_name)
  query = {uuid: uuid, column_name: cell_name}

  @store.where(table_name, shardable_value, query).order(:ref_key).last
end

#index(name = nil, &block) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/shameless/model.rb', line 15

def index(name = nil, &block)
  @indices ||= []
  index = Index.new(name, self, &block)
  @indices << index

  define_singleton_method("#{index.name}_index") { index }
end

#prevent_readonly_attribute_mutation!(key) ⇒ Object



76
77
78
# File 'lib/shameless/model.rb', line 76

def prevent_readonly_attribute_mutation!(key)
  @indices.each {|i| i.prevent_readonly_attribute_mutation!(key) }
end

#put(values) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/shameless/model.rb', line 32

def put(values)
  uuid = SecureRandom.uuid

  new(uuid, values).tap do |model|
    model.save

    index_values = values.merge(uuid: uuid)
    @indices.each {|i| i.put(index_values) }
  end
end

#put_cell(shardable_value, cell_values) ⇒ Object



43
44
45
# File 'lib/shameless/model.rb', line 43

def put_cell(shardable_value, cell_values)
  @store.put(table_name, shardable_value, cell_values)
end

#table_nameObject



53
54
55
# File 'lib/shameless/model.rb', line 53

def table_name
  "#{@store.name}_#{@name}"
end

#where(query) ⇒ Object



72
73
74
# File 'lib/shameless/model.rb', line 72

def where(query)
  primary_index.where(query)
end