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


40
41
42
# File 'lib/epub/searcher/result.rb', line 40

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


35
36
37
# File 'lib/epub/searcher/result.rb', line 35

def end_steps
  @end_steps
end

#parent_stepsObject (readonly)

Returns the value of attribute parent_steps


35
36
37
# File 'lib/epub/searcher/result.rb', line 35

def parent_steps
  @parent_steps
end

#start_stepsObject (readonly)

Returns the value of attribute start_steps


35
36
37
# File 'lib/epub/searcher/result.rb', line 35

def start_steps
  @start_steps
end

Class Method Details

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

Returns Thee 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>>)

    Thee 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


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

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


65
66
67
68
# File 'lib/epub/searcher/result.rb', line 65

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_cfi_sObject


59
60
61
62
63
# File 'lib/epub/searcher/result.rb', line 59

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

#to_xpath_and_offset(with_xmlns = false) ⇒ Object


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/epub/searcher/result.rb', line 44

def to_xpath_and_offset(with_xmlns=false)
  xpath = (@parent_steps + @start_steps).reduce('.') {|path, step|
    case step.type
    when :element
      path + '/%s*[%d]' % [with_xmlns ? 'xhtml:' : nil, step.index + 1]
    when :text
      path + '/text()[%s]' % [step.index + 1]
    else
      path
    end
  }

  [xpath, @start_steps.last.index]
end