bibsync 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,7 @@ require 'nokogiri'
2
2
  require 'shellwords'
3
3
  require 'date'
4
4
  require 'pathname'
5
+ require 'forwardable'
5
6
  require 'bibsync/version'
6
7
  require 'bibsync/utils'
7
8
  require 'bibsync/log'
@@ -7,8 +7,8 @@ module BibSync
7
7
  SliceSize = 20
8
8
 
9
9
  def initialize(options)
10
- raise 'Option :bib is required' unless @bib = options[:bib]
11
- raise 'Option :dir is required' unless @dir = options[:dir]
10
+ raise 'Option --bib is required' unless @bib = options[:bib]
11
+ raise 'Option --dir is required' unless @dir = options[:dir]
12
12
  @update = options[:update]
13
13
  end
14
14
 
@@ -5,7 +5,7 @@ module BibSync
5
5
  include Log
6
6
 
7
7
  def initialize(options)
8
- raise 'Option :bib is required' unless @bib = options[:bib]
8
+ raise 'Option --bib is required' unless @bib = options[:bib]
9
9
  @force = options[:resync]
10
10
  end
11
11
 
@@ -27,7 +27,7 @@ module BibSync
27
27
 
28
28
  def determine_arxiv_and_doi(entry)
29
29
  if file = entry.file
30
- if file[:type] == :PDF && !entry[:arxiv] && !entry[:doi]
30
+ if file[:type] == 'PDF' && !entry[:arxiv] && !entry[:doi]
31
31
  debug('Searching for arXiv or doi identifier in pdf file', key: entry)
32
32
  text = `pdftotext -f 1 -l 2 #{Shellwords.escape file[:path]} - 2>/dev/null`
33
33
  entry[:arxiv] = $1 if text =~ /arXiv:\s*([\w\.\/\-]+)/
@@ -7,8 +7,8 @@ module BibSync
7
7
  include Utils
8
8
 
9
9
  def initialize(options)
10
- raise 'Option :fetch is required' unless @fetch = options[:fetch]
11
- raise 'Option :dir is required' unless @dir = options[:dir]
10
+ raise 'Option --fetch is required' unless @fetch = options[:fetch]
11
+ raise 'Option --dir is required' unless @dir = options[:dir]
12
12
  end
13
13
 
14
14
  def run
@@ -5,8 +5,8 @@ module BibSync
5
5
  include Utils
6
6
 
7
7
  def initialize(options)
8
- raise 'Option :bib is required' unless @bib = options[:bib]
9
- raise 'Option :citedbyme is required' unless @dir = options[:citedbyme]
8
+ raise 'Option --bib is required' unless @bib = options[:bib]
9
+ raise 'Option --citedbyme is required' unless @dir = options[:citedbyme]
10
10
  raise "#{@dir} is not a directory" unless File.directory?(@dir)
11
11
  end
12
12
 
@@ -5,7 +5,7 @@ module BibSync
5
5
  include Log
6
6
 
7
7
  def initialize(options)
8
- raise 'Option :bib is required' unless @bib = options[:bib]
8
+ raise 'Option --bib is required' unless @bib = options[:bib]
9
9
  end
10
10
 
11
11
  def run
@@ -7,8 +7,8 @@ module BibSync
7
7
  FileTypes = %w(djvu pdf ps)
8
8
 
9
9
  def initialize(options)
10
- raise 'Option :bib is required' unless @bib = options[:bib]
11
- raise 'Option :dir is required' unless @dir = options[:dir]
10
+ raise 'Option --bib is required' unless @bib = options[:bib]
11
+ raise 'Option --dir is required' unless @dir = options[:dir]
12
12
  end
13
13
 
14
14
  def run
@@ -5,7 +5,7 @@ module BibSync
5
5
  include Log
6
6
 
7
7
  def initialize(options)
8
- raise 'Option :bib is required' unless @bib = options[:bib]
8
+ raise 'Option --bib is required' unless @bib = options[:bib]
9
9
  @force = options[:resync]
10
10
  end
11
11
 
@@ -5,7 +5,7 @@ module BibSync
5
5
  include Log
6
6
 
7
7
  def initialize(options)
8
- raise 'Bibliography must be set' unless @bib = options[:bib]
8
+ raise 'Option --bib is required' unless @bib = options[:bib]
9
9
  end
10
10
 
11
11
  def run
@@ -1,9 +1,12 @@
1
1
  module BibSync
2
2
  class Bibliography
3
3
  include Enumerable
4
+ extend Forwardable
4
5
 
5
6
  attr_reader :file
6
7
  attr_accessor :save_hook
8
+ def_delegators :@entries, :empty?, :size
9
+ def_delegator :@entries, :each_value, :each
7
10
 
8
11
  def initialize(file = nil)
9
12
  @entries, @save_hook = {}, nil
@@ -18,14 +21,6 @@ module BibSync
18
21
  @dirty = true
19
22
  end
20
23
 
21
- def empty?
22
- @entries.empty?
23
- end
24
-
25
- def size
26
- @entries.size
27
- end
28
-
29
24
  def [](key)
30
25
  @entries[key.to_s]
31
26
  end
@@ -51,14 +46,9 @@ module BibSync
51
46
  Pathname.new(file).realpath.relative_path_from(Pathname.new(bibpath)).to_s
52
47
  end
53
48
 
54
- def each(&block)
55
- @entries.each_value(&block)
56
- end
57
-
58
49
  def save(file = nil)
59
50
  if file
60
51
  @file = file
61
- @parent_path = nil
62
52
  dirty!
63
53
  end
64
54
 
@@ -82,14 +72,8 @@ module BibSync
82
72
  dirty!
83
73
  end
84
74
 
85
- def load(file)
86
- parse(File.read(file)) if file && File.exists?(file)
87
- @file = file
88
- @dirty = false
89
- end
90
-
91
- def load!(file)
92
- parse(File.read(file))
75
+ def load(file, check = true)
76
+ parse(File.read(file)) if !check || (file && File.exists?(file))
93
77
  @file = file
94
78
  @dirty = false
95
79
  end
@@ -30,7 +30,7 @@ module BibSync
30
30
  @options[:bib] = bib
31
31
  end
32
32
 
33
- opts.on('-d', '--directory directory', 'Set directory') do |dir|
33
+ opts.on('-d', '--dir directory', 'Set directory') do |dir|
34
34
  @options[:dir] = dir
35
35
  end
36
36
 
@@ -1,18 +1,21 @@
1
1
  module BibSync
2
2
  class Entry
3
3
  include Enumerable
4
+ extend Forwardable
4
5
 
5
6
  attr_accessor :bibliography, :type
6
7
  attr_reader :key
8
+ def_delegators :@fields, :empty?, :size, :each
7
9
 
8
10
  def self.parse(text)
9
11
  Entry.new.tap {|e| e.parse(text) }
10
12
  end
11
13
 
12
14
  def initialize(fields = {})
15
+ @fields = {}
13
16
  self.key = fields.delete(:key) if fields.include?(:key)
14
17
  self.type = fields.delete(:type) if fields.include?(:type)
15
- @fields = fields
18
+ fields.each {|k,v| self[k] = v }
16
19
  end
17
20
 
18
21
  def key=(key)
@@ -39,7 +42,7 @@ module BibSync
39
42
  raise 'No bibliography set' unless bibliography
40
43
  _, file, type = self[:file].split(':', 3)
41
44
  path = File.join(File.absolute_path(File.dirname(bibliography.file)), file)
42
- { name: File.basename(path), type: type.upcase.to_sym, path: path }
45
+ { name: File.basename(path), type: type.upcase, path: path }
43
46
  end
44
47
  end
45
48
 
@@ -68,10 +71,6 @@ module BibSync
68
71
  end
69
72
  end
70
73
 
71
- def each(&block)
72
- @fields.each(&block)
73
- end
74
-
75
74
  def comment?
76
75
  type.to_s.downcase == 'comment'
77
76
  end
@@ -158,7 +157,7 @@ module BibSync
158
157
  end
159
158
 
160
159
  def convert_key(key)
161
- key.to_s.downcase.to_sym
160
+ key.to_s.downcase
162
161
  end
163
162
  end
164
163
  end
@@ -20,34 +20,16 @@ module BibSync
20
20
  self.trace = false
21
21
  self.level = :info
22
22
 
23
- def debug(message, opts = {})
24
- log(:debug, message, opts)
25
- end
26
-
27
- def info(message, opts = {})
28
- log(:info, message, opts)
29
- end
30
-
31
- def notice(message, opts = {})
32
- log(:notice, message, opts)
33
- end
34
-
35
- def warning(message, opts = {})
36
- log(:warning, message, opts)
37
- end
38
-
39
- def error(message, opts = {})
40
- log(:error, message, opts)
23
+ [:debug, :info, :notice, :warning, :error].each do |level|
24
+ define_method level do |message, opts = {}|
25
+ log(level, message, opts)
26
+ end
41
27
  end
42
28
 
43
29
  def log(level, message, opts = {})
44
30
  return if Level.keys.index(level) < Level.keys.index(Log.level)
45
- if ex = opts[:ex]
46
- message = "#{message} - #{ex.message}"
47
- end
48
- if color = Level[level]
49
- message = "#{color}#{message}#{Reset}"
50
- end
31
+ message = "#{message} - #{opts[:ex].message}" if opts[:ex]
32
+ message = "#{Level[level]}#{message}#{Reset}" if Level[level]
51
33
  if key = opts[:key]
52
34
  key = key.key if key.respond_to? :key
53
35
  message = "#{key} : #{message}"
@@ -1,3 +1,3 @@
1
1
  module BibSync
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -127,9 +127,6 @@ describe BibSync::Bibliography do
127
127
  describe '#load' do
128
128
  end
129
129
 
130
- describe '#load!' do
131
- end
132
-
133
130
  describe '#parse' do
134
131
  end
135
132
 
@@ -49,6 +49,12 @@ describe BibSync::Entry do
49
49
  describe '#each' do
50
50
  end
51
51
 
52
+ describe '#size' do
53
+ end
54
+
55
+ describe '#empty?' do
56
+ end
57
+
52
58
  describe '#comment?' do
53
59
  it 'should return true for a comment entry' do
54
60
  BibSync::Entry.new(type: 'coMMent').comment?.must_equal true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bibsync
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: