Class: StickyFlag::ThorApp

Inherits:
Thor
  • Object
show all
Includes:
Configuration, Database, ExternalCmds, Paths, TagFactory, Thor::Actions
Defined in:
lib/stickyflag.rb

Constant Summary

Constants included from TagFactory

StickyFlag::TagFactory::TAG_EXTENSIONS, StickyFlag::TagFactory::TAG_MODULES

Constants included from ExternalCmds

ExternalCmds::EXTERNAL_CMDS

Constants included from Configuration

Configuration::DEFAULT_CONFIG

Instance Method Summary collapse

Methods included from Database

#clear_database_tags, #create_tables, #drop_tables, #files_for_tags, #get_file_id, #get_tag_id, #load_database, #set_database_tag, #tag_list, #unset_database_tag, #update_database_from_files

Methods included from TagFactory

#available_tagging_extensions, #call_tag_method, #clear_tags_for, #get_tags_for, #set_tag_for, #unset_tag_for

Methods included from ExternalCmds

#find_external_cmds

Methods included from Configuration

#dump_config, #get_config, #load_config!, #reset_config!, #save_config!, #set_config

Methods included from Paths

#config_path, #database_path

Constructor Details

#initialize(*args) ⇒ ThorApp

Returns a new instance of ThorApp.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/stickyflag.rb', line 41

def initialize(*args)
  super

  if options.color?
    self.shell = Thor::Shell::Color.new
  end

  load_config!
  find_external_cmds
  load_database
end

Instance Method Details

#clear(*files) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/stickyflag.rb', line 181

def clear(*files)
  if files.empty?
    raise Thor::Error.new("stickyflag clear requires at least 1 argument: \"stickyflag clear [FILES]\"")
  end

  files.each do |file_name|
    unless File.exist? file_name
      say_status :error, "File #{file_name} does not exist", :red unless options.force? || options.quiet?
      return
    end
  
    clear_tags_for file_name
    say_status :success, "Tags cleared for #{file_name}", :green unless options.quiet?
  end
end

#config(value = nil) ⇒ Object



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

def config(value = nil)
  if options.reset?
    reset_config!
    return
  end

  if options.list? || (options[:key].nil? && value.nil?)
    dump_config
    return
  end

  if options[:key].nil?
    raise Thor::Error.new("ERROR: Cannot set a value without a key specified")
  end

  if value.nil?
    value = get_config options[:key]
    say "#{options[:key]}: '#{value}'"
    return
  end

  set_config options[:key], value
  say "'#{options[:key]}' set to '#{value}'" unless options.quiet?

  save_config!
end

#find(*tags) ⇒ Object



231
232
233
234
235
236
237
238
239
240
# File 'lib/stickyflag.rb', line 231

def find(*tags)
  if tags.empty?
    raise Thor::Error.new("stickyflag find requires at least 1 argument: \"stickyflag find [TAG] [...]\"")
  end
  tags.each { |t| check_tag t }

  files_for_tags(tags).each do |file|
    say file
  end
end

#get(*files) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/stickyflag.rb', line 103

def get(*files)
  if files.empty?
    raise Thor::Error.new("stickyflag get requires at least 1 argument: \"stickyflag get [FILES]\"")
  end

  files.each do |file_name|
    unless File.exist? file_name
      say_status :error, "File #{file_name} does not exist", :red unless options.force? || options.quiet?
      next
    end
  
    tags = get_tags_for file_name
    if tags.empty?
      say "#{file_name}: no tags" unless options.force? || options.quiet?
      next
    else
      say "#{file_name}: #{tags.join(', ')}"
      next
    end
  end
end

#set(file_name, tag) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/stickyflag.rb', line 132

def set(file_name, tag)
  check_tag tag

  unless File.exist? file_name
    say_status :error, "File #{file_name} does not exist", :red unless options.quiet?
    return
  end

  set_tag_for file_name, tag

  tags = get_tags_for file_name
  say_status :success, "New tags for #{file_name}: #{tags.join(', ')}", :green unless options.quiet?
end

#tagsObject



216
217
218
219
220
221
222
223
224
# File 'lib/stickyflag.rb', line 216

def tags
  say "Tags currently in use:" unless options.quiet?
  padding = ''
  padding = '   ' unless options.quiet?

  tag_list.each do |t|
    say "#{padding}#{t}"
  end
end

#unset(file_name, tag) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/stickyflag.rb', line 153

def unset(file_name, tag)
  check_tag tag

  unless File.exist? file_name
    say_status :error, "File #{file_name} does not exist", :red unless options.quiet?
    return
  end

  unset_tag_for file_name, tag

  # Unsetting a tag might leave us with no tags at all, which makes this more
  # complicated than the #set behavior
  unless options.quiet?
    tags = get_tags_for file_name
    if tags.empty?
      say_status :success, "New tags for #{file_name}: no tags", :green
    else
      say_status :success, "New tags for #{file_name}: #{tags.join(', ')}", :green
    end
  end
end

#updateObject



203
204
205
206
207
208
# File 'lib/stickyflag.rb', line 203

def update
  root = get_config(:root).strip
  root = '.' if root.empty? || root.nil?

  update_database_from_files root
end