Class: Get::Smart::Logic

Inherits:
Object
  • Object
show all
Defined in:
lib/get/smart/logic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogic

Returns a new instance of Logic.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/get/smart/logic.rb', line 4

def initialize
  validate_level

  @topics = Get::Smart::Topics.all
  @memory = Get::Smart::Memory.new

  @collection = Get::Smart::Collection.new(@topics)
  @files = filter_by_level(collection.files)
  @files = filter_by_learning_path(@files)

  Get::Smart.log("Files before filtering: #{collection.files.size}")
  Get::Smart.log("Files after filtering: #{files.size}")
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



2
3
4
# File 'lib/get/smart/logic.rb', line 2

def collection
  @collection
end

#filesObject (readonly)

Returns the value of attribute files.



2
3
4
# File 'lib/get/smart/logic.rb', line 2

def files
  @files
end

#memoryObject (readonly)

Returns the value of attribute memory.



2
3
4
# File 'lib/get/smart/logic.rb', line 2

def memory
  @memory
end

Instance Method Details

#callObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/get/smart/logic.rb', line 18

def call
  return unless show?

  @random_file = random_file

  if @random_file.nil? && files.any?
    memory.reset
    @random_file = random_file
  end

  Get::Smart.log("Getting random file: #{@random_file}")
  Get::Smart::Tip.print_tip(@random_file)
  memory.write(@random_file)
end

#current_levelObject



60
61
62
# File 'lib/get/smart/logic.rb', line 60

def current_level
  Array.wrap(Get::Smart.level).map { |level| level.to_sym }
end

#current_level_regexpObject



64
65
66
67
# File 'lib/get/smart/logic.rb', line 64

def current_level_regexp
  # to match /beginner/ or /beginner|middle/
  Regexp.new("/" + current_level.join("|") + "(/|$)")
end

#filter_by_level(files) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/get/smart/logic.rb', line 52

def filter_by_level(files)
  if current_level == [ :any ]
    files
  else
    files.select { |file| file =~ current_level_regexp }
  end
end

#frequency_datetimeObject



48
49
50
# File 'lib/get/smart/logic.rb', line 48

def frequency_datetime
  Get::Smart::FREQUENCIES[Get::Smart.frequency].call.ago
end

#show?Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/get/smart/logic.rb', line 33

def show?
  last_shown_datetime = memory.last_shown_datetime

  Get::Smart.log("Level: #{current_level}")
  Get::Smart.log("Last shown datetime: #{last_shown_datetime}")
  Get::Smart.log("Frequency: #{Get::Smart.frequency}")
  Get::Smart.log("Frequency datetime: #{frequency_datetime}")

  result = last_shown_datetime.nil? || last_shown_datetime < frequency_datetime

  Get::Smart.log("Show? -> #{result}")

  result
end