Class: Punch

Inherits:
Object
  • Object
show all
Defined in:
lib/config.rb

Defined Under Namespace

Classes: Option

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.configObject


12
13
14
15
# File 'lib/config.rb', line 12

def config
  init_and_load_from_config_file if @config.nil?
  @config
end

.optionsObject

Returns the value of attribute options


5
6
7
# File 'lib/config.rb', line 5

def options
  @options
end

Class Method Details

.config_fileObject


32
33
34
# File 'lib/config.rb', line 32

def config_file
  "#{Dir.home}/.punchrc"
end

.configure {|config| ... } ⇒ Object

Yields:


96
97
98
# File 'lib/config.rb', line 96

def configure
  yield config
end

.init_and_load_from_config_fileObject


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/config.rb', line 17

def init_and_load_from_config_file
  @config = new
  return unless File.exist?(config_file)
  begin
    load config_file
  rescue Exception => e
    message = "Something went wrong while trying to load ~/.punchrc:\n".
      highlighted
    message << "\n#{e}\n\n"
    message << "Proceeding with whatever settings could be loaded.\n".
      highlighted
    puts message
  end
end

.load_card(card) ⇒ Object


36
37
38
39
40
41
42
43
44
45
# File 'lib/config.rb', line 36

def load_card(card)
  card_config = config.cards.fetch(card.to_sym) do
    puts "The card \"#{card}\" doesn't exist".highlighted
    exit
  end

  card_config.each do |k, v|
    config.send("#{k}=", v)
  end
end

.option(opt, desc, default_value, args = {}) ⇒ Object

Generates smart accessor and predicate methods and remembers options to inject them into .punchrc.

Examples:

Generating a boolean option with default value `false`


option :adult                            # Name of the option
  "Whether you have hair on your face.", # Short description
  false                                  # Default value

# Generates following methods on the config instance:
Punch.config.adult        # => false
Punch.config.adult?       # => false
Punch.config.adult = true
Punch.config.adult?       # => true

# And will add following option to .punchrc with the given description
# as a comment:

# Whether you have hair on your face.
config.adult = false

Parameters:

  • args (Hash) (defaults to: {})

Options Hash (args):

  • :hidden (Boolean) — default: false

    whether to hide this option .punchrc.

  • :path (Boolean) — default: false

    whether to treat the values as file paths. Punch will for instance substitute '~' with ENV.


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/config.rb', line 74

def option(opt, desc, default_value, args = {})
  options << Option.new(opt, desc) unless args.fetch(:hidden, false)

  if args.fetch(:path, false)
    define_method "#{opt}=" do |opt_value|
      instance_variable_set "@#{opt}", opt_value.to_s.absolute_path
    end
  else
    attr_writer opt
  end

  define_method opt do
    value = instance_variable_get("@#{opt}")
    return value unless value.nil?
    default_value
  end

  define_method "#{opt}?" do
    send(opt)
  end
end

Instance Method Details

#active_cardSymbol, NilClass

Returns:

  • (Symbol, NilClass)

225
226
227
# File 'lib/config.rb', line 225

option :active_card,
"Which card to load by default. Set this to nil to load no card.",
nil

#cardsHash

Returns:

  • (Hash)

220
221
222
# File 'lib/config.rb', line 220

option :cards,
"Register different punch cards.",
{}

#clear_buffer_before_punchBoolean

Returns:

  • (Boolean)

190
191
192
# File 'lib/config.rb', line 190

option :clear_buffer_before_punch,
"Clear terminal buffer before printing month.",
false

#colors_enabledBoolean

Returns:

  • (Boolean)

155
156
157
# File 'lib/config.rb', line 155

option :colors_enabled,
"Whether to color certain output.",
true

#config_fileObject


240
241
242
# File 'lib/config.rb', line 240

def config_file
  self.class.config_file
end

#debugBoolean

Returns:

  • (Boolean)

195
196
197
# File 'lib/config.rb', line 195

option :debug,
"Print stack trace instead of user friendly hint.",
false

#default_argsString

Returns:


120
121
122
# File 'lib/config.rb', line 120

option :default_args,
"Arguments to pass by default. e.g. '-y' to always punch yesterday.",
""

#generate_config_fileObject


248
249
250
# File 'lib/config.rb', line 248

def generate_config_file
  File.open(config_file, "w") { |f| f.write config_string }
end

#group_weeks_in_interactive_modeBoolean

Returns:

  • (Boolean)

170
171
172
# File 'lib/config.rb', line 170

option :group_weeks_in_interactive_mode,
"Whether to add padding to group by week in the interactive editor.",
true

#hand_in_dateFixnum

Returns:

  • (Fixnum)

135
136
137
# File 'lib/config.rb', line 135

option :hand_in_date,
"After which day punch should generate the next month's BRF file.",
20

#highlight_color_codeFixnum

Returns:

  • (Fixnum)

160
161
162
# File 'lib/config.rb', line 160

option :highlight_color_code,
"Which color to use for highlighting important text.",
35

#hourly_payFixnum

Returns:

  • (Fixnum)

125
126
127
# File 'lib/config.rb', line 125

option :hourly_pay,
"How much you earn per hour.",
0

#hours_folderString

Returns:


104
105
106
107
# File 'lib/config.rb', line 104

option :hours_folder,
"Where to look for the BRF files.",
File.expand_path('../hours', File.dirname(__FILE__)),
:path => true

#mailer_configHash

Returns:

  • (Hash)

206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/config.rb', line 206

option :mailer_config,
"BRFMailer configurations.",
{
  :smtp_domain => "example.com",
  :smtp_server => "smtp.example.com",
  :smtp_port   => 465,
  :smtp_user   => "[email protected]",
  :smtp_pw     => "gary4ever",
  :receiver    => "[email protected]",
  :bcc         => "",
  :body        => "Hi Mr Krabs, you'll find my hours attached. Cheers S."
}

#monthly_goalFixnum

Returns:

  • (Fixnum)

140
141
142
# File 'lib/config.rb', line 140

option :monthly_goal,
"How many hours you want to work per month.",
68

#nameString

Returns:


110
111
112
# File 'lib/config.rb', line 110

option :name,
"Your full name.",
"Spongebob Schwammkopf"

#out#puts, #print

Returns:

  • (#puts, #print)

230
231
232
233
# File 'lib/config.rb', line 230

option :out,
"Where to output stuff.",
STDOUT,
:hidden => true

#punch_now_minute_precisionFixnum

Returns:

  • (Fixnum)

180
181
182
# File 'lib/config.rb', line 180

option :punch_now_minute_precision,
"What precision (minutes) to round to.",
5

#punch_now_rounderSymbol

Returns:

  • (Symbol)

175
176
177
# File 'lib/config.rb', line 175

option :punch_now_rounder,
"Rounding strategy applied when substituting \"now\" (:fair or :exact).",
:fair

#regenerate_punchrc_after_udpateBoolean

Returns:

  • (Boolean)

185
186
187
# File 'lib/config.rb', line 185

option :regenerate_punchrc_after_udpate,
"Whether to automatically regenerate ~/.punchrc with punch --update.",
false

#reset!Object


244
245
246
# File 'lib/config.rb', line 244

def reset!
  options.each { |option| send("#{option.name}=", nil) }
end

#system_rubyString

Returns:


200
201
202
203
# File 'lib/config.rb', line 200

option :system_ruby,
"Which ruby command to use to execute subcommands.",
"ruby",
:path => true

#text_editorString

Returns:


130
131
132
# File 'lib/config.rb', line 130

option :text_editor,
"Which terminal application to use to edit files.",
"open"

#titleString

Returns:


115
116
117
# File 'lib/config.rb', line 115

option :title,
"Title that appears in the BRF file.",
""

#today_color_codeFixnum

Returns:

  • (Fixnum)

165
166
167
# File 'lib/config.rb', line 165

option :today_color_code,
"Which color to use for highlighting the current date.",
34

#totals_formatSymbol

Returns:

  • (Symbol)

150
151
152
# File 'lib/config.rb', line 150

option :totals_format,
"How to render totals (:digital or :decimal).",
:digital

#workdaysArray

Returns:

  • (Array)

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

option :workdays,
"Which days you work on. Used for stats.",
[:monday, :tuesday, :wednesday, :thursday, :friday]