Class: AnyStyle::CLI::Commands::Base
- Inherits:
-
Object
- Object
- AnyStyle::CLI::Commands::Base
- Defined in:
- lib/anystyle/cli/commands/base.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#output_folder ⇒ Object
readonly
Returns the value of attribute output_folder.
Instance Method Summary collapse
- #each_format(&block) ⇒ Object
- #extsub(path, new_extname) ⇒ Object
- #find(input, **opts) ⇒ Object
- #format(dataset, fmt) ⇒ Object
-
#initialize(options) ⇒ Base
constructor
A new instance of Base.
- #overwrite? ⇒ Boolean
- #parse(input) ⇒ Object
- #run(args, params) ⇒ Object
- #say(*args) ⇒ Object
- #set_output_folder(path) ⇒ Object
- #stdout? ⇒ Boolean
- #transpose(path, base_path) ⇒ Object
- #verbose? ⇒ Boolean
- #walk(input) ⇒ Object
- #write(content, path, base_path) ⇒ Object
Constructor Details
#initialize(options) ⇒ Base
Returns a new instance of Base.
7 8 9 |
# File 'lib/anystyle/cli/commands/base.rb', line 7 def initialize() = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/anystyle/cli/commands/base.rb', line 5 def end |
#output_folder ⇒ Object (readonly)
Returns the value of attribute output_folder.
5 6 7 |
# File 'lib/anystyle/cli/commands/base.rb', line 5 def output_folder @output_folder end |
Instance Method Details
#each_format(&block) ⇒ Object
27 28 29 |
# File 'lib/anystyle/cli/commands/base.rb', line 27 def each_format(&block) [:format].each(&block) end |
#extsub(path, new_extname) ⇒ Object
56 57 58 59 |
# File 'lib/anystyle/cli/commands/base.rb', line 56 def extsub(path, new_extname) basename = path.basename(path.extname) path.dirname.join("#{basename}#{new_extname}") end |
#find(input, **opts) ⇒ Object
31 32 33 |
# File 'lib/anystyle/cli/commands/base.rb', line 31 def find(input, **opts) AnyStyle.find(input, format: :wapiti, **opts) end |
#format(dataset, fmt) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/anystyle/cli/commands/base.rb', line 39 def format(dataset, fmt) case fmt when 'bib' AnyStyle.parser.format_bibtex(dataset).to_s when 'csl' JSON.pretty_generate AnyStyle.parser.format_csl(dataset) when 'json' JSON.pretty_generate AnyStyle.parser.format_hash(dataset) when 'ref', 'txt' dataset.to_txt when 'xml' dataset.to_xml(indent: 2).to_s else raise ArgumentError, "format not supported: #{fmt}" end end |
#overwrite? ⇒ Boolean
23 24 25 |
# File 'lib/anystyle/cli/commands/base.rb', line 23 def overwrite? !![:overwrite] end |
#parse(input) ⇒ Object
35 36 37 |
# File 'lib/anystyle/cli/commands/base.rb', line 35 def parse(input) AnyStyle.parse(input, format: :wapiti) end |
#run(args, params) ⇒ Object
11 12 13 |
# File 'lib/anystyle/cli/commands/base.rb', line 11 def run(args, params) raise NotImplementedYet end |
#say(*args) ⇒ Object
82 83 84 |
# File 'lib/anystyle/cli/commands/base.rb', line 82 def say(*args) STDERR.print(*args) if verbose? end |
#set_output_folder(path) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/anystyle/cli/commands/base.rb', line 69 def set_output_folder(path) @output_folder = Pathname.new(path). unless path.nil? ensure unless @output_folder.nil? if @output_folder.exist? raise ArgumentError, "not a directory: #{path}" unless @output_folder.directory? else @output_folder.mkdir end end end |
#stdout? ⇒ Boolean
19 20 21 |
# File 'lib/anystyle/cli/commands/base.rb', line 19 def stdout? !![:stdout] end |
#transpose(path, base_path) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/anystyle/cli/commands/base.rb', line 61 def transpose(path, base_path) if output_folder.nil? path else output_folder.join(path.relative_path_from(base_path)) end end |
#verbose? ⇒ Boolean
15 16 17 |
# File 'lib/anystyle/cli/commands/base.rb', line 15 def verbose? !![:verbose] end |
#walk(input) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/anystyle/cli/commands/base.rb', line 86 def walk(input) path = Pathname(input). raise ArgumentError, "path does not exist: #{input}" unless path.exist? if path.directory? path.each_child do |file| yield file, path unless file.directory? end else yield path, path.dirname end end |
#write(content, path, base_path) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/anystyle/cli/commands/base.rb', line 99 def write(content, path, base_path) if stdout? STDOUT.puts(content) else path = transpose(path, base_path) if !overwrite? && path.exist? raise RuntimeError, "file exists, use --overwrite to force saving: #{path}" end File.write path, content end end |