class Alexandria::SmartLibrary::Rule

Constants

BOOLEAN_OPERATORS
INTEGER_OPERATORS
Operand
Operator
STRING_ARRAY_OPERATORS
STRING_OPERATORS
TIME_OPERATORS

Attributes

operand[RW]
operation[RW]
value[RW]

Public Class Methods

from_hash(hash) click to toggle source
# File lib/alexandria/smart_library.rb, line 248
def self.from_hash(hash)
  operand = Operands::LEFT.find do |x|
    x.book_selector == hash[:operand]
  end
  operator = Operators::ALL.find do |x|
    x.sym == hash[:operation]
  end
  Rule.new(operand, operator, hash[:value])
end
new(operand, operation, value) click to toggle source
# File lib/alexandria/smart_library.rb, line 240
def initialize(operand, operation, value)
  raise if operand.nil? || operation.nil? # value can be nil

  @operand = operand
  @operation = operation
  @value = value
end
operations_for_operand(operand) click to toggle source
# File lib/alexandria/smart_library.rb, line 444
def self.operations_for_operand(operand)
  case operand.klass.name
  when "String"
    STRING_OPERATORS.map { |x| [x, Operands::STRING] }
  when "Array"
    STRING_ARRAY_OPERATORS.map { |x| [x, Operands::STRING] }
  when "Integer"
    INTEGER_OPERATORS.map { |x| [x, Operands::INTEGER] }
  when "TrueClass"
    BOOLEAN_OPERATORS.map { |x| [x, nil] }
  when "Time"
    TIME_OPERATORS.map do |x|
      if (x == Operators::IS_IN_LAST) ||
          (x == Operators::IS_NOT_IN_LAST)

        [x, Operands::DAYS]
      else
        [x, Operands::TIME]
      end
    end
  else
    raise format(_("invalid operand klass %<klass>s"), klass: operand.klass)
  end
end

Public Instance Methods

filter_proc() click to toggle source
# File lib/alexandria/smart_library.rb, line 469
def filter_proc
  proc do |book|
    left_value = book.send(@operand.book_selector)
    right_value = @value
    if right_value.is_a?(String)
      left_value = left_value.to_s.downcase
      right_value = right_value.downcase
    end
    params = [left_value]
    params << right_value unless right_value.nil?
    @operation.proc.call(*params)
  end
end
to_hash() click to toggle source
# File lib/alexandria/smart_library.rb, line 258
def to_hash
  {
    operand: @operand.book_selector,
    operation: @operation.sym,
    value: @value
  }
end