Class: Hyperactive::Record::MatchSaver

Inherits:
Object
  • Object
show all
Defined in:
lib/hyperactive/record.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).



114
115
116
117
118
# File 'lib/hyperactive/record.rb', line 114

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

Instance Method Details

#call(argument, &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.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/hyperactive/record.rb', line 124

def call(argument, &block)
  yield

  record = argument
  record = argument.last if Array === argument

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