Class: Targetmy::Config::Site

Inherits:
Object
  • Object
show all
Defined in:
lib/targetmy/config/site.rb,
lib/targetmy/config/site/mobile.rb,
lib/targetmy/config/site/teaser.rb

Defined Under Namespace

Classes: Mobile, Teaser

Constant Summary collapse

REQUIRED_KEYS =
[:type, :link, :adv_products]
SUPPORTED_KEYS =
[:type, :link, :adv_products]
SUPPORTED_ADV_PRODUCTS =
[:teaser]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Site

Returns a new instance of Site.



10
11
12
# File 'lib/targetmy/config/site.rb', line 10

def initialize(config)
  parse config
end

Instance Attribute Details

#adv_productsObject (readonly)

Returns the value of attribute adv_products.



4
5
6
# File 'lib/targetmy/config/site.rb', line 4

def adv_products
  @adv_products
end

Returns the value of attribute link.



4
5
6
# File 'lib/targetmy/config/site.rb', line 4

def link
  @link
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/targetmy/config/site.rb', line 4

def type
  @type
end

Instance Method Details

#parse(config) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/targetmy/config/site.rb', line 14

def parse(config)
  config_required_items = REQUIRED_KEYS & config.keys
  raise Targetmy::MissingRequiredKeys if config_required_items.length != REQUIRED_KEYS.length

  SUPPORTED_KEYS.each do |key|
    next unless config.has_key?(key)
    
    case key
    when :adv_products
      @adv_products = []
      parse_adv_products config[key]
    else
      instance_variable_set("@#{key}", config[key])
    end
  end
end

#parse_adv_products(prods) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/targetmy/config/site.rb', line 31

def parse_adv_products(prods)
  adv_products = prods.is_a?(Hash) ? [prods] : prods
  adv_products.each do |prod|
    if prod.has_key?(:type) && SUPPORTED_ADV_PRODUCTS.include?(prod[:type].to_sym)
      type = prod[:type].to_s.capitalize
      klass = Object.const_get("Targetmy::Config::Site::#{type}")

      @adv_products << klass.new(prod)
    else
      raise Targetmy::MissingRequiredKey.new(:type)
    end
  end
end