Skip to content

Commit

Permalink
fixup! improve store
Browse files Browse the repository at this point in the history
- add Contracts
- rename store to store_name
- use named arguments
  • Loading branch information
barraq committed Aug 21, 2016
1 parent f76c179 commit 220fe03
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/nanoc/base/repos/checksum_store.rb
Expand Up @@ -6,7 +6,7 @@ module Nanoc::Int
class ChecksumStore < ::Nanoc::Int::Store
# @param [Nanoc::Int::Site] site
def initialize(site: nil)
super(Nanoc::Int::Store.tmp_path_for((site.config.env if site), 'checksums'), 1)
super(Nanoc::Int::Store.tmp_path_for(env: (site.config.env if site), store_name: 'checksums'), 1)

@site = site

Expand Down
2 changes: 1 addition & 1 deletion lib/nanoc/base/repos/compiled_content_cache.rb
Expand Up @@ -5,7 +5,7 @@ module Nanoc::Int
# @api private
class CompiledContentCache < ::Nanoc::Int::Store
def initialize(env: nil)
super(Nanoc::Int::Store.tmp_path_for(env, 'compiled_content'), 2)
super(Nanoc::Int::Store.tmp_path_for(env: env, store_name: 'compiled_content'), 2)

@cache = {}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/nanoc/base/repos/dependency_store.rb
Expand Up @@ -6,7 +6,7 @@ class DependencyStore < ::Nanoc::Int::Store

# @param [Array<Nanoc::Int::Item, Nanoc::Int::Layout>] objects
def initialize(objects, env: nil)
super(Nanoc::Int::Store.tmp_path_for(env, 'dependencies'), 4)
super(Nanoc::Int::Store.tmp_path_for(env: env, store_name: 'dependencies'), 4)

@objects = objects
@graph = Nanoc::Int::DirectedGraph.new([nil] + @objects)
Expand Down
2 changes: 1 addition & 1 deletion lib/nanoc/base/repos/rule_memory_store.rb
Expand Up @@ -5,7 +5,7 @@ module Nanoc::Int
# @api private
class RuleMemoryStore < ::Nanoc::Int::Store
def initialize(env: nil)
super(Nanoc::Int::Store.tmp_path_for(env, 'rule_memory'), 1)
super(Nanoc::Int::Store.tmp_path_for(env: env, store_name: 'rule_memory'), 1)

@rule_memories = {}
end
Expand Down
7 changes: 5 additions & 2 deletions lib/nanoc/base/repos/store.rb
Expand Up @@ -12,6 +12,8 @@ module Nanoc::Int
#
# @api private
class Store
include Nanoc::Int::ContractsSupport

# @return [String] The name of the file where data will be loaded from and
# stored to.
attr_reader :filename
Expand All @@ -36,8 +38,9 @@ def initialize(filename, version)

# Logic for building tmp path from active environment and store name
# @api private
def self.tmp_path_for(env, store)
File.join('tmp', env.to_s, store)
contract C::KeywordArgs[env: C::Maybe[C::Or[String, Symbol]], store_name: C::Or[String, Symbol]] => String
def self.tmp_path_for(env:, store_name:)
File.join('tmp', env.to_s, store_name)
end

# @group Loading and storing data
Expand Down
6 changes: 3 additions & 3 deletions test/base/test_store.rb
Expand Up @@ -34,13 +34,13 @@ def test_delete_and_reload_on_error
end

def test_tmp_path_with_nil_env
tmp_path_for_checksum = Nanoc::Int::Store.tmp_path_for(nil, 'checksum')
tmp_path_for_checksum = Nanoc::Int::Store.tmp_path_for(env: nil, store_name: 'checksum')
assert_equal('tmp/checksum', tmp_path_for_checksum)
end

def test_tmp_path_with_test_env
tmp_path_for_checksum = Nanoc::Int::Store.tmp_path_for('test', 'checksum')
tmp_path_for_dependencies = Nanoc::Int::Store.tmp_path_for(:test, 'dependencies')
tmp_path_for_checksum = Nanoc::Int::Store.tmp_path_for(env: 'test', store_name: 'checksum')
tmp_path_for_dependencies = Nanoc::Int::Store.tmp_path_for(env: :test, store_name: 'dependencies')
assert_equal('tmp/test/checksum', tmp_path_for_checksum)
assert_equal('tmp/test/dependencies', tmp_path_for_dependencies)
end
Expand Down

0 comments on commit 220fe03

Please sign in to comment.