Class: Legion::Extensions::MindGrowth::Helpers::ProposalStore
- Inherits:
-
Object
- Object
- Legion::Extensions::MindGrowth::Helpers::ProposalStore
- Defined in:
- lib/legion/extensions/mind_growth/helpers/proposal_store.rb
Constant Summary collapse
- MAX_PROPOSALS =
500
Instance Method Summary collapse
- #all ⇒ Object
- #approved ⇒ Object
- #build_queue ⇒ Object
- #by_category(category) ⇒ Object
- #by_status(status) ⇒ Object
- #clear ⇒ Object
- #clear_persisted! ⇒ Object
- #get(id) ⇒ Object
-
#initialize(rehydrate: true) ⇒ ProposalStore
constructor
A new instance of ProposalStore.
- #recent(limit: 20) ⇒ Object
- #stats ⇒ Object
- #store(proposal) ⇒ Object
- #update(proposal) ⇒ Object
Constructor Details
#initialize(rehydrate: true) ⇒ ProposalStore
Returns a new instance of ProposalStore.
10 11 12 13 14 15 |
# File 'lib/legion/extensions/mind_growth/helpers/proposal_store.rb', line 10 def initialize(rehydrate: true) @proposals = {} @mutex = Mutex.new @persistence = ProposalPersistence.new rehydrate_from_cache if rehydrate end |
Instance Method Details
#all ⇒ Object
36 37 38 |
# File 'lib/legion/extensions/mind_growth/helpers/proposal_store.rb', line 36 def all @mutex.synchronize { @proposals.values.dup } end |
#approved ⇒ Object
48 49 50 |
# File 'lib/legion/extensions/mind_growth/helpers/proposal_store.rb', line 48 def approved by_status(:approved) end |
#build_queue ⇒ Object
52 53 54 |
# File 'lib/legion/extensions/mind_growth/helpers/proposal_store.rb', line 52 def build_queue by_status(:approved).sort_by { |p| -(p.scores.values.sum / p.scores.size.to_f) } end |
#by_category(category) ⇒ Object
44 45 46 |
# File 'lib/legion/extensions/mind_growth/helpers/proposal_store.rb', line 44 def by_category(category) @mutex.synchronize { @proposals.values.select { |p| p.category == category.to_sym } } end |
#by_status(status) ⇒ Object
40 41 42 |
# File 'lib/legion/extensions/mind_growth/helpers/proposal_store.rb', line 40 def by_status(status) @mutex.synchronize { @proposals.values.select { |p| p.status == status.to_sym } } end |
#clear ⇒ Object
67 68 69 |
# File 'lib/legion/extensions/mind_growth/helpers/proposal_store.rb', line 67 def clear @mutex.synchronize { @proposals.clear } end |
#clear_persisted! ⇒ Object
71 72 73 74 75 76 |
# File 'lib/legion/extensions/mind_growth/helpers/proposal_store.rb', line 71 def clear_persisted! @mutex.synchronize do @proposals.clear @persistence.delete_all_proposals end end |
#get(id) ⇒ Object
25 26 27 |
# File 'lib/legion/extensions/mind_growth/helpers/proposal_store.rb', line 25 def get(id) @mutex.synchronize { @proposals[id] } end |
#recent(limit: 20) ⇒ Object
56 57 58 |
# File 'lib/legion/extensions/mind_growth/helpers/proposal_store.rb', line 56 def recent(limit: 20) @mutex.synchronize { @proposals.values.sort_by { |p| -p.created_at.to_f }.first(limit) } end |
#stats ⇒ Object
60 61 62 63 64 65 |
# File 'lib/legion/extensions/mind_growth/helpers/proposal_store.rb', line 60 def stats @mutex.synchronize do statuses = @proposals.values.group_by(&:status).transform_values(&:count) { total: @proposals.size, by_status: statuses } end end |
#store(proposal) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/legion/extensions/mind_growth/helpers/proposal_store.rb', line 17 def store(proposal) @mutex.synchronize do evict_oldest if @proposals.size >= MAX_PROPOSALS @proposals[proposal.id] = proposal @persistence.save_proposal(proposal.to_h) end end |
#update(proposal) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/legion/extensions/mind_growth/helpers/proposal_store.rb', line 29 def update(proposal) @mutex.synchronize do @proposals[proposal.id] = proposal @persistence.save_proposal(proposal.to_h) end end |