File: config.rb

package info (click to toggle)
ruby-table-print 1.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 296 kB
  • sloc: ruby: 1,823; makefile: 2
file content (89 lines) | stat: -rw-r--r-- 1,761 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
module TablePrint
  class Config

    DEFAULT_MAX_WIDTH = 30
    DEFAULT_TIME_FORMAT = "%Y-%m-%d %H:%M:%S"
    DEFAULT_IO = $stdout
    DEFAULT_CAPITALIZE_HEADERS = true
    DEFAULT_SEPARATOR = "|"

    @@max_width = DEFAULT_MAX_WIDTH
    @@time_format = DEFAULT_TIME_FORMAT
    @@multibyte = false
    @@io = DEFAULT_IO
    @@capitalize_headers = true
    @@separator = DEFAULT_SEPARATOR

    @@klasses = {}

    def self.set(klass, val)
      if klass.is_a? Class
        @@klasses[klass] = val  # val is a hash of column options
      else
        TablePrint::Config.send("#{klass}=", val.first)
      end
    end

    def self.for(klass)
      @@klasses.fetch(klass) {}
    end

    def self.clear(klass)
      if klass.is_a? Class
        @@klasses.delete(klass)
      else
        original_value = TablePrint::Config.const_get("DEFAULT_#{klass.to_s.upcase}")
        TablePrint::Config.send("#{klass}=", original_value)
      end
    end

    def self.max_width
      @@max_width
    end

    def self.max_width=(width)
      @@max_width = width
    end

    def self.multibyte
      @@multibyte
    end

    def self.multibyte=(width)
      @@multibyte = width
    end

    def self.time_format
      @@time_format
    end

    def self.time_format=(format)
      @@time_format = format
    end

    def self.capitalize_headers
      @@capitalize_headers
    end

    def self.capitalize_headers=(caps)
      @@capitalize_headers = caps
    end

    def self.separator
      @@separator
    end

    def self.separator=(separator)
      @@separator = separator
    end

    def self.io
      @@io
    end

    def self.io=(io)
      raise StandardError.new("IO object must respond to :puts") unless io.respond_to? :puts
      @@io = io
    end
  end
end