Class: Squared::Config::Viewer
- Inherits:
-
Object
- Object
- Squared::Config::Viewer
- Includes:
- Format, Rake::DSL, Squared::Common, Task
- Defined in:
- lib/squared/config.rb
Instance Attribute Summary collapse
-
#main ⇒ Object
readonly
Returns the value of attribute main.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
-
#theme ⇒ Object
readonly
Returns the value of attribute theme.
Class Method Summary collapse
Instance Method Summary collapse
- #add(type, gem: nil, parse: nil, ext: nil, opts: {}, command: 'view', file: nil, exist: nil) ⇒ Object
- #also(path, type = nil, name: nil, gem: nil, parse: nil, opts: {}) ⇒ Object
- #build {|_self| ... } ⇒ Object
- #enabled? ⇒ Boolean
- #extensions ⇒ Object
-
#initialize(main, name = nil, project: nil, dump: nil, opts: {}, auto: true, common: true) ⇒ Viewer
constructor
A new instance of Viewer.
- #inspect ⇒ Object
- #style(name, *args) ⇒ Object
- #to_s ⇒ Object
Methods included from Squared::Common
#__get__, #__set__, #as_a, #finalize!, #message
Constructor Details
#initialize(main, name = nil, project: nil, dump: nil, opts: {}, auto: true, common: true) ⇒ Viewer
Returns a new instance of Viewer.
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 |
# File 'lib/squared/config.rb', line 21 def initialize(main, name = nil, project: nil, dump: nil, opts: {}, auto: true, common: true) if project main = @project.base_path(main).to_s if (@project = __get__(:project)[project.to_sym]) @required = true end @name = name&.to_s || @project&.name @ext = File.extname(main) @dump = dump @mime = {} @theme = common ? __get__(:theme)[:viewer] : {} if exist? @main = main.chomp(@ext) @name = @main unless @name || @required if auto case @ext when '.json', '.js' add('json', command: File.basename(@main), opts: opts) when '.yaml', '.yml' add('yaml', command: File.basename(@main), opts: opts) end end else @main = main end return unless warning? && ((missing = exist? && !File.exist?(main)) || !@name) msg, hint = if missing ['path not found', realpath] else @required = true project ? [project, 'not found'] : %w[name missing] end warn (:warn, msg, subject: self.class, hint: hint, color: !pipe?) end |
Instance Attribute Details
#main ⇒ Object (readonly)
Returns the value of attribute main.
19 20 21 |
# File 'lib/squared/config.rb', line 19 def main @main end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
19 20 21 |
# File 'lib/squared/config.rb', line 19 def name @name end |
#project ⇒ Object (readonly)
Returns the value of attribute project.
19 20 21 |
# File 'lib/squared/config.rb', line 19 def project @project end |
#theme ⇒ Object (readonly)
Returns the value of attribute theme.
19 20 21 |
# File 'lib/squared/config.rb', line 19 def theme @theme end |
Class Method Details
.to_s ⇒ Object
14 15 16 |
# File 'lib/squared/config.rb', line 14 def to_s super.to_s.match(/[^:]+$/)[0] end |
Instance Method Details
#add(type, gem: nil, parse: nil, ext: nil, opts: {}, command: 'view', file: nil, exist: nil) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/squared/config.rb', line 84 def add(type, gem: nil, parse: nil, ext: nil, opts: {}, command: 'view', file: nil, exist: nil) return self if @mime.frozen? type = type.to_s if parse && enabled? require(gem || type) obj = eval(parse) ext << type if (ext = as_a(ext)).empty? file = realpath if file.nil? && exist? namespace name do desc format_desc(command, ext, exist: exist) namespace command do task type, [:keys] do |_, args| if file read_keys(obj, type, file.to_s, collect_args(args, :keys), ext: ext) else read_keys(obj, type, args.keys, args.extras, ext: ext) end end end end end rescue LoadError, NameError self else @mime[type] = opts if exist? @command = command @mime.freeze end self end |
#also(path, type = nil, name: nil, gem: nil, parse: nil, opts: {}) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/squared/config.rb', line 117 def also(path, type = nil, name: nil, gem: nil, parse: nil, opts: {}) return self if @mime.frozen? || !(file = base_path(path)).exist? ext = mime_type(file) type = type&.to_s || ext if !parse case type when 'json' parse = 'JSON' when 'yaml', 'yml' type = 'yaml' parse = 'YAML' end end name ||= file.basename.to_s.chomp(File.extname(file)) add(type, gem: gem, parse: parse, ext: ext, opts: opts, command: name, file: file, exist: true) end |
#build {|_self| ... } ⇒ Object
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 |
# File 'lib/squared/config.rb', line 56 def build return unless enabled? params = ->(args) { exist? ? [realpath, [args.keys] + args.extras] : [args.keys, args.extras] } namespace name do view = @command && @command != name ? @command : 'view' namespace view do if @mime['json'] && (exist? || !::Rake::Task.task_defined?("#{name}:#{view}:json")) desc format_desc(view, %w[json]) task :json, [:keys] do |_, args| read_keys(JSON, 'json', *params.(args)) end end if @mime['yaml'] && (exist? || !::Rake::Task.task_defined?("#{name}:#{view}:yaml")) desc format_desc(view, %w[yaml yml]) task :yaml, [:keys] do |_, args| require 'yaml' read_keys(YAML, 'yaml', *params.(args), ext: %w[yml yaml]) end end end end yield self if block_given? end |
#enabled? ⇒ Boolean
154 155 156 157 158 |
# File 'lib/squared/config.rb', line 154 def enabled? return File.exist?(realpath) if exist? !@required || (!project.nil? && project.enabled?) end |
#extensions ⇒ Object
140 141 142 |
# File 'lib/squared/config.rb', line 140 def extensions exist? ? [@ext.sub('.', '')] : @mime.keys end |
#inspect ⇒ Object
150 151 152 |
# File 'lib/squared/config.rb', line 150 def inspect "#<#{self.class}: #{name} => #{exist? ? realpath : "#{main} {#{extensions.join(', ')}}"}>" end |
#style(name, *args) ⇒ Object
135 136 137 138 |
# File 'lib/squared/config.rb', line 135 def style(name, *args) apply_style(theme, name, *args) self end |
#to_s ⇒ Object
144 145 146 147 148 |
# File 'lib/squared/config.rb', line 144 def to_s realpath if exist? @mime.keys.map { |ext| "#{main}.#{ext}" }.join(',') end |