Class: Cmsimple::RegionsProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/cmsimple/regions_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regions_hash) ⇒ RegionsProxy

Returns a new instance of RegionsProxy.



5
6
7
8
9
10
# File 'lib/cmsimple/regions_proxy.rb', line 5

def initialize(regions_hash)
  if regions_hash.is_a?(Hash)
    @regions_hash = ActiveSupport::HashWithIndifferentAccess.new(regions_hash.dup)
    define_regions
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



28
29
30
# File 'lib/cmsimple/regions_proxy.rb', line 28

def method_missing method, *args, &block
  Region.new {}
end

Instance Attribute Details

#regions_hashObject

Returns the value of attribute regions_hash.



3
4
5
# File 'lib/cmsimple/regions_proxy.rb', line 3

def regions_hash
  @regions_hash
end

Instance Method Details

#define_regionsObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/cmsimple/regions_proxy.rb', line 17

def define_regions
  mod = Module.new
  extend mod

  region_methods = @regions_hash.collect do |key, value|
    %{def #{key}() Cmsimple::Region.new(@regions_hash[:#{key}]); end}
  end

  mod.module_eval region_methods.join("\n"), __FILE__, __LINE__ + 1
end

#snippets_hashObject



12
13
14
15
# File 'lib/cmsimple/regions_proxy.rb', line 12

def snippets_hash
  return {} unless @regions_hash.present?
  @regions_hash.inject(ActiveSupport::HashWithIndifferentAccess.new){|h, (k,v)| h.merge(v[:snippets].presence || {})}
end