Class: Hackernews::HomeState

Inherits:
ApplicationState show all
Defined in:
app/state/home_state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ HomeState

Returns a new instance of HomeState.



19
20
21
22
23
24
25
# File 'app/state/home_state.rb', line 19

def initialize(**attributes)
  super
  @story_ids_by_feed = {}
  @stories_by_page = {}
  @articles_by_story_id = {}
  @failed_pages = {}
end

Instance Attribute Details

#articles_by_story_idObject

Returns the value of attribute articles_by_story_id.



16
17
18
# File 'app/state/home_state.rb', line 16

def articles_by_story_id
  @articles_by_story_id
end

#extracting_story_idObject

Returns the value of attribute extracting_story_id.



16
17
18
# File 'app/state/home_state.rb', line 16

def extracting_story_id
  @extracting_story_id
end

#failed_pagesObject

Returns the value of attribute failed_pages.



16
17
18
# File 'app/state/home_state.rb', line 16

def failed_pages
  @failed_pages
end

#loading_keyObject

Returns the value of attribute loading_key.



16
17
18
# File 'app/state/home_state.rb', line 16

def loading_key
  @loading_key
end

#stories_by_pageObject

Returns the value of attribute stories_by_page.



16
17
18
# File 'app/state/home_state.rb', line 16

def stories_by_page
  @stories_by_page
end

#story_ids_by_feedObject

Returns the value of attribute story_ids_by_feed.



16
17
18
# File 'app/state/home_state.rb', line 16

def story_ids_by_feed
  @story_ids_by_feed
end

Instance Method Details

#articleObject



55
56
57
# File 'app/state/home_state.rb', line 55

def article
  articles_by_story_id[reading_story_id]
end

#cached_page?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'app/state/home_state.rb', line 39

def cached_page?
  stories_by_page.key?(page_key)
end

#current_storiesObject



31
32
33
# File 'app/state/home_state.rb', line 31

def current_stories
  stories_by_page.fetch(page_key, [])
end

#current_story_idsObject



35
36
37
# File 'app/state/home_state.rb', line 35

def current_story_ids
  story_ids_by_feed.fetch(feed, [])
end

#failed_page?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/state/home_state.rb', line 43

def failed_page?
  failed_pages[page_key]
end

#page_key(feed_name = feed, page_number = page) ⇒ Object



27
28
29
# File 'app/state/home_state.rb', line 27

def page_key(feed_name = feed, page_number = page)
  "#{feed_name}:#{page_number.to_i}"
end

#reading?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'app/state/home_state.rb', line 59

def reading?
  reading_story_id.to_i.positive?
end

#reset_list_positionObject



67
68
69
70
71
# File 'app/state/home_state.rb', line 67

def reset_list_position
  self.selected_index = 0
  self.article_scroll = 0
  self.reading_story_id = nil
end

#selected_storyObject



47
48
49
# File 'app/state/home_state.rb', line 47

def selected_story
  current_stories[selected_index]
end

#selected_story=(story) ⇒ Object



51
52
53
# File 'app/state/home_state.rb', line 51

def selected_story=(story)
  self.selected_index = current_stories.index(story) || 0
end

#working?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/state/home_state.rb', line 63

def working?
  (loading && !cached_page?) || extracting_story_id.to_i.positive?
end