Class: TrustyCms::AdminUI::RegionSet

Inherits:
Object
  • Object
show all
Defined in:
lib/trusty_cms/admin_ui/region_set.rb

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ RegionSet

Returns a new instance of RegionSet.

Yields:

  • (_self)

Yield Parameters:



2
3
4
5
6
7
# File 'lib/trusty_cms/admin_ui/region_set.rb', line 2

def initialize
  @regions = Hash.new do |h, k|
    h[k] = []
  end
  yield self if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/trusty_cms/admin_ui/region_set.rb', line 27

def method_missing(method, *args, &block)
  if args.empty?
    self[method]
  else
    super
  end
end

Instance Method Details

#[](region) ⇒ Object



9
10
11
# File 'lib/trusty_cms/admin_ui/region_set.rb', line 9

def [](region)
  @regions[region.to_sym]
end

#add(region = nil, partial = nil, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/trusty_cms/admin_ui/region_set.rb', line 13

def add(region = nil, partial = nil, options = {})
  raise ArgumentError, 'You must specify a region and a partial' unless region && partial

  if options[:before]
    index = @regions[region].empty? ? 0 : (@regions[region].index(options[:before]) || @regions[region].size)
    self[region].insert(index, partial)
  elsif options[:after]
    index = @regions[region].empty? ? 0 : (@regions[region].index(options[:after]) || @regions[region].size - 1)
    self[region].insert(index + 1, partial)
  else
    self[region] << partial
  end
end