Class: Alexandria::SmartLibrary::Rule

Inherits:
Object
  • Object
show all
Extended by:
GetText
Includes:
GetText
Defined in:
lib/alexandria/smart_library.rb

Defined Under Namespace

Modules: Operands, Operators Classes: LeftOperand, Operand, Operator

Constant Summary collapse

BOOLEAN_OPERATORS =
[
  Operators::IS_TRUE,
  Operators::IS_NOT_TRUE
].sort
STRING_OPERATORS =
[
  Operators::IS,
  Operators::IS_NOT,
  Operators::CONTAINS,
  Operators::DOES_NOT_CONTAIN,
  Operators::STARTS_WITH,
  Operators::ENDS_WITH
].sort
STRING_ARRAY_OPERATORS =
[
  Operators::CONTAINS,
  Operators::DOES_NOT_CONTAIN
].sort
INTEGER_OPERATORS =
[
  Operators::IS,
  Operators::IS_NOT,
  Operators::IS_GREATER_THAN,
  Operators::IS_LESS_THAN
].sort
TIME_OPERATORS =
[
  Operators::IS,
  Operators::IS_NOT,
  Operators::IS_AFTER,
  Operators::IS_BEFORE,
  Operators::IS_IN_LAST,
  Operators::IS_NOT_IN_LAST
].sort

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operand, operation, value) ⇒ Rule

Returns a new instance of Rule.



240
241
242
243
244
245
246
# 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

Instance Attribute Details

#operandObject

Returns the value of attribute operand.



238
239
240
# File 'lib/alexandria/smart_library.rb', line 238

def operand
  @operand
end

#operationObject

Returns the value of attribute operation.



238
239
240
# File 'lib/alexandria/smart_library.rb', line 238

def operation
  @operation
end

#valueObject

Returns the value of attribute value.



238
239
240
# File 'lib/alexandria/smart_library.rb', line 238

def value
  @value
end

Class Method Details

.from_hash(hash) ⇒ Object



248
249
250
251
252
253
254
255
256
# 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

.operations_for_operand(operand) ⇒ Object



444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# 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

Instance Method Details

#filter_procObject



469
470
471
472
473
474
475
476
477
478
479
480
481
# 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_hashObject



258
259
260
261
262
263
264
# File 'lib/alexandria/smart_library.rb', line 258

def to_hash
  {
    operand: @operand.book_selector,
    operation: @operation.sym,
    value: @value
  }
end