Class: Squared::Config::Viewer

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

Constant Summary

Constants included from Squared::Common

Squared::Common::KEY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(main, name = nil, project: nil, prefix: nil, dump: nil, opts: {}, auto: true, common: Common::KEY[:COMMON], pipe: Common::KEY[:PIPE]) ⇒ Viewer

Returns a new instance of Viewer.



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

def initialize(main, name = nil,
               project: nil, prefix: nil, dump: nil, opts: {}, auto: true,
               common: Common::KEY[:COMMON], pipe: Common::KEY[:PIPE], **)
  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
  @prefix = prefix
  @ext = File.extname(main)
  @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'] : %w[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.



21
22
23
# File 'lib/squared/config.rb', line 21

def main
  @main
end

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/squared/config.rb', line 21

def name
  @name
end

#pipeObject

Returns the value of attribute pipe.



22
23
24
# File 'lib/squared/config.rb', line 22

def pipe
  @pipe
end

#projectObject (readonly)

Returns the value of attribute project.



21
22
23
# File 'lib/squared/config.rb', line 21

def project
  @project
end

#themeObject (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_sObject



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



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
119
120
121
122
123
124
# File 'lib/squared/config.rb', line 91

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 task_name(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, 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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/squared/config.rb', line 126

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
  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:



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

def build
  return unless enabled?

  params = ->(args) { exist? ? [realpath, [args.keys] + args.extras] : [args.keys, args.extras] }

  namespace(ns = task_name(name)) do
    view = @command && @command != name ? @command : 'view'
    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: %w[yml yaml])
        end
      end
    end
  end

  yield self if block_given?
end

#enabled?Boolean

Returns:

  • (Boolean)


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

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

  !@required || (!project.nil? && 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



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

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