Class: HyperResource::Configuration

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

Overview

HyperResource::Configuration is a class which implements a hostmask- scoped set of configurations. Key/value pairs are stored under hostmasks like ‘api.example.com:8080’, ‘api.example.com’, ‘*.example.com’, or ‘*’. Values are retrieved using a hostname and a key, preferring more specific hostmasks when more than one matching hostmask and key are present.

HyperResource users are not expected to use this class directly.

Instance Method Summary collapse

Constructor Details

#initialize(cfg = {}) ⇒ Configuration

Creates a new HyperResource::Configuration, with the given initial internal state if provided.



17
18
19
20
# File 'lib/hyper_resource/configuration.rb', line 17

def initialize(cfg={})
  @cfg = cfg
  @cfg['*'] ||= {}
end

Instance Method Details

#as_hashObject

Returns this object as a Hash.



64
65
66
# File 'lib/hyper_resource/configuration.rb', line 64

def as_hash
  clone.send(:cfg)
end

#cloneObject

Returns a deep copy of this object.



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

def clone
  self.class.new.send(:initialize_copy, self)
end

#config(hash) ⇒ Object

Applies a given Hash of configurations on top of this one.



59
60
61
# File 'lib/hyper_resource/configuration.rb', line 59

def config(hash)
  merge!(self.class.new(hash))
end

#get(mask, key) ⇒ Object

Returns the value for a particular hostmask and key, or nil if not present.



70
71
72
73
# File 'lib/hyper_resource/configuration.rb', line 70

def get(mask, key)
  cfg[mask] ||= {}
  cfg[mask][key.to_s]
end

#get_for_url(url, key) ⇒ Object

Returns the best matching value for the given URL and key, or nil otherwise.



83
84
85
# File 'lib/hyper_resource/configuration.rb', line 83

def get_for_url(url, key)
  subconfig_for_url(url)[key.to_s]
end

#matching_masks_for_url(url) ⇒ Object

Returns hostmasks from our config which match the given url.



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hyper_resource/configuration.rb', line 99

def matching_masks_for_url(url)
  url = url.to_s
  return ['*'] if !url || cfg.keys.count == 1
  @masks ||= {} ## key = mask string, value = FuzzyURL
  cfg.keys.each {|key| @masks[key] ||= FuzzyURL.new(key) }

  ## Test for matches, and sort by score.
  scores = {}
  cfg.keys.each {|key| scores[key] = @masks[key].match(url) }
  scores = Hash[ scores.select{|k,v| v} ] # remove nils
  scores.keys.sort_by{|k| [-scores[k], -k.length]} ## TODO length is cheesy
end

#merge(new) ⇒ Object

Merges a given Configuration with this one. Deep-merges config attributes correctly.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hyper_resource/configuration.rb', line 29

def merge(new)
  merged_cfg = {}
  new_cfg = new.send(:cfg)

  (new_cfg.keys | cfg.keys).each do |mask|
    new_cfg_attrs = new_cfg[mask] || {}
    cfg_attrs = cfg[mask] || {}
    merged_cfg[mask] = {}

    ## Do a hash merge when it makes sense, except when it doesn't.
    (new_cfg_attrs.keys | cfg_attrs.keys).each do |attr|
      if !(%w(namespace adapter).include?(attr)) &&
         ((!cfg_attrs[attr] || cfg_attrs[attr].kind_of?(Hash)) &&
          (!new_cfg_attrs[attr] || new_cfg_attrs[attr].kind_of?(Hash)))
        merged_cfg[mask][attr] =
          (cfg_attrs[attr] || {}).merge(new_cfg_attrs[attr] || {})
      else
        merged_cfg[mask][attr] = new_cfg_attrs[attr] || cfg_attrs[attr]
      end
    end
  end
  self.class.new(merged_cfg)
end

#merge!(new) ⇒ Object

Applies a given Configuration on top of this one.



54
55
56
# File 'lib/hyper_resource/configuration.rb', line 54

def merge!(new)
  initialize_copy(merge(new))
end

#set(mask, key, value) ⇒ Object

Sets a key and value pair for the given hostmask.



76
77
78
79
# File 'lib/hyper_resource/configuration.rb', line 76

def set(mask, key, value)
  cfg[mask] ||= {}
  cfg[mask][key.to_s] = value
end

#set_for_url(url, key, value) ⇒ Object

Sets a key and value pair, using the given URL as the basis of the hostmask. Path, query, and fragment are ignored.



89
90
91
92
93
94
95
# File 'lib/hyper_resource/configuration.rb', line 89

def set_for_url(url, key, value)
  furl = FuzzyURL.new(url || '*')
  furl.path = nil
  furl.query = nil
  furl.fragment = nil
  set(furl.to_s, key, value)
end