Class: Ghost::Store::HostsFileStore
- Inherits:
-
Object
- Object
- Ghost::Store::HostsFileStore
- Defined in:
- lib/ghost/store/hosts_file_store.rb
Overview
TODO: A lot of this duplicates Resolv::Hosts in Ruby stdlib.
Can that be modifiied to use tokens in place of this?
Constant Summary collapse
- MAX_HOSTS_PER_LINE =
5
- DEFAULT_FILE =
Resolv::Hosts::DefaultFileName
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
-
#path ⇒ Object
Returns the value of attribute path.
-
#section_name ⇒ Object
Returns the value of attribute section_name.
Instance Method Summary collapse
- #add(host) ⇒ Object
- #all ⇒ Object
- #delete(host) ⇒ Object
- #delete_host(host, buffer, strict = false) ⇒ Object
- #empty ⇒ Object
- #find(filter) ⇒ Object
-
#initialize(options = {}) ⇒ HostsFileStore
constructor
A new instance of HostsFileStore.
- #purge(host) ⇒ Object
- #set(host) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ HostsFileStore
Returns a new instance of HostsFileStore.
18 19 20 21 22 23 24 25 |
# File 'lib/ghost/store/hosts_file_store.rb', line 18 def initialize( = {}) self.path = .fetch(:path, DEFAULT_FILE) self.section_name = .fetch(:section_name) self.file = Ghost::TokenizedFile.new(self.path, "# #{self.section_name} start", "# #{self.section_name} end") end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
15 16 17 |
# File 'lib/ghost/store/hosts_file_store.rb', line 15 def file @file end |
#path ⇒ Object
Returns the value of attribute path.
15 16 17 |
# File 'lib/ghost/store/hosts_file_store.rb', line 15 def path @path end |
#section_name ⇒ Object
Returns the value of attribute section_name.
16 17 18 |
# File 'lib/ghost/store/hosts_file_store.rb', line 16 def section_name @section_name end |
Instance Method Details
#add(host) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/ghost/store/hosts_file_store.rb', line 34 def add(host) sync do |buffer| buffer[host.ip] << host.name buffer_changed! end true end |
#all ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/ghost/store/hosts_file_store.rb', line 53 def all sync do |buffer| buffer.map do |ip, hosts| hosts.map { |h| Ghost::Host.new(h, ip) } end.flatten.sort end end |
#delete(host) ⇒ Object
65 66 67 68 69 |
# File 'lib/ghost/store/hosts_file_store.rb', line 65 def delete(host) sync do |buffer| delete_host host, buffer, :strict end end |
#delete_host(host, buffer, strict = false) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ghost/store/hosts_file_store.rb', line 77 def delete_host(host, buffer, strict = false) result = SortedSet.new buffer.each do |ip, names| names.each do |name| if host.kind_of? Host next unless host.name == name elsif host.kind_of? String next unless host == name else next unless host.match(name) end next if host.respond_to?(:ip) && host.ip != ip && strict result << Ghost::Host.new(name, ip) names.delete(name) buffer_changed! end end result.to_a end |
#empty ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ghost/store/hosts_file_store.rb', line 99 def empty result = false sync do |buffer| unless buffer.empty? result = true buffer.replace({}) buffer_changed! end end result end |
#find(filter) ⇒ Object
61 62 63 |
# File 'lib/ghost/store/hosts_file_store.rb', line 61 def find(filter) all.select { |host| host.name =~ filter } end |
#purge(host) ⇒ Object
71 72 73 74 75 |
# File 'lib/ghost/store/hosts_file_store.rb', line 71 def purge(host) sync do |buffer| delete_host host, buffer end end |
#set(host) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/ghost/store/hosts_file_store.rb', line 43 def set(host) sync do |buffer| delete_host host, buffer buffer[host.ip] << host.name buffer_changed! end true end |