Class: Coral::Util::Interface
- Inherits:
-
Object
- Object
- Coral::Util::Interface
- Defined in:
- lib/coral_core/util/interface.rb
Constant Summary collapse
- COLORS =
{ :clear => "\e[0m", :red => "\e[31m", :green => "\e[32m", :yellow => "\e[33m" }
- COLOR_MAP =
{ :warn => COLORS[:yellow], :error => COLORS[:red], :success => COLORS[:green] }
- @@logger =
Properties
Log4r::Logger.new("coral::interface")
Instance Attribute Summary collapse
-
#color ⇒ Object
—————————————————————————– Accessors / Modifiers.
-
#delegate ⇒ Object
—————————————————————————– Accessors / Modifiers.
-
#error(message, *args) ⇒ Object
—.
-
#input ⇒ Object
—————————————————————————– Accessors / Modifiers.
-
#logger ⇒ Object
—————————————————————————– Accessors / Modifiers.
-
#output ⇒ Object
—————————————————————————– Accessors / Modifiers.
-
#resource ⇒ Object
—————————————————————————– Accessors / Modifiers.
Class Method Summary collapse
-
.logger ⇒ Object
—————————————————————————–.
Instance Method Summary collapse
-
#ask(message, options = {}) ⇒ Object
—.
-
#check_delegate(method) ⇒ Object
—————————————————————————–.
-
#format_message(type, message, options = {}) ⇒ Object
—————————————————————————– Utilities.
-
#info(message, *args) ⇒ Object
—————————————————————————–.
-
#initialize(options = {}) ⇒ Interface
constructor
—————————————————————————– Constructor.
-
#inspect ⇒ Object
—.
-
#safe_puts(message = nil, options = {}) ⇒ Object
—.
-
#say(type, message, options = {}) ⇒ Object
—————————————————————————– UI functionality.
-
#success(message, *args) ⇒ Object
—.
-
#warn(message, *args) ⇒ Object
—.
Constructor Details
#initialize(options = {}) ⇒ Interface
Constructor
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 |
# File 'lib/coral_core/util/interface.rb', line 30 def initialize( = {}) class_name = self.class.to_s.downcase if .is_a?(String) = { :resource => , :logger => } end config = Config.ensure() if config.get(:logger, false) if config[:logger].is_a?(String) @logger = Log4r::Logger.new(config[:logger]) else @logger = config[:logger] end else @logger = Log4r::Logger.new(class_name) end @resource = config.get(:resource, '') @color = config.get(:color, true) @printer = config.get(:printer, :puts) @input = config.get(:input, $stdin) @output = config.get(:output, $stdout) @error = config.get(:error, $stderr) @delegate = config.get(:ui_delegate, nil) end |
Instance Attribute Details
#color ⇒ Object
Accessors / Modifiers
69 70 71 |
# File 'lib/coral_core/util/interface.rb', line 69 def color @color end |
#delegate ⇒ Object
Accessors / Modifiers
69 70 71 |
# File 'lib/coral_core/util/interface.rb', line 69 def delegate @delegate end |
#error(message, *args) ⇒ Object
69 70 71 |
# File 'lib/coral_core/util/interface.rb', line 69 def error @error end |
#input ⇒ Object
Accessors / Modifiers
69 70 71 |
# File 'lib/coral_core/util/interface.rb', line 69 def input @input end |
#logger ⇒ Object
Accessors / Modifiers
69 70 71 |
# File 'lib/coral_core/util/interface.rb', line 69 def logger @logger end |
#output ⇒ Object
Accessors / Modifiers
69 70 71 |
# File 'lib/coral_core/util/interface.rb', line 69 def output @output end |
#resource ⇒ Object
Accessors / Modifiers
69 70 71 |
# File 'lib/coral_core/util/interface.rb', line 69 def resource @resource end |
Class Method Details
.logger ⇒ Object
73 74 75 |
# File 'lib/coral_core/util/interface.rb', line 73 def self.logger return @@logger end |
Instance Method Details
#ask(message, options = {}) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/coral_core/util/interface.rb', line 94 def ask(, = {}) return @delegate.ask(, ) if check_delegate('ask') [:new_line] = false if ! .has_key?(:new_line) [:prefix] = false if ! .has_key?(:prefix) say(:info, , ) return @input.gets.chomp end |
#check_delegate(method) ⇒ Object
185 186 187 |
# File 'lib/coral_core/util/interface.rb', line 185 def check_delegate(method) return ( @delegate && @delegate.respond_to?(method.to_s) ) end |
#format_message(type, message, options = {}) ⇒ Object
Utilities
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/coral_core/util/interface.rb', line 143 def (type, , = {}) return @delegate.(type, , ) if check_delegate('format_message') if @resource && ! @resource.empty? && [:prefix] prefix = "[#{@resource}]" end = "#{prefix} #{}".strip if @color if .has_key?(:color) color = COLORS[[:color]] = "#{color}#{}#{COLORS[:clear]}" else = "#{COLOR_MAP[type]}#{}#{COLORS[:clear]}" if COLOR_MAP[type] end end return end |
#info(message, *args) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/coral_core/util/interface.rb', line 106 def info(, *args) @logger.info("info: #{}") return @delegate.info(, *args) if check_delegate('info') say(:info, , *args) end |
#inspect ⇒ Object
62 63 64 |
# File 'lib/coral_core/util/interface.rb', line 62 def inspect "#<#{self.class}: #{@resource}>" end |
#safe_puts(message = nil, options = {}) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/coral_core/util/interface.rb', line 164 def safe_puts( = nil, = {}) return @delegate.safe_puts(, ) if check_delegate('safe_puts') #dbg(message, 'message') #dbg(options, 'options') ||= "" = { :channel => @output, :printer => @printer, }.merge() begin [:channel].send([:printer], ) rescue Errno::EPIPE return end end |
#say(type, message, options = {}) ⇒ Object
UI functionality
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/coral_core/util/interface.rb', line 80 def say(type, , = {}) return @delegate.say(type, , ) if check_delegate('say') defaults = { :new_line => true, :prefix => true } = defaults.merge() printer = [:new_line] ? :puts : :print channel = type == :error || [:channel] == :error ? @error : @output safe_puts((type, , ), :channel => channel, :printer => printer) end |
#success(message, *args) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/coral_core/util/interface.rb', line 133 def success(, *args) @logger.info("success: #{}") return @delegate.success(, *args) if check_delegate('success') say(:success, , *args) end |
#warn(message, *args) ⇒ Object
115 116 117 118 119 120 |
# File 'lib/coral_core/util/interface.rb', line 115 def warn(, *args) @logger.info("warn: #{}") return @delegate.warn(, *args) if check_delegate('warn') say(:warn, , *args) end |