Class: Squared::Config::Viewer

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

Constant Summary

Constants included from Squared::Common

Squared::Common::ARG

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, dump: nil, opts: {}, auto: true, common: ARG[:COMMON], pipe: ARG[:PIPE]) ⇒ 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
55
56
57
# File 'lib/squared/config.rb', line 21

def initialize(main, name = nil, project: nil, prefix: nil, dump: nil, opts: {}, auto: true,
               common: ARG[:COMMON], pipe: ARG[:PIPE], **)
  if project
    main = @project.basepath(main).to_s if (@project = __get__(:project)[project.to_s])
    @required = true
  end
  @name = name&.to_s || @project&.name
  @prefix = prefix
  @ext = File.extname(main).downcase
  @dump = dump
  @mime = {}
  @theme = common ? __get__(:theme)[:viewer] : {}
  @pipe = env_pipe(pipe, @project ? @project.pipe : 1)
  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'] : ['name', 'missing']
              end
  warn log_message(:warn, msg, subject: self.class, hint: hint)
end

Instance Attribute Details

#mainObject (readonly)

Returns the value of attribute main.



18
19
20
# File 'lib/squared/config.rb', line 18

def main
  @main
end

#nameObject (readonly)

Returns the value of attribute name.



18
19
20
# File 'lib/squared/config.rb', line 18

def name
  @name
end

#pipeObject

Returns the value of attribute pipe.



19
20
21
# File 'lib/squared/config.rb', line 19

def pipe
  @pipe
end

#projectObject (readonly)

Returns the value of attribute project.



18
19
20
# File 'lib/squared/config.rb', line 18

def project
  @project
end

#themeObject (readonly)

Returns the value of attribute theme.



18
19
20
# File 'lib/squared/config.rb', line 18

def theme
  @theme
end

Class Method Details

.to_sObject



14
15
16
# File 'lib/squared/config.rb', line 14

def self.to_s
  super.match(/[^:]+\z/)[0]
end

Instance Method Details

#add(type, gem: nil, parse: nil, ext: nil, opts: {}, command: 'view', file: nil, exist: nil) ⇒ Object



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
116
117
118
# File 'lib/squared/config.rb', line 85

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 && exist?

    namespace task_name(name) do
      namespace command do
        desc format_desc(command, *ext, exist: exist)
        task type, [:keys] do |_, args|
          if file
            read_keys(obj, type, file.to_s, args.to_a, ext: ext)
          else
            read_keys(obj, type, args.keys, args.extras, ext: ext)
          end
        end
      end
    end
  end
rescue LoadError, NameError => e
  project&.log&.warn e
  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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/squared/config.rb', line 120

def also(path, type = nil, name: nil, gem: nil, parse: nil, opts: {})
  return self if @mime.frozen? || !(file = basepath(path)).exist?

  ext = mimetype(file)
  type = type&.to_s || ext
  unless 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

Yields:

  • (_self)

Yield Parameters:



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

def build
  return unless enabled?

  namespace(ns = task_name(name)) do
    view = @command && @command != name ? @command : 'view'
    params = ->(args) { exist? ? [realpath, [args.keys] + args.extras] : [args.keys, args.extras] }
    namespace view do
      if @mime['json'] && (exist? || !Rake::Task.task_defined?("#{ns}:#{view}:json"))
        desc format_desc(view, 'json')
        task 'json', [:keys] do |_, args|
          read_keys(JSON, 'json', *params.(args))
        end
      end

      if @mime['yaml'] && (exist? || !Rake::Task.task_defined?("#{ns}:#{view}:yaml"))
        desc format_desc(view, 'yaml', 'yml')
        task 'yaml', [:keys] do |_, args|
          require 'yaml'
          read_keys(YAML, 'yaml', *params.(args), ext: ['yml', 'yaml'])
        end
      end
    end
  end
  yield self if block_given?
end

#enabled?Boolean

Returns:

  • (Boolean)


143
144
145
146
147
# File 'lib/squared/config.rb', line 143

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

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

#extensionsObject



149
150
151
# File 'lib/squared/config.rb', line 149

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

#inspectObject



159
160
161
# File 'lib/squared/config.rb', line 159

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

#style(name, *args) ⇒ Object



138
139
140
141
# File 'lib/squared/config.rb', line 138

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

#to_sObject



153
154
155
156
157
# File 'lib/squared/config.rb', line 153

def to_s
  realpath if exist?

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