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.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)

Returns the value of attribute store.



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

def store
  @store
end

Class Method Details

.default_configObject



94
95
96
97
98
99
100
101
102
103
# File 'lib/lookbook/stores/panel_store.rb', line 94

def self.default_config
  config = ConfigLoader.call(CONFIG_FILE)
  config.each do |group, panels|
    panels.map! do |opts|
      opts.transform_values! do |value|
        (value.is_a?(String) && value.start_with?("->")) ? eval(value) : value # standard:disable Security/Eval
      end
    end
  end
end

.init_from_configObject



90
91
92
# File 'lib/lookbook/stores/panel_store.rb', line 90

def self.init_from_config
  new(default_config)
end

.resolve_config(opts, data) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/lookbook/stores/panel_store.rb', line 77

def self.resolve_config(opts, data)
  if opts[:name].present?
    data = data.is_a?(Store) ? data : Store.new(data)
    data.name = opts[:name].to_s
    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, group_name, *args) ⇒ Object



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

def add_panel(name, group_name, *args)
  if get_panel(name)
    raise ConfigError.new("panel with name '#{name}' already exists", scope: "panels.config")
  else
    panel = build_config(name, group_name, *args)
    insert_at_position(group_name, panel.position, panel)
  end
end

#count_panels(group_name = nil) ⇒ Object



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

def count_panels(group_name = nil)
  panels(group_name).count
end

#get_panel(name, group_name = nil) ⇒ Object



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

def get_panel(name, group_name = nil)
  panels(group_name).find { |p| p.name == name.to_sym }
end

#in_group(name) ⇒ Object



67
68
69
# File 'lib/lookbook/stores/panel_store.rb', line 67

def in_group(name)
  store[name.to_sym] ||= []
end

#load_config(config) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/lookbook/stores/panel_store.rb', line 51

def load_config(config)
  config.to_h.each do |group_name, panels|
    panels.each do |opts|
      add_panel(opts[:name], group_name, opts.except(:name))
    end
  end
end

#panels(group_name = nil) ⇒ Object



71
72
73
74
75
# File 'lib/lookbook/stores/panel_store.rb', line 71

def panels(group_name = nil)
  store.to_h.reduce([]) do |result, (name, group_panels)|
    result.push(*group_panels) if group_name.nil? || name == group_name.to_sym
  end
end

#remove_panel(name) ⇒ Object



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

def remove_panel(name)
  store.each do |group_name, panels|
    return true unless panels.reject! { |p| p.name == name.to_sym }.nil?
  end
  not_found!(name)
end

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



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lookbook/stores/panel_store.rb', line 31

def update_panel(name, opts = {})
  panel = get_panel(name)
  if panel.present?
    panel.merge!(opts.except(:name, :position))
    if opts.key?(:position)
      remove_panel(name)
      insert_at_position(panel.group, opts[:position], panel)
    end
  else
    not_found!(name)
  end
end