Class: MapBackgrounds::Layer

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, url, enabled, by_default, options, provider) ⇒ Layer

Returns a new instance of Layer.


12
13
14
15
16
17
18
19
20
21
# File 'lib/map_backgrounds.rb', line 12

def initialize(name, url, enabled, by_default, options, provider)
  @name = name
  @url = url || provider.url
  @enabled = enabled.nil? ? false : enabled
  @by_default = by_default.nil? ? false : by_default
  @provider = provider
  @options = options || provider.options
  @options[:attribution] ||= provider.options[:attribution]
  @options[:subdomains] ||= provider.options[:subdomains]
end

Instance Attribute Details

#by_defaultObject (readonly)

Returns the value of attribute by_default


9
10
11
# File 'lib/map_backgrounds.rb', line 9

def by_default
  @by_default
end

#enabledObject (readonly)

Returns the value of attribute enabled


8
9
10
# File 'lib/map_backgrounds.rb', line 8

def enabled
  @enabled
end

#nameObject (readonly)

Returns the value of attribute name


6
7
8
# File 'lib/map_backgrounds.rb', line 6

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options


10
11
12
# File 'lib/map_backgrounds.rb', line 10

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url


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

def url
  @url
end

Class Method Details

.find(reference_name) ⇒ Object


59
60
61
62
63
64
# File 'lib/map_backgrounds.rb', line 59

def find(reference_name)
  return nil unless reference_name
  items.detect do |layer|
    layer.reference_name == reference_name
  end
end

.load(path = nil) ⇒ Object

Load backgrounds from config_path parameter (if any) or from config/map_backgrounds.yml


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/map_backgrounds.rb', line 37

def load(path = nil)
  config_path = path || Rails.root.join('config', 'map_backgrounds.yml')
  raise 'No valid config file found for MapBackgrounds::Layer' unless Pathname(config_path).exist?
  layers = YAML.load_file(config_path).deep_symbolize_keys
  layers.each do |provider, attributes|
    provider = ::MapBackgrounds::Provider.new(provider, attributes.try(:[], :url), attributes.try(:[], :enabled), attributes.try(:[], :by_default), attributes.try(:[], :options))

    # A layer can be represented by a variant or by the provider itself
    if attributes.key?(:variants) && attributes[:variants].is_a?(Hash)
      attributes[:variants].each do |k, v|
        items << ::MapBackgrounds::Layer.new(k, v.try(:[], :url), v.try(:[], :enabled), v.try(:[], :by_default), v.try(:[], :options), provider)
      end
    else
      items << ::MapBackgrounds::Layer.new(:default, provider.url, provider.enabled, provider.by_default, provider.options, provider)
    end
  end
end

.providersObject


55
56
57
# File 'lib/map_backgrounds.rb', line 55

def providers
  items.collect(&:provider_name).uniq
end

Instance Method Details

#labelObject


23
24
25
# File 'lib/map_backgrounds.rb', line 23

def label
  @name == :default ? @provider.label : "#{@provider.label} #{@name.to_s.split('_').join(' ').camelize}"
end

#provider_nameObject


31
32
33
# File 'lib/map_backgrounds.rb', line 31

def provider_name
  @provider.name
end

#reference_nameObject


27
28
29
# File 'lib/map_backgrounds.rb', line 27

def reference_name
  @reference_name ||= "#{@provider.name}.#{@name}"
end