Class: Moku::Sites

Inherits:
Object
  • Object
show all
Defined in:
lib/moku/sites.rb,
lib/moku/sites/scope.rb

Overview

A mapping of hosts within zero or more sites.

Defined Under Namespace

Classes: Host, Scope

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sites, default_user = nil) ⇒ Sites

Returns a new instance of Sites.


73
74
75
76
# File 'lib/moku/sites.rb', line 73

def initialize(sites, default_user = nil)
  @sites = sites
  @default_user = default_user
end

Class Method Details

.for(object) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/moku/sites.rb', line 12

def for(object)
  case object
  when Pathname
    from_file(object)
  when String
    from_yaml(object)
  when Hash
    if object.key?("nodes") || object.key?(:nodes)
      from_reverse_hash(object)
    else
      from_hash(object)
    end
  else
    raise ArgumentError, object.inspect
  end
end

.from_file(path) ⇒ Object


29
30
31
# File 'lib/moku/sites.rb', line 29

def from_file(path)
  from_yaml(File.read(path))
end

.from_hash(hash) ⇒ Object


46
47
48
49
50
51
52
53
# File 'lib/moku/sites.rb', line 46

def from_hash(hash)
  hash = hash.symbolize_keys
  sites = hash.reject {|k, _| k == :user }
    .transform_values do |hosts|
      hosts.map {|host| normalize_host(host, hash[:user]) }
    end
  new(sites, hash[:user])
end

.from_reverse_hash(hash) ⇒ Object


37
38
39
40
41
42
43
44
# File 'lib/moku/sites.rb', line 37

def from_reverse_hash(hash)
  hash = hash.symbolize_keys

  sites = Hash.new {|h, k| h[k] = [] }
  hash[:nodes].each {|host, site| sites[site] << host }

  from_hash({ user: hash[:user] }.merge(sites))
end

.from_yaml(yaml) ⇒ Object


33
34
35
# File 'lib/moku/sites.rb', line 33

def from_yaml(yaml)
  from_hash(YAML.safe_load(yaml))
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)

120
121
122
# File 'lib/moku/sites.rb', line 120

def eql?(other)
  sites == other.send(:sites)
end

#host(*hostnames) ⇒ Array<Sites::Host>

The hosts that match the given hostname

Returns:


114
115
116
117
118
# File 'lib/moku/sites.rb', line 114

def host(*hostnames)
  hosts.select do |host|
    hostnames.include? host.hostname
  end
end

#hostsArray<Sites::Host>

All of the hosts in the sites

Returns:


85
86
87
# File 'lib/moku/sites.rb', line 85

def hosts
  sites.values.flatten
end

#primariesArray<Sites::Host>

Only the hosts that are primary to each site

Returns:


91
92
93
# File 'lib/moku/sites.rb', line 91

def primaries
  sites.values.map(&:first).flatten
end

#primarySites::Host

Exactly one host that serves as the primary across sites.

Returns:


98
99
100
# File 'lib/moku/sites.rb', line 98

def primary
  hosts.first
end

#site(*site_names) ⇒ Sites

A new instance that contains only the specified sites.

Returns:


104
105
106
107
108
109
110
# File 'lib/moku/sites.rb', line 104

def site(*site_names)
  self.class.new(
    sites.clone.keep_if do |site_name, _hosts|
      site_names.include? site_name.to_s
    end
  )
end

#site_namesArray<String>

Returns:

  • (Array<String>)

79
80
81
# File 'lib/moku/sites.rb', line 79

def site_names
  sites.keys.map(&:to_s)
end

#to_hObject


124
125
126
127
# File 'lib/moku/sites.rb', line 124

def to_h
  Hash[sites.map {|site, hosts| [site.to_s, hosts_to_h(hosts)] }]
    .tap {|h| h["user"] = default_user if default_user }
end