Class: ManifestDestiny

Inherits:
Object
  • Object
show all
Defined in:
lib/manifest-destiny.rb

Defined Under Namespace

Modules: Rails Classes: Config

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ ManifestDestiny

Returns a new instance of ManifestDestiny.



81
82
83
84
85
86
87
88
# File 'lib/manifest-destiny.rb', line 81

def initialize(options = {}, &block)
  @cache = options[:cache]
  @root = Pathname.new(options[:root] || Dir.pwd)
  
  if block_given?
    @config = Config.new(@root, &block)
  end
end

Class Method Details

.configure(*args, &block) ⇒ Object



77
78
79
# File 'lib/manifest-destiny.rb', line 77

def self.configure(*args, &block)
  new(*args, &block)
end

Instance Method Details

#to_sObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/manifest-destiny.rb', line 90

def to_s
  body = ["CACHE MANIFEST"]

  # Generate sha hash of cache and watch files
  hash = (@config.cache + @config.watch).map do |item|
    if (item =~ /:\/\//)
      Digest::SHA2.hexdigest(item)
    else
      begin
        path = File.new(File.join(@root, item))
        Digest::SHA2.hexdigest(path.read) if ::File.file?(path)
      rescue
        $stderr.puts "Couldn't find file #{File.join(@root, item)}, not adding to cache hash"
        nil
      end
    end
  end
  body << "# #{Digest::SHA2.hexdigest(hash.compact.join)}"

  unless @config.cache.empty?
    body << "" << "CACHE:"
    body.concat @config.cache.uniq
  end

  unless @config.network.empty?
    body << "" << "NETWORK:"
    body.concat @config.network.uniq
  end

  unless @config.fallback.empty?
    body << "" << "FALLBACK:"
    @config.fallback.each do |namespace, url|
      body << "#{namespace} #{URI.escape(url.to_s)}"
    end
  end
  
  return body.join("\n")
end