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
Constructor Details
#initialize(main, name = nil, project: nil, dump: nil, opts: {}, auto: true, common: true) ⇒ Viewer
Returns a new instance of Viewer.
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 |
# File 'lib/squared/config.rb', line 23 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.
21 22 23 |
# File 'lib/squared/config.rb', line 21 def main @main end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
21 22 23 |
# File 'lib/squared/config.rb', line 21 def name @name end |
#project ⇒ Object (readonly)
Returns the value of attribute project.
21 22 23 |
# File 'lib/squared/config.rb', line 21 def project @project end |
#theme ⇒ Object (readonly)
Returns the value of attribute theme.
21 22 23 |
# File 'lib/squared/config.rb', line 21 def theme @theme end |
Class Method Details
.to_s ⇒ Object
16 17 18 |
# File 'lib/squared/config.rb', line 16 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
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 116 117 |
# File 'lib/squared/config.rb', line 86 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
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/squared/config.rb', line 119 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
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 |
# File 'lib/squared/config.rb', line 58 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
156 157 158 159 160 |
# File 'lib/squared/config.rb', line 156 def enabled? return File.exist?(realpath) if exist? !@required || (!project.nil? && project.enabled?) end |
#extensions ⇒ Object
142 143 144 |
# File 'lib/squared/config.rb', line 142 def extensions exist? ? [@ext.sub('.', '')] : @mime.keys end |
#inspect ⇒ Object
152 153 154 |
# File 'lib/squared/config.rb', line 152 def inspect "#<#{self.class}: #{name} => #{exist? ? realpath : "#{main} {#{extensions.join(', ')}}"}>" end |
#style(name, *args) ⇒ Object
137 138 139 140 |
# File 'lib/squared/config.rb', line 137 def style(name, *args) apply_style(theme, name, *args) self end |
#to_s ⇒ Object
146 147 148 149 150 |
# File 'lib/squared/config.rb', line 146 def to_s realpath if exist? @mime.keys.map { |ext| "#{main}.#{ext}" }.join(',') end |