Class: Lookbook::PanelStore

Inherits:
Object
  • Object
show all
Defined in:
lib/lookbook/stores/panel_store.rb

Constant Summary collapse

CONFIG_FILE =
"config/panels.yml"
DEFAULTS =
{
  label: lambda { |data| data.name.to_s.titleize },
  hotkey: nil,
  disabled: false,
  show: true,
  copy: nil,
  locals: {}
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ PanelStore

Returns a new instance of PanelStore.



17
18
19
20
# File 'lib/lookbook/stores/panel_store.rb', line 17

def initialize(config = nil)
  @store = {}
  load_config(config)
end

Instance Attribute Details

#storeObject (readonly)



14
15
16
# File 'lib/lookbook/stores/panel_store.rb', line 14

def store
  @store
end

Class Method Details

.default_configObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/lookbook/stores/panel_store.rb', line 85

def self.default_config
  config = ConfigLoader.call(CONFIG_FILE)
  config.to_h.transform_values! do |opts|
    opts.transform_values! do |value|
      if value.is_a?(String) && value.start_with?("->")
        proc {
          eval(value) # standard:disable Security/Eval
        }.call
      else
        value
      end
    end
  end
end

.init_from_configObject



81
82
83
# File 'lib/lookbook/stores/panel_store.rb', line 81

def self.init_from_config
  new(default_config)
end

.resolve_config(opts, data) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/lookbook/stores/panel_store.rb', line 68

def self.resolve_config(opts, data)
  if opts[:name].present?
    data = data.is_a?(Store) ? data : Store.new(data)
    data.name = Utils.symbolize_name(opts[:name])
    resolved = opts.transform_values do |value|
      value.respond_to?(:call) ? value.call(data) : value
    end
    Store.new(resolved)
  else
    raise ConfigError.new(":name key is required when resolving config", scope: "panels.config")
  end
end

Instance Method Details

#add_panel(name, *args) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/lookbook/stores/panel_store.rb', line 22

def add_panel(name, *args)
  if get_panel(name)
    raise ConfigError.new("panel with name '#{name}' already exists", scope: "panels.config")
  else
    store[Utils.symbolize_name(name)] = build_config(name, *args)
  end
end

#get_panel(name) ⇒ Object



50
51
52
# File 'lib/lookbook/stores/panel_store.rb', line 50

def get_panel(name)
  panels.find { |panel| panel.name == Utils.symbolize_name(name) }
end

#get_panels(*names) ⇒ Object



54
55
56
# File 'lib/lookbook/stores/panel_store.rb', line 54

def get_panels(*names)
  ListResolver.call(names.flatten, panels.map(&:name)) { |name| get_panel(name) }
end

#load_config(config) ⇒ Object



43
44
45
46
47
48
# File 'lib/lookbook/stores/panel_store.rb', line 43

def load_config(config)
  config.to_h.each do |name, opts|
    opts[:system] = true
    add_panel(name, opts)
  end
end

#namesObject



62
63
64
# File 'lib/lookbook/stores/panel_store.rb', line 62

def names
  panels.map(&:name)
end

#panelsObject Also known as: all



58
59
60
# File 'lib/lookbook/stores/panel_store.rb', line 58

def panels
  store.map { |name, panel| panel }
end

#remove_panel(name) ⇒ Object



39
40
41
# File 'lib/lookbook/stores/panel_store.rb', line 39

def remove_panel(name)
  store.delete(Utils.symbolize_name(name)) { |name| not_found!(name) }
end

#update_panel(name, opts = {}) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/lookbook/stores/panel_store.rb', line 30

def update_panel(name, opts = {})
  panel = get_panel(name)
  if panel.present?
    panel.merge!(opts.except(:name))
  else
    not_found!(name)
  end
end