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 =
{}
@@task_desc =
Rake::TaskManager.

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

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.



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
93
94
# File 'lib/squared/config.rb', line 46

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.



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

def main
  @main
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#pipeObject

Returns the value of attribute pipe.



44
45
46
# File 'lib/squared/config.rb', line 44

def pipe
  @pipe
end

#projectObject (readonly)

Returns the value of attribute project.



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

def project
  @project
end

#themeObject (readonly)

Returns the value of attribute theme.



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

def theme
  @theme
end

Class Method Details



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

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

  ret = Viewer.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
25
# File 'lib/squared/config.rb', line 15

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

.to_sObject



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

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

Instance Method Details

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



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
161
162
# File 'lib/squared/config.rb', line 125

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
      as_a(ext).each do |val|
        next unless (data = @@mime_obj[val])

        obj = data.first
        break
      end
    end
    if obj
      ext << type if (ext = as_a(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



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

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:



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
122
123
# File 'lib/squared/config.rb', line 96

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)


178
179
180
181
182
# File 'lib/squared/config.rb', line 178

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

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

#extensionsObject



184
185
186
# File 'lib/squared/config.rb', line 184

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

#inspectObject



194
195
196
# File 'lib/squared/config.rb', line 194

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

#style(name, *args) ⇒ Object



173
174
175
176
# File 'lib/squared/config.rb', line 173

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

#to_sObject



188
189
190
191
192
# File 'lib/squared/config.rb', line 188

def to_s
  realpath if target?

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