Class: Pageflow::WidgetTypes

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/pageflow/widget_types.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWidgetTypes

Returns a new instance of WidgetTypes


7
8
9
# File 'lib/pageflow/widget_types.rb', line 7

def initialize
  clear
end

Instance Attribute Details

#defaults_by_roleObject (readonly)

Returns the value of attribute defaults_by_role


5
6
7
# File 'lib/pageflow/widget_types.rb', line 5

def defaults_by_role
  @defaults_by_role
end

Instance Method Details

#clearObject


21
22
23
24
# File 'lib/pageflow/widget_types.rb', line 21

def clear
  @widget_types = {}
  @defaults_by_role = {}
end

#each(&block) ⇒ Object


26
27
28
# File 'lib/pageflow/widget_types.rb', line 26

def each(&block)
  @widget_types.values.each(&block)
end

#fetch_by_name(name, &block) ⇒ Object


36
37
38
# File 'lib/pageflow/widget_types.rb', line 36

def fetch_by_name(name, &block)
  @widget_types.fetch(name, &block)
end

#find_all_by_role(role) ⇒ Object


40
41
42
43
44
# File 'lib/pageflow/widget_types.rb', line 40

def find_all_by_role(role)
  select do |widget_type|
    widget_type.roles.include?(role)
  end
end

#find_by_name!(name) ⇒ Object


30
31
32
33
34
# File 'lib/pageflow/widget_types.rb', line 30

def find_by_name!(name)
  fetch_by_name(name) do
    raise "Unknown widget type with name '#{name}'."
  end
end

#register(widget_type, options = {}) ⇒ Object


11
12
13
14
15
16
17
18
19
# File 'lib/pageflow/widget_types.rb', line 11

def register(widget_type, options = {})
  @widget_types[widget_type.name] = widget_type

  if options[:default]
    widget_type.roles.each do |role|
      defaults_by_role[role] = widget_type
    end
  end
end

#rolesObject


46
47
48
# File 'lib/pageflow/widget_types.rb', line 46

def roles
  @widget_types.values.flat_map(&:roles).uniq
end