Class: Squared::Config::Viewer

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Viewer.



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

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 log_message(:warn, msg, subject: self.class, hint: hint)
end

Instance Attribute Details

#mainObject (readonly)

Returns the value of attribute main.



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

def main
  @main
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#projectObject (readonly)

Returns the value of attribute project.



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

def project
  @project
end

#themeObject (readonly)

Returns the value of attribute theme.



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

def theme
  @theme
end

Class Method Details

.to_sObject



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

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



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
# 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.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, args.to_a, 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



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

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

Yields:

  • (_self)

Yield Parameters:



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

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

Returns:

  • (Boolean)


155
156
157
158
159
# File 'lib/squared/config.rb', line 155

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

  !@required || (!project.nil? && project.enabled?)
end

#extensionsObject



141
142
143
# File 'lib/squared/config.rb', line 141

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

#inspectObject



151
152
153
# File 'lib/squared/config.rb', line 151

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

#style(name, *args) ⇒ Object



136
137
138
139
# File 'lib/squared/config.rb', line 136

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

#to_sObject



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

def to_s
  realpath if exist?

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