SHARE
TWEET

Untitled

a guest Aug 14th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class TextField
  2.   attr_accessor :value
  3.  
  4.   def initialize
  5.     @value = ""
  6.   end
  7. end
  8.  
  9. class TextFieldWithObserver < TextField
  10.   attr_reader :observers
  11.  
  12.   def initialize
  13.     @observers = []
  14.     super
  15.   end
  16.  
  17.   def value=(value)
  18.     super
  19.     observers.each { |observer| observer.text_field_changed }
  20.   end
  21. end
  22.  
  23. class List
  24.   attr_accessor :words
  25.  
  26.   def initialize(words)
  27.     @words = words
  28.   end
  29. end
  30.  
  31. class ListSearchMediator
  32.  
  33.   def initialize(text_field, list)
  34.     @text_field = text_field
  35.     @text_field.observers << self
  36.     @list = list
  37.   end
  38.  
  39.   def text_field_changed
  40.     @list.words = @list.words.inject([]) do |narrowed_list, word|
  41.       if word.start_with?(@text_field.value)
  42.         narrowed_list.push(word)
  43.       end
  44.       narrowed_list
  45.     end
  46.   end
  47. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top