Class: Ephemeron::Store

Inherits:
ThreadStore
  • Object
show all
Defined in:
lib/ephemeron/store/update.rb,
lib/ephemeron/store/convert.rb,
lib/ephemeron/store/finders.rb,
lib/ephemeron/writers/store.rb,
lib/ephemeron/store/allow_fetch.rb

Defined Under Namespace

Modules: AllowFetch, Finders, Update Classes: Convert

Instance Method Summary collapse

Instance Method Details

#add(model, opts = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/ephemeron/writers/store.rb', line 19

def add(model, opts = {})
  return unless Validators::StoreElement.call(model)

  in_store_before = stored?(model)
  store[to_store_key(model)] = model unless stored?(model)
  Logger.log(model, :fetched) if opts[:log]
  add_alias(model, opts[:as], in_store_before)
  model
end

#aliasesObject



11
12
13
# File 'lib/ephemeron/writers/store.rb', line 11

def aliases
  self.ts_aliases ||= {}
end

#destroy(model) ⇒ Object



51
52
53
54
55
56
# File 'lib/ephemeron/writers/store.rb', line 51

def destroy(model)
  key = Convert.call model
  return if store[key].nil?

  store.delete key
end

#get(key) ⇒ Object



29
30
31
32
33
34
# File 'lib/ephemeron/writers/store.rb', line 29

def get(key)
  model = store[key]
  model ||= store[aliases[key]]
  Logger.log(model, :used) if model
  model
end

#has?(model, as_new_record: false) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/ephemeron/writers/store.rb', line 15

def has?(model, as_new_record: false)
  !store[Convert.call(model, as_new_record: as_new_record)].nil?
end

#persistObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/ephemeron/writers/store.rb', line 40

def persist
  changed_models = store.filter { |_, v| v.changed? }.values
                        .filter { |model| !Logger.has?(model, :skip_save) }
  ActiveRecord::Base.transaction do
    changed_models.each do |model|
      Ephemeron.allow_save! model
      model.save!
    end
  end
end

#resetObject



58
59
60
61
# File 'lib/ephemeron/writers/store.rb', line 58

def reset
  self.ts_store = {}
  self.ts_aliases = {}
end

#storeObject



7
8
9
# File 'lib/ephemeron/writers/store.rb', line 7

def store
  self.ts_store ||= {}
end

#validateObject



36
37
38
# File 'lib/ephemeron/writers/store.rb', line 36

def validate
  Validators::Store.call
end