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
# File 'lib/shameless/model.rb', line 23

def cell(name)
  name = name.to_s

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

#create_tables!Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/shameless/model.rb', line 76

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

    created_at_type = @store.configuration.legacy_created_at_is_bigint ? :bigint : :datetime
    t.column :created_at, created_at_type, null: false

    t.index %i[uuid column_name ref_key], name: "#{sharded_table_name}_model", unique: true
  end

  @indices.each(&:create_tables!)
end

#fetch_cell(shardable_value, uuid, cell_name, ref_key) ⇒ Object



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

def fetch_cell(shardable_value, uuid, cell_name, ref_key)
  query = {uuid: uuid, column_name: cell_name}
  query[:ref_key] = ref_key if ref_key

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

#fetch_latest_cells(shard:, cursor:, limit:) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/shameless/model.rb', line 59

def fetch_latest_cells(shard:, cursor:, limit:)
  query = ['id > ?', cursor]
  @store.where(table_name, shard, query).limit(limit).map do |cell_values|
    model = new(cell_values[:uuid])
    name = cell_values[:column_name].to_sym
    Cell.new(model, name, cell_values)
  end
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.full_name) { index }
end

#prevent_readonly_attribute_mutation!(key) ⇒ Object



101
102
103
# File 'lib/shameless/model.rb', line 101

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

#put(values) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/shameless/model.rb', line 31

def put(values)
  if model = where(values).first
    model_values = reject_index_values(values)
    model.update(model_values)
    model
  else
    uuid = SecureRandom.uuid

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

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

#put_cell(shardable_value, cell_values) ⇒ Object



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

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

#reject_index_values(values) ⇒ Object



97
98
99
# File 'lib/shameless/model.rb', line 97

def reject_index_values(values)
  values.reject {|k, _| @indices.any? {|i| i.column?(k) } }
end

#table_nameObject



68
69
70
# File 'lib/shameless/model.rb', line 68

def table_name
  [@store.name, @name].compact.join('_')
end

#table_namesObject



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

def table_names
  [table_name, *@indices.map(&:table_name)]
end

#where(query) ⇒ Object



93
94
95
# File 'lib/shameless/model.rb', line 93

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