Class: PMS::TokenProxy

Inherits:
Proxy
  • Object
show all
Defined in:
lib/pms/proxy.rb

Instance Attribute Summary collapse

Attributes inherited from Proxy

#index, #pms, #results

Instance Method Summary collapse

Methods inherited from Proxy

#matches

Constructor Details

#initialize(pms, token) ⇒ TokenProxy

Returns a new instance of TokenProxy.



101
102
103
104
105
106
# File 'lib/pms/proxy.rb', line 101

def initialize(pms, token)
  super(pms)

  @results_with_positions = index.results_with_positions(token)
  @token, @results = token, @results_with_positions.keys
end

Instance Attribute Details

#results_with_positionsObject (readonly)

Returns the value of attribute results_with_positions.



99
100
101
# File 'lib/pms/proxy.rb', line 99

def results_with_positions
  @results_with_positions
end

#tokenObject (readonly)

Returns the value of attribute token.



99
100
101
# File 'lib/pms/proxy.rb', line 99

def token
  @token
end

Instance Method Details

#adjacent(token, distance = 1) ⇒ Object Also known as: adj, ^



131
132
133
# File 'lib/pms/proxy.rb', line 131

def adjacent(token, distance = 1)
  near(token, distance, true)
end

#near(token, distance = 1, order = false) ⇒ Object Also known as: %



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/pms/proxy.rb', line 108

def near(token, distance = 1, order = false)
  results1 = results_with_positions
  results2 = index.results_with_positions(token)

  doc_nums = results1.keys & results2.keys

  # TODO: i'm sure this can be simplified...
  doc_nums.delete_if { |doc_num|
    positions = results2[doc_num]

    !results1[doc_num].any? { |pos1|
      positions.find { |pos2|
        diff = pos2 - pos1
        order && diff < 0 ? break : diff.abs <= distance
      }
    }
  }

  apply_operator(:and, doc_nums)
end