Class: LooseTightDictionary::Wrapper
- Inherits:
-
Object
- Object
- LooseTightDictionary::Wrapper
- Defined in:
- lib/loose_tight_dictionary/wrapper.rb
Overview
Wrappers are the tokens that are passed around when doing scoring and optimizing.
Constant Summary collapse
- WORD_BOUNDARY =
%r{\s*\b\s*}
Instance Attribute Summary collapse
-
#loose_tight_dictionary ⇒ Object
readonly
:nodoc: all.
-
#read ⇒ Object
readonly
Returns the value of attribute read.
-
#record ⇒ Object
readonly
Returns the value of attribute record.
Instance Method Summary collapse
-
#initialize(loose_tight_dictionary, record, read = nil) ⇒ Wrapper
constructor
A new instance of Wrapper.
- #inspect ⇒ Object
- #render ⇒ Object (also: #to_str)
- #rendered? ⇒ Boolean
- #similarity(other) ⇒ Object
- #variants ⇒ Object
- #words ⇒ Object
Constructor Details
#initialize(loose_tight_dictionary, record, read = nil) ⇒ Wrapper
Returns a new instance of Wrapper.
8 9 10 11 12 |
# File 'lib/loose_tight_dictionary/wrapper.rb', line 8 def initialize(loose_tight_dictionary, record, read = nil) @loose_tight_dictionary = loose_tight_dictionary @record = record @read = read end |
Instance Attribute Details
#loose_tight_dictionary ⇒ Object (readonly)
:nodoc: all
4 5 6 |
# File 'lib/loose_tight_dictionary/wrapper.rb', line 4 def loose_tight_dictionary @loose_tight_dictionary end |
#read ⇒ Object (readonly)
Returns the value of attribute read.
6 7 8 |
# File 'lib/loose_tight_dictionary/wrapper.rb', line 6 def read @read end |
#record ⇒ Object (readonly)
Returns the value of attribute record.
5 6 7 |
# File 'lib/loose_tight_dictionary/wrapper.rb', line 5 def record @record end |
Instance Method Details
#inspect ⇒ Object
14 15 16 |
# File 'lib/loose_tight_dictionary/wrapper.rb', line 14 def inspect "#<Wrapper render=#{render} variants=#{variants.length}>" end |
#render ⇒ Object Also known as: to_str
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/loose_tight_dictionary/wrapper.rb', line 18 def render return @render if rendered? str = case read when ::Proc read.call record when ::Symbol if record.respond_to?(read) record.send read else record[read] end when ::NilClass record else record[read] end.to_s.dup loose_tight_dictionary.stop_words.each do |stop_word| stop_word.apply! str end str.strip! @render = str.freeze @rendered = true @render end |
#rendered? ⇒ Boolean
63 64 65 |
# File 'lib/loose_tight_dictionary/wrapper.rb', line 63 def rendered? @rendered == true end |
#similarity(other) ⇒ Object
50 51 52 |
# File 'lib/loose_tight_dictionary/wrapper.rb', line 50 def similarity(other) Similarity.new self, other end |
#variants ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/loose_tight_dictionary/wrapper.rb', line 54 def variants @variants ||= loose_tight_dictionary.tighteners.inject([ render ]) do |memo, tightener| if tightener.apply? render memo.push tightener.apply(render) end memo end.uniq end |
#words ⇒ Object
46 47 48 |
# File 'lib/loose_tight_dictionary/wrapper.rb', line 46 def words @words ||= render.split(WORD_BOUNDARY) end |