Class: Squared::Config::Viewer

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL, Squared::Common::Format, Utils
Defined in:
lib/squared/config.rb

Constant Summary collapse

@@mime_obj =
{}

Constants included from Squared::Common

Squared::Common::ARG, Squared::Common::PATH

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Squared::Common::Format

#enable_aixterm, #enable_drawing

Constructor Details

#initialize(main, name = nil, project: nil, prefix: nil, command: nil, dump: nil, opts: {}, auto: true, common: , pipe: ) ⇒ Viewer

Returns a new instance of Viewer.



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
90
91
92
# File 'lib/squared/config.rb', line 44

def initialize(main, name = nil, project: nil, prefix: nil, command: nil, dump: nil, opts: {}, auto: true,
               common: ARG[:COMMON], pipe: ARG[:PIPE], **)
  if project && (project.respond_to?(:workspace) || (project = __get__(:project)[project.to_s]))
    main = project.basepath(main).to_s
    @project = project
    @envname = project.instance_variable_get(:@envname)
    @required = true
  end
  @name = name || @project&.name
  @prefix = prefix unless @project
  @ext = File.extname(main)
  @dump = dump
  @mime = {}
  @theme = common ? __get__(:theme)[:viewer] : {}
  @pipe = env_pipe(pipe, @project ? @project.pipe : 1)
  if target?
    @main = main.chomp(@ext)
    @name = @main unless @name || @required
    if auto
      unless command
        command = File.basename(@main)
        command = ARG[:VIEW] if command == @name
      end
      ext = @ext[1..-1].downcase
      if (data = @@mime_obj[ext])
        add(data[1].first, command: command, opts: opts, ext: data[1])
      else
        case ext
        when 'json', 'js'
          add('json', command: command, opts: opts)
        when 'yaml', 'yml'
          add('yaml', command: command, opts: opts)
        end
      end
    end
  else
    @main = main
    @command = command || ARG[:VIEW]
  end
  return unless warning? && ((missing = target? && !File.exist?(main)) || !@name)

  msg, hint = if missing
                ['path not found', realpath]
              else
                @required = true
                project ? [project, 'not found'] : ['name', 'missing']
              end
  warn log_message(Logger::WARN, msg, subject: self.class, hint: hint)
end

Instance Attribute Details

#mainObject (readonly)

Returns the value of attribute main.



41
42
43
# File 'lib/squared/config.rb', line 41

def main
  @main
end

#nameObject (readonly)

Returns the value of attribute name.



41
42
43
# File 'lib/squared/config.rb', line 41

def name
  @name
end

#pipeObject

Returns the value of attribute pipe.



42
43
44
# File 'lib/squared/config.rb', line 42

def pipe
  @pipe
end

#projectObject (readonly)

Returns the value of attribute project.



41
42
43
# File 'lib/squared/config.rb', line 41

def project
  @project
end

#themeObject (readonly)

Returns the value of attribute theme.



41
42
43
# File 'lib/squared/config.rb', line 41

def theme
  @theme
end

Class Method Details



26
27
28
29
30
31
32
# File 'lib/squared/config.rb', line 26

def link(project, main = project.dependfile.basename, name = nil, **kwargs, &blk)
  return unless project.enabled?

  ret = new(main, name, project: project, **kwargs)
  ret.instance_eval(&blk) if block_given?
  ret
end

.parse(gem, namespace, ext = [pkg]) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/squared/config.rb', line 15

def parse(gem, namespace, ext = [pkg])
  require gem
  obj = eval namespace
  Array(ext).each { |val| @@mime_obj[val] = [obj, ext] }
rescue LoadError, NameError => e
  warn e
  nil
else
  @@mime_obj[ext.first]
end

.to_sObject



34
35
36
# File 'lib/squared/config.rb', line 34

def to_s
  super[/[^:]+\z/, 0]
end

Instance Method Details

#add(type, ext: nil, opts: {}, command: , gem: nil, namespace: nil, file: nil) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/squared/config.rb', line 123

def add(type, ext: nil, opts: {}, command: ARG[:VIEW], gem: nil, namespace: nil, file: nil)
  return self if @mime.frozen?

  if enabled?
    if namespace
      require(gem || type)
      obj = eval namespace
    else
      Array(ext).each do |val|
        next unless (data = @@mime_obj[val])

        obj = data.first
        break
      end
    end
    if obj
      ext << type if (ext = Array(ext)).empty?
      if !file && target?
        ext.each do |val|
          next unless (out = basepath("#{main}.#{val}")).exist?

          file = out
          break
        end
      end
    end
  end
rescue LoadError, NameError => e
  log&.warn e
  self
else
  (@mime[type] ||= []) << [command || @command, file, opts]
  if target?
    @mime[type].freeze
    @mime.freeze
  end
  self
end

#also(path, type = nil, name: nil, **kwargs) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/squared/config.rb', line 162

def also(path, type = nil, name: nil, **kwargs)
  return self if @mime.frozen? || !(file = basepath(path)).exist?

  ext = mimetype file
  type ||= ext
  name ||= file.basename.to_s.chomp(File.extname(file))
  add(type, ext: ext, command: name, file: file, **kwargs)
end

#build {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/squared/config.rb', line 94

def build
  return unless enabled?

  namespace(ns = task_name(name)) do
    @mime.each do |type, items|
      items.each do |command, file, opts|
        next if Rake::Task.task_defined?("#{ns}:#{command}:#{type}")

        namespace command do
          unless (data = @@mime_obj[type])
            ext = [type]
            ext << 'yml' if type == 'yaml'
            next unless (data = Viewer.parse(type, type.upcase, ext))
          end
          obj, ext = data
          target = file || target? ? file || realpath : nil

          task_desc(command, *ext, target: target)
          task type, [:keys] do |_, args|
            params = target ? [target, args.to_a] : [args.keys, args.extras]
            read_keys(obj, ext.first, *params, ext: ext, opts: opts)
          end
        end
      end
    end
  end
  yield self if block_given?
end

#enabled?Boolean

Returns:

  • (Boolean)


176
177
178
179
180
# File 'lib/squared/config.rb', line 176

def enabled?
  return File.exist?(realpath) if target?

  !@required || !!project&.enabled?
end

#extensionsObject



182
183
184
# File 'lib/squared/config.rb', line 182

def extensions
  target? ? [@ext.sub('.', '').downcase] : @mime.keys
end

#inspectObject



192
193
194
# File 'lib/squared/config.rb', line 192

def inspect
  "#<#{self.class}: #{name} => #{target? ? realpath : "#{main} {#{extensions.join(', ')}}"}>"
end

#style(name, *args) ⇒ Object



171
172
173
174
# File 'lib/squared/config.rb', line 171

def style(name, *args)
  apply_style theme, name, args
  self
end

#to_sObject



186
187
188
189
190
# File 'lib/squared/config.rb', line 186

def to_s
  realpath if target?

  @mime.keys.map! { |ext| "#{main}.#{ext}" }.join(',')
end