Class: Reviewer::Tool

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/reviewer/tool.rb,
lib/reviewer/tool/settings.rb
more...

Overview

Provides an instance of a specific tool for accessing its settings and run history

Defined Under Namespace

Classes: Settings

Constant Summary collapse

SIX_HOURS_IN_SECONDS =

In general, Reviewer tries to save time where it can. In the case of the “prepare” command used by some tools to retrieve data, it only runs it occasionally in order to save time. This is the default window that it uses to determine if the tool’s preparation step should be considered stale and needs to be rerun. Frequent enough that it shouldn’t get stale, but infrequent enough that it’s not cumbersome.

60 * 60 * 6

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tool_key) ⇒ Tool

Create an instance of a tool

Parameters:

  • tool_key (Symbol)

    the key to the tool from the configuration file

[View source]

41
42
43
# File 'lib/reviewer/tool.rb', line 41

def initialize(tool_key)
  @settings = Settings.new(tool_key)
end

Instance Attribute Details

#historyObject (readonly)

Returns the value of attribute history.


20
21
22
# File 'lib/reviewer/tool.rb', line 20

def history
  @history
end

#settingsObject (readonly)

Returns the value of attribute settings.


20
21
22
# File 'lib/reviewer/tool.rb', line 20

def settings
  @settings
end

Instance Method Details

#average_time(command) ⇒ Object

[View source]

107
108
109
110
111
# File 'lib/reviewer/tool.rb', line 107

def average_time(command)
  times = get_timing(command)

  times.any? ? times.sum / times.size : 0
end

#command?(command_type) ⇒ Boolean

Determines whether a tool has a specific command type configured

Parameters:

  • command_type (Symbol)

    one of the available command types defined in Command::TYPES

Returns:

  • (Boolean)

    true if the command type is configured and not blank

[View source]

58
59
60
# File 'lib/reviewer/tool.rb', line 58

def command?(command_type)
  commands.key?(command_type) && !commands[command_type].nil?
end

#eql?(other) ⇒ Boolean Also known as: ==

Determines if two tools are equal

Parameters:

  • other (Tool)

    the tool to compare to the current instance

Returns:

  • (Boolean)

    true if the settings match

[View source]

153
154
155
# File 'lib/reviewer/tool.rb', line 153

def eql?(other)
  settings == other.settings
end

#formattable?Boolean

Determines if the tool can run a ‘format` command

Returns:

  • (Boolean)

    true if there is a non-blank ‘format` command configured

[View source]

86
87
88
# File 'lib/reviewer/tool.rb', line 86

def formattable?
  command?(:format)
end

#get_timing(command) ⇒ Object

[View source]

113
114
115
# File 'lib/reviewer/tool.rb', line 113

def get_timing(command)
  Reviewer.history.get(key, command.raw_string) || []
end

Returns the text for the install link if available

Returns:

  • (String, nil)

    the link if it exists, nil otherwise

[View source]

145
146
147
# File 'lib/reviewer/tool.rb', line 145

def install_link
  install_link? ? links.fetch(:install) : nil
end

#install_link?Boolean

Convenience method for determining if a tool has a configured install link

Returns:

  • (Boolean)

    true if there is an ‘install` key under links and the value isn’t blank

[View source]

138
139
140
# File 'lib/reviewer/tool.rb', line 138

def install_link?
  links.key?(:install) && !links[:install].nil?
end

#installable?Boolean

Determines if the tool can run a ‘install` command

Returns:

  • (Boolean)

    true if there is a non-blank ‘install` command configured

[View source]

65
66
67
# File 'lib/reviewer/tool.rb', line 65

def installable?
  command?(:install)
end

#last_prepared_atTime

Specifies when the tool last had it’s ‘prepare` command run

Returns:

  • (Time)

    timestamp of when the ‘prepare` command was last run

[View source]

93
94
95
96
97
# File 'lib/reviewer/tool.rb', line 93

def last_prepared_at
  date_string = Reviewer.history.get(key, :last_prepared_at)

  date_string == '' || date_string.nil? ? nil : DateTime.parse(date_string).to_time
end

#last_prepared_at=(last_prepared_at) ⇒ DateTime

Sets the timestamp for when the tool last ran its ‘prepare` command

Parameters:

  • last_prepared_at (DateTime)

    the value to record for when the ‘prepare` command last ran

Returns:

  • (DateTime)

    timestamp of when the ‘prepare` command was last run

[View source]

103
104
105
# File 'lib/reviewer/tool.rb', line 103

def last_prepared_at=(last_prepared_at)
  Reviewer.history.set(key, :last_prepared_at, last_prepared_at.to_s)
end

#preparable?Boolean

Determines if the tool can run a ‘prepare` command

Returns:

  • (Boolean)

    true if there is a non-blank ‘prepare` command configured

[View source]

72
73
74
# File 'lib/reviewer/tool.rb', line 72

def preparable?
  command?(:prepare)
end

#prepare?Boolean

For determining if the tool should run it’s prepration command. It will only be run both if the tool has a preparation command, and the command hasn’t been run 6 hours

Returns:

  • (Boolean)

    true if the tool has a configured ‘prepare` command that hasn’t been run in the last 6 hours

[View source]

50
51
52
# File 'lib/reviewer/tool.rb', line 50

def prepare?
  preparable? && stale?
end

#record_timing(command, time) ⇒ Object

[View source]

117
118
119
120
121
122
123
# File 'lib/reviewer/tool.rb', line 117

def record_timing(command, time)
  return if time.nil?

  timing = get_timing(command).take(4) << time.round(2)

  Reviewer.history.set(key, command.raw_string, timing)
end

#reviewable?Boolean

Determines if the tool can run a ‘review` command

Returns:

  • (Boolean)

    true if there is a non-blank ‘review` command configured

[View source]

79
80
81
# File 'lib/reviewer/tool.rb', line 79

def reviewable?
  command?(:review)
end

#stale?Boolean

Determines whether the ‘prepare` command was run recently enough

Returns:

  • (Boolean)

    true if a prepare command exists, a timestamp exists, and it was run more than six hours ago

[View source]

129
130
131
132
133
# File 'lib/reviewer/tool.rb', line 129

def stale?
  return false unless preparable?

  last_prepared_at.nil? || last_prepared_at < Time.now - SIX_HOURS_IN_SECONDS
end