Module: AbideDevUtils::Output

Defined in:
lib/abide_dev_utils/output.rb

Constant Summary collapse

FWRITER =
AbideDevUtils::Files::Writer.new

Class Method Summary collapse

Class Method Details

.json(in_obj, console: false, file: nil, pretty: true, **_) ⇒ Object



27
28
29
30
31
32
# File 'lib/abide_dev_utils/output.rb', line 27

def self.json(in_obj, console: false, file: nil, pretty: true, **_)
  AbideDevUtils::Validate.hashable(in_obj)
  json_out = pretty ? JSON.pretty_generate(in_obj) : JSON.generate(in_obj)
  simple(json_out) if console
  FWRITER.write_json(json_out, file: file) unless file.nil?
end

.progress(title: 'Progress', start: 0, total: 100, format: nil, **_) ⇒ Object



58
59
60
# File 'lib/abide_dev_utils/output.rb', line 58

def self.progress(title: 'Progress', start: 0, total: 100, format: nil, **_)
  ProgressBar.create(title: title, starting_at: start, total: total, format: format)
end

.simple(msg, stream: $stdout, **_) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/abide_dev_utils/output.rb', line 13

def self.simple(msg, stream: $stdout, **_)
  case msg
  when Hash
    stream.puts JSON.pretty_generate(msg)
  else
    stream.puts msg
  end
end

.text(msg, console: false, file: nil, **_) ⇒ Object



22
23
24
25
# File 'lib/abide_dev_utils/output.rb', line 22

def self.text(msg, console: false, file: nil, **_)
  simple(msg) if console
  FWRITER.write_text(msg, file: file) unless file.nil?
end

.yaml(in_obj, console: false, file: nil, stringify: false, **_) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/abide_dev_utils/output.rb', line 34

def self.yaml(in_obj, console: false, file: nil, stringify: false, **_)
  yaml_out = if in_obj.is_a? String
               in_obj
             else
               AbideDevUtils::Validate.hashable(in_obj)
               if stringify
                 JSON.parse(JSON.generate(in_obj)).to_yaml
               else
                 # Use object's #to_yaml method if it exists, convert to hash if not
                 in_obj.respond_to?(:to_yaml) ? in_obj.to_yaml : in_obj.to_h.to_yaml
               end
             end
  simple(yaml_out) if console
  FWRITER.write_yaml(yaml_out, file: file) unless file.nil?
end

.yml(in_obj, console: false, file: nil, **_) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/abide_dev_utils/output.rb', line 50

def self.yml(in_obj, console: false, file: nil, **_)
  AbideDevUtils::Validate.hashable(in_obj)
  # Use object's #to_yaml method if it exists, convert to hash if not
  yml_out = in_obj.respond_to?(:to_yaml) ? in_obj.to_yaml : in_obj.to_h.to_yaml
  simple(yml_out) if console
  FWRITER.write_yml(yml_out, file: file) unless file.nil?
end