forematter 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -25,6 +25,9 @@ module Forematter
25
25
  class UsageException < Exception
26
26
  end
27
27
 
28
+ class UnexpectedValue < Exception
29
+ end
30
+
28
31
  class << self
29
32
  attr_reader :root_command
30
33
  # attr_accessor :verbose
@@ -4,16 +4,25 @@ module Forematter
4
4
  class CommandRunner < ::Cri::CommandRunner
5
5
  def call
6
6
  run
7
+ exit 1 if @has_error
7
8
  rescue UsageException
8
- path = [command.supercommand]
9
- path.unshift(path[0].supercommand) until path[0].nil?
10
- super_usage = path[1..-1].map { |c| c.name + ' ' }.join
11
9
  $stderr.puts "usage: #{super_usage}#{command.usage}"
12
10
  exit 1
13
11
  end
14
12
 
15
13
  protected
16
14
 
15
+ def log_skip(file, msg)
16
+ $stderr.puts "#{super_usage}#{command.name}: #{file.filename}: #{msg}"
17
+ @has_error = 1
18
+ end
19
+
20
+ def super_usage
21
+ path = [command.supercommand]
22
+ path.unshift(path[0].supercommand) until path[0].nil?
23
+ path[1..-1].map { |c| c.name + ' ' }.join
24
+ end
25
+
17
26
  def field
18
27
  partition
19
28
  fail UsageException, 'Missing field name' unless @field
@@ -18,8 +18,8 @@ module Forematter::Commands
18
18
  class Add < Forematter::CommandRunner
19
19
  def run
20
20
  files.each do |file|
21
- old = file[field].to_ruby || []
22
- fail "#{field} is not an array" unless old.is_a?(Array)
21
+ old = file.key?(field) ? file[field].to_ruby : []
22
+ log_skip(file, "#{field} is not an array, unable to add") && next unless old.is_a?(Array)
23
23
  add = options[:'allow-dupes'] ? values : values.select { |v| !old.include?(v) }
24
24
  next if add.empty?
25
25
  add.each { |v| old << v }
@@ -29,8 +29,14 @@ module Forematter::Commands
29
29
  load_classifier
30
30
 
31
31
  puts 'Getting categories'
32
- categories_for(files_with(field)).each do |cat|
33
- bayes.add_category(cat.to_sym)
32
+ categories_for(files_with(field)).each { |cat| bayes.add_category(cat) }
33
+
34
+ if bayes.categories.empty?
35
+ $stderr.puts "No categories found in #{field}, unable to classify"
36
+ exit 1
37
+ else
38
+ found = bayes.categories.length
39
+ puts "#{found} #{found == 1 ? 'category' : 'categories'} found"
34
40
  end
35
41
 
36
42
  puts 'Training classifier'
@@ -54,7 +60,11 @@ module Forematter::Commands
54
60
  end
55
61
 
56
62
  def categories_for(files)
57
- files.map { |file| file[field].to_ruby }.reject(&:nil?).uniq
63
+ files
64
+ .map { |file| file[field].to_ruby }
65
+ .select { |f| f.is_a?(String) }
66
+ .uniq
67
+ .map(&:to_sym)
58
68
  end
59
69
 
60
70
  def files_to_classify
@@ -65,7 +75,10 @@ module Forematter::Commands
65
75
 
66
76
  def train(file)
67
77
  val = file[field].to_ruby
68
- fail 'Unable to classify by non-string fields' unless val.is_a?(String)
78
+ unless val.is_a?(String)
79
+ skip file, "unable to train, #{field} is not a string"
80
+ return
81
+ end
69
82
  bayes.train(val.to_sym, file.content)
70
83
  end
71
84
 
@@ -25,7 +25,11 @@ module Forematter::Commands
25
25
 
26
26
  files_with(field).each do |file|
27
27
  old = file[field].to_ruby
28
- val = cleanup(old)
28
+ begin
29
+ val = cleanup(old)
30
+ rescue Forematter::UnexpectedValue => e
31
+ log_skip(file, e.message) && next
32
+ end
29
33
  unless val == old
30
34
  file[field] = val
31
35
  file.write
@@ -47,7 +51,7 @@ module Forematter::Commands
47
51
  def cleanup(val)
48
52
  val = val.dup
49
53
  return cleanup_array(val) if val.is_a?(Array)
50
- fail 'Unable to sort non-array values' if options[:sort]
54
+ fail Forematter::UnexpectedValue, "#{field} is not an array" if options[:sort]
51
55
  options.keys.each do |option|
52
56
  val = val.method(CLEANUP_MAP[option]).call if CLEANUP_MAP.key?(option) && options[option]
53
57
  end
@@ -10,7 +10,7 @@ EOS
10
10
  module Forematter::Commands
11
11
  class List < Forematter::CommandRunner
12
12
  def run
13
- puts tags.uniq.sort_by(&:downcase).join("\n")
13
+ puts tags.uniq.compact.map(&:to_s).sort_by(&:downcase).join("\n")
14
14
  end
15
15
 
16
16
  protected
@@ -17,7 +17,7 @@ module Forematter::Commands
17
17
 
18
18
  files_with(field).each do |file|
19
19
  old = file[field].to_ruby
20
- fail "#{field} is not an array" unless old.is_a?(Array)
20
+ log_skip(file, "#{field} is not an array") && next unless old.is_a?(Array)
21
21
 
22
22
  # Continue unless unless field had one of the values to remove
23
23
  next if (old & dups).empty?
@@ -15,7 +15,7 @@ module Forematter::Commands
15
15
  def run
16
16
  files_with(field).each do |file|
17
17
  old = file[field].to_ruby
18
- fail "#{field} is not an array" unless old.is_a?(Array)
18
+ log_skip(file, "#{field} is not an array") && next unless old.is_a?(Array)
19
19
 
20
20
  # Continue unless old contains elements of values
21
21
  next if (old & values).empty?
@@ -5,7 +5,7 @@ module Forematter
5
5
  extend Forwardable
6
6
 
7
7
  def initialize(filename)
8
- fail "File not found: #{filename}" unless File.exist?(filename)
8
+ fail Forematter::UnexpectedValue, "File not found: #{filename}" unless File.file?(filename)
9
9
  @filename = filename
10
10
  end
11
11
 
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Forematter
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forematter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -139,18 +139,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
139
139
  - - ! '>='
140
140
  - !ruby/object:Gem::Version
141
141
  version: '0'
142
- segments:
143
- - 0
144
- hash: 2379320938142761946
145
142
  required_rubygems_version: !ruby/object:Gem::Requirement
146
143
  none: false
147
144
  requirements:
148
145
  - - ! '>='
149
146
  - !ruby/object:Gem::Version
150
147
  version: '0'
151
- segments:
152
- - 0
153
- hash: 2379320938142761946
154
148
  requirements: []
155
149
  rubyforge_project:
156
150
  rubygems_version: 1.8.23