Class: Hyperactive::Index::MatchSaver

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperactive/index.rb

Overview

A tiny callable class that saves stuff inside containers if they match certain criteria.

Instance Method Summary collapse

Constructor Details

#initialize(key, matcher, mode) ⇒ MatchSaver

Initialize this MatchSaver with a key, a callable matcher and a mode (:select, :reject, :delete_if_match or :delete_unless_match).



100
101
102
103
104
# File 'lib/hyperactive/index.rb', line 100

def initialize(key, matcher, mode)
  @key = key
  @matcher = matcher
  @mode = mode
end

Instance Method Details

#call(*arguments, &block) ⇒ Object

Depending on @mode and return value of @matcher.call may save record in the Hash-like container named @key in the main database after having yielded to block.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/hyperactive/index.rb', line 110

def call(*arguments, &block)
  yield

  record = arguments.first

  case @mode
  when :select
    if @matcher.call(record)
      Archipelago::Pirate::BLACKBEARD[@key, record.transaction][record.record_id] = record
    else
      Archipelago::Pirate::BLACKBEARD[@key, record.transaction].delete(record.record_id)
    end
  when :reject
    if @matcher.call(record)
      Archipelago::Pirate::BLACKBEARD[@key, record.transaction].delete(record.record_id)
    else
      Archipelago::Pirate::BLACKBEARD[@key, record.transaction][record.record_id] = record
    end
  when :delete_if_match
    if @matcher.call(record)
      Archipelago::Pirate::BLACKBEARD[@key, record.transaction].delete(record.record_id)
    end
  when :delete_unless_match
    unless @matcher.call(record)
      Archipelago::Pirate::BLACKBEARD[@key, record.transaction].delete(record.record_id)
    end
  end
end