Class: EPUB::Searcher::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/epub/searcher/result.rb

Defined Under Namespace

Classes: Step

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_steps, start_steps, end_steps) ⇒ Result

Returns a new instance of Result.

Parameters:

  • parent_steps (Array<Step>)

    common steps between start and end

  • start_steps (Array<Step>)

    steps to start from parent_steps

  • end_steps (Array<Step>)

    steps to end from parent_steps



42
43
44
# File 'lib/epub/searcher/result.rb', line 42

def initialize(parent_steps, start_steps, end_steps)
  @parent_steps, @start_steps, @end_steps = parent_steps, start_steps, end_steps
end

Instance Attribute Details

#end_stepsObject (readonly)

Returns the value of attribute end_steps.



37
38
39
# File 'lib/epub/searcher/result.rb', line 37

def end_steps
  @end_steps
end

#parent_stepsObject (readonly)

Returns the value of attribute parent_steps.



37
38
39
# File 'lib/epub/searcher/result.rb', line 37

def parent_steps
  @parent_steps
end

#start_stepsObject (readonly)

Returns the value of attribute start_steps.



37
38
39
# File 'lib/epub/searcher/result.rb', line 37

def start_steps
  @start_steps
end

Class Method Details

.aggregate_step_intersection(steps1, steps2) ⇒ Array<Array<Array>>

Returns Three arrays:

  1. “intersection” of steps1 and steps2. “intersection” here is not the term of mathmatics

  2. remaining steps of steps1

  3. remaining steps of steps2.

Examples:

Result.aggregate_step_intersection([a, b, c], [a, b, d]) # => [[a, b], [c], [d]]
Result.aggregate_step_intersection([a, b, c], [a, d, c]) # => [[a], [b, c], [d, c]]
# Note that c here is not included in the first element of returned value.

Parameters:

  • steps1 (Array<Step>, Array<Array>)
  • steps2 (Array<Step>, Array<Array>)

Returns:

  • (Array<Array<Array>>)

    Three arrays:

    1. “intersection” of steps1 and steps2. “intersection” here is not the term of mathmatics

    2. remaining steps of steps1

    3. remaining steps of steps2



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/epub/searcher/result.rb', line 18

def aggregate_step_intersection(steps1, steps2)
  intersection = []
  steps1_remaining = []
  steps2_remaining = []
  broken = false
  steps1.zip steps2 do |step1, step2|
    broken = true unless step1 && step2 && step1 == step2
    if broken
      steps1_remaining << step1 unless step1.nil?
      steps2_remaining << step2 unless step2.nil?
    else
      intersection << step1
    end
  end

  [intersection, steps1_remaining, steps2_remaining]
end

Instance Method Details

#==(other) ⇒ Object



53
54
55
56
# File 'lib/epub/searcher/result.rb', line 53

def ==(other)
  [@parent_steps + @start_steps.to_a] == [other.parent_steps + other.start_steps.to_a] and
    [@parent_steps + @end_steps.to_a] == [other.parent_steps + other.end_steps.to_a]
end

#to_cfiObject



46
47
48
49
50
51
# File 'lib/epub/searcher/result.rb', line 46

def to_cfi
  str = [@parent_steps, @start_steps, @end_steps].collect {|steps|
    steps ? steps.collect(&:to_cfi).join : nil
  }.compact.join(',')
  EPUB::CFI(str)
end