Class: Squared::Config::Viewer

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Squared::Common::Format

check_style, emphasize, log_message, log_title, sub_style

Methods included from Squared::Common

#as_a, #get, #message, #set

Constructor Details

#initialize(main = 'package', project: nil, name: nil) ⇒ Viewer

Returns a new instance of Viewer.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/squared/config.rb', line 35

def initialize(main = 'package', project: nil, name: nil)
  if project
    @project = get(:project)[project.to_sym]
    @required = true
  end
  @name = (name || @project&.name)&.to_s
  unless @name
    msg, hint = project ? [project, 'not found'] : %w[name missing]
    warn log_message(:warn, msg, subject: 'Config::Viewer', hint: hint, color: !pipe?)
    @required = true
  end
  @main = main
  @include = {}
end

Class Attribute Details

.stylesObject (readonly)

Returns the value of attribute styles.



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

def styles
  @styles
end

Instance Attribute Details

#mainObject (readonly)

Returns the value of attribute main.



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

def main
  @main
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#projectObject (readonly)

Returns the value of attribute project.



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

def project
  @project
end

Class Method Details

.style(name, *args) ⇒ Object



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

def style(name, *args)
  args = check_style(args)
  styles[name.to_sym]&.clear&.concat(args)
end

Instance Method Details

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/squared/config.rb', line 74

def add(type, gem: nil, parse: nil, ext: [], opts: {}, command: nil, file: nil)
  type = type.to_s
  if parse && enabled?
    require(gem || type)
    obj = eval(parse)
    ext = as_a(ext)
    namespace @name do
      desc format_desc(ext.first || type, command: command)
      namespace(command || :view) do
        task type, [:keys] do |_, args|
          if file
            read_keys obj, type, file.to_s, collect_args(args, :keys), *ext
          else
            read_keys obj, type, args.keys, args.extras, *ext
          end
        end
      end
    end
  end
rescue LoadError, NameError
  self
else
  @include[type] = opts
  self
end

#also(path, type = nil, name: nil, gem: nil, parse: nil, opts: {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/squared/config.rb', line 100

def also(path, type = nil, name: nil, gem: nil, parse: nil, opts: {})
  return self unless (file = base_path(path)).exist?

  ext = chop_extname(file).downcase
  name ||= file.basename.to_s.chomp(".#{ext}")
  type = (type || ext).to_s
  if !parse
    case type
    when 'json'
      parse = 'JSON'
    when 'yaml', 'yml'
      type = 'yaml'
      parse = 'YAML'
    end
  end
  add(type, gem: gem, parse: parse, ext: ext, opts: opts, command: name, file: file)
end

#buildObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/squared/config.rb', line 50

def build
  return unless enabled?

  namespace @name do
    namespace :view do
      if @include['json'] && !::Rake::Task.task_defined?("#{@name}:view:json")
        desc format_desc('json')
        task :json, [:keys] do |_, args|
          require 'json'
          read_keys JSON, 'json', args.keys, args.extras
        end
      end

      if @include['yaml'] && !::Rake::Task.task_defined?("#{@name}:view:yaml")
        desc format_desc('yaml', 'yml')
        task :yaml, [:keys] do |_, args|
          require 'yaml'
          read_keys YAML, 'yaml', args.keys, args.extras, 'yml', 'yaml'
        end
      end
    end
  end
end

#enabled?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/squared/config.rb', line 118

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