Class: ContactManager::ContactManagerPresenter

Inherits:
Object
  • Object
show all
Defined in:
lib/glimmer-dsl-opal/samples/elaborate/contact_manager/contact_manager_presenter.rb

Constant Summary collapse

@@contact_attributes =
[:first_name, :last_name, :email]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contact_repository = nil) ⇒ ContactManagerPresenter

Returns a new instance of ContactManagerPresenter.



9
10
11
12
# File 'lib/glimmer-dsl-opal/samples/elaborate/contact_manager/contact_manager_presenter.rb', line 9

def initialize(contact_repository = nil)
  @contact_repository = contact_repository || ContactRepository.new
  @results = []
end

Instance Attribute Details

#resultsObject

Returns the value of attribute results.



5
6
7
# File 'lib/glimmer-dsl-opal/samples/elaborate/contact_manager/contact_manager_presenter.rb', line 5

def results
  @results
end

Instance Method Details

#findObject



18
19
20
21
22
23
24
25
26
# File 'lib/glimmer-dsl-opal/samples/elaborate/contact_manager/contact_manager_presenter.rb', line 18

def find
  filter_map = {}
  @@contact_attributes.each do |attribute_name|
    filter_map[attribute_name] = self.send(attribute_name) if self.send(attribute_name)
  end
  self.results = @contact_repository.find(filter_map)
  @sort_attribute_name = nil
  @sort_direction_ascending = nil
end

#listObject



14
15
16
# File 'lib/glimmer-dsl-opal/samples/elaborate/contact_manager/contact_manager_presenter.rb', line 14

def list
  self.results = @contact_repository.find({})
end

#toggle_sort(attribute_name) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/glimmer-dsl-opal/samples/elaborate/contact_manager/contact_manager_presenter.rb', line 28

def toggle_sort(attribute_name)
  @sort_attribute_name = attribute_name
  @sort_direction_ascending = !@sort_direction_ascending
  sorted_results = self.results.sort_by {|contact| contact.send(attribute_name).downcase}
  sorted_results = sorted_results.reverse unless @sort_direction_ascending
  self.results = sorted_results
end