Class: Revector

Inherits:
Object
  • Object
show all
Defined in:
lib/revector.rb,
lib/revector/utility.rb,
lib/revector/version.rb,
lib/revector/predicate/in.rb,
lib/revector/predicate/equal.rb,
lib/revector/predicate/endify.rb,
lib/revector/predicate/contains.rb,
lib/revector/predicate/startify.rb,
lib/revector/predicate/less_than.rb,
lib/revector/predicate/greater_than.rb

Defined Under Namespace

Modules: Contains, Endify, Equal, GreaterThan, GreaterThanEqual, Included, LessThan, LessThanEqual, NotContains, NotEndify, NotEqual, NotIncluded, NotStartify, Startify, Utility

Constant Summary collapse

AFFIRMATIVES =
[*]
      right = [''] unless right.any?
      Array(right).any? { |o| predicatable.compare(o, valueable) }
    else
      predicatable.compare(right, valueable)
    end
  end
end

def find_by_other(key, value, row)
  parts = key.to_s.split('_')

  predicate = Array(parts[-2..]).filter do |pkey|
    next unless PREDICATES.include? pkey

    parts -= [pkey]

    pkey
  end&.last || :eq

  iteratee = parts.join('_')

  predicatable = Predicate[predicate]
  valueable = value.respond_to?(:call) ? value.call : value

  predicatable.check!(row, iteratee, valueable)
end

Predicate = lambda do |named|
  {
    eq: Equal,
    noteq: NotEqual,
    not_eq: NotEqual,
    cont: Contains,
    notcont: NotContains,
    not_cont: NotContains,
    lt: LessThan,
    lteq: LessThanEqual,
    gt: GreaterThan,
    gteq: GreaterThanEqual,
    start: Startify,
    st: Startify,
    notstart: NotStartify,
    notst: NotStartify,
    not_start: NotStartify,
    not_st: NotStartify,
    end: Endify,
    notend: NotEndify,
    not_end: NotEndify,
    in: Included,
    notin: NotIncluded,
    not_in: NotIncluded,
    ex: Exists,
    not_ex: NotExists
  }[named.to_sym || :eq]
end
private_constant :Predicate

AFFIRMATIVES = %w[
  eq ex
  in cont
  lt lteq
  gt gteq
  st start
  end
].freeze
NEGATIVES =
%w[
  noteq not_eq
  not_ex
  notcont not_cont
  notstart notst not_start not_st
  notend not_end
  notin not_in
].freeze
VERSION =
'0.1.1'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bigdata, filters) ⇒ Revector

Returns a new instance of Revector.



20
21
22
23
24
# File 'lib/revector.rb', line 20

def initialize(bigdata, filters)
  @result = Array(bigdata)
  @filters = filters
  @idx = ::Set.new
end

Class Method Details

.swap(bigdata, filters) ⇒ Object



16
17
18
# File 'lib/revector.rb', line 16

def self.swap(bigdata, filters)
  new(bigdata, filters).swap
end

Instance Method Details

#filter_by(row, key, hash_or_value) ⇒ Object



41
42
43
44
45
46
# File 'lib/revector.rb', line 41

def filter_by(row, key, hash_or_value)
  case hash_or_value
  in ::Hash then find_by_hash(key, hash_or_value, row)
  else find_by_other(key, hash_or_value, row)
  end
end

#find_by_hash(key, pair_condition, row) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/revector.rb', line 48

def find_by_hash(key, pair_condition, row)
  pair_condition.collect do |predicate, value|
    keys = Utility::Keys.to_ary(key)

    right = Utility::TryFetchOrBlank[row, keys[0..-1]]
    predicatable = Predicate[predicate]
    valueable = value.respond_to?(:call) ? value.call : value

    case right
    in [*]
      right = [''] unless right.any?
      Array(right).any? { |o| predicatable.compare(o, valueable) }
    else
      predicatable.compare(right, valueable)
    end
  end
end

#find_by_other(key, value, row) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/revector.rb', line 66

def find_by_other(key, value, row)
  parts = key.to_s.split('_')

  predicate = Array(parts[-2..]).filter do |pkey|
    next unless PREDICATES.include? pkey

    parts -= [pkey]

    pkey
  end&.last || :eq

  iteratee = parts.join('_')

  predicatable = Predicate[predicate]
  valueable = value.respond_to?(:call) ? value.call : value

  predicatable.check!(row, iteratee, valueable)
end

#swapObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/revector.rb', line 26

def swap
  @result.each_with_index do |row, index|
    collected = @filters.collect do |key, condition|
      filter_by(row, key, condition)
    end.flatten.uniq

    case collected.all?
    in TrueClass then @idx.add(index)
    in FalseClass then @idx.delete(index)
    end
  end

  @result = @result.values_at(*@idx.to_a)
end