Skip to content

Commit

Permalink
Code tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
jodosha committed Jul 7, 2016
1 parent 3af6256 commit 414d466
Show file tree
Hide file tree
Showing 52 changed files with 334 additions and 303 deletions.
8 changes: 8 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Metrics/LineLength:
Enabled: false
Style/RaiseArgs:
Enabled: false

AllCops:
Exclude:
- 'lib/hanami/assets/compressors/*'
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: ruby
sudo: false
cache: bundler
script: 'bundle exec rake test:coverage --trace'
script: 'bundle exec rake test:coverage --trace && bundle exec rubocop --display-cop-names'
rvm:
- 2.2.5
- 2.3.1
Expand Down
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ unless ENV['TRAVIS']
gem 'yard', require: false
end

gem 'coveralls', require: false

gem 'hanami-utils', '~> 0.8', github: 'hanami/utils', branch: '0.8.x'
gem 'hanami-helpers', '~> 0.4', github: 'hanami/helpers', branch: '0.4.x'
gem 'hanami-view', '~> 0.7', github: 'hanami/view', branch: '0.7.x'

gem 'hanami-emberjs', path: 'test/fixtures/hanami-emberjs', require: false
gem 'hanami-compass', path: 'test/fixtures/hanami-compass', require: false
gem 'hanami-foo-compressor', path: 'test/fixtures/hanami-foo-compressor', require: false

gem 'rubocop', '~> 0.41', require: false
gem 'coveralls', require: false
1 change: 0 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ namespace :test do
end

task default: :test

8 changes: 4 additions & 4 deletions bin/hanami-assets
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ require 'pathname'

options = {}
OptionParser.new do |opts|
opts.banner = "Usage: hanami-assets --config=path/to/config.rb"
opts.banner = 'Usage: hanami-assets --config=path/to/config.rb'

opts.on("-c", "--config FILE", "Path to config") do |c|
opts.on('-c', '--config FILE', 'Path to config') do |c|
options[:config] = c
end
end.parse!

config = options.fetch(:config) { raise ArgumentError.new("You must specify a configuration file") }
config = options.fetch(:config) { raise ArgumentError.new('You must specify a configuration file') }
config = Pathname.new(config)
config.exist? or raise ArgumentError.new("Cannot find configuration file: #{ config }")
config.exist? or raise ArgumentError.new("Cannot find configuration file: #{config}") # rubocop:disable Style/AndOr

require 'hanami/assets'
load config
Expand Down
6 changes: 3 additions & 3 deletions hanami-assets.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ Gem::Specification.new do |spec|
spec.version = Hanami::Assets::VERSION
spec.authors = ['Luca Guidi', 'Trung Lê', 'Alfonso Uceda']
spec.email = ['me@lucaguidi.com', 'trung.le@ruby-journal.com', 'uceda73@gmail.com']
spec.summary = %q{Assets management}
spec.description = %q{Assets management for Ruby web applications}
spec.summary = 'Assets management'
spec.description = 'Assets management for Ruby web applications'
spec.homepage = 'http://hanamirb.org'
spec.license = 'MIT'

spec.files = `git ls-files -- lib/* bin/* CHANGELOG.md LICENSE.md README.md hanami-assets.gemspec`.split($/)
spec.files = `git ls-files -- lib/* bin/* CHANGELOG.md LICENSE.md README.md hanami-assets.gemspec`.split($/) # rubocop:disable Style/SpecialGlobalVars
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']
Expand Down
20 changes: 11 additions & 9 deletions lib/hanami/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def self.load!
# Hanami::Assets.sources << '/path/to/emberjs/assets'
def self.sources
synchronize do
@@sources ||= Config::GlobalSources.new
@@sources ||= Config::GlobalSources.new # rubocop:disable Style/ClassVars
end
end

Expand All @@ -104,11 +104,11 @@ def self.sources
#
# @return [Module] a copy of Hanami::Assets
#
# @since 0.1.0
# @since 0.1.0
#
# @see Hanami::Assets#dupe
# @see Hanami::Assets::Configuration
def self.duplicate(mod, &blk)
def self.duplicate(_mod, &blk)
dupe.tap do |duplicated|
duplicated.configure(&blk) if block_given?
duplicates << duplicated
Expand Down Expand Up @@ -143,16 +143,18 @@ def self.dupe
# @see Hanami::Assets#dupe
def self.duplicates
synchronize do
@@duplicates ||= Array.new
@@duplicates ||= [] # rubocop:disable Style/ClassVars
end
end

private
class << self
private

# @since 0.1.0
# @api private
def self.synchronize(&blk)
Mutex.new.synchronize(&blk)
# @since 0.1.0
# @api private
def synchronize(&blk)
Mutex.new.synchronize(&blk)
end
end
end
end
12 changes: 6 additions & 6 deletions lib/hanami/assets/bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Assets
class Bundler
# @since 0.1.0
# @api private
DEFAULT_PERMISSIONS = 0644
DEFAULT_PERMISSIONS = 0o644

# @since 0.1.0
# @api private
Expand All @@ -36,7 +36,7 @@ class Bundler
# @since 0.1.0
# @api private
def initialize(configuration, duplicates)
@manifest = Hash.new
@manifest = Hash[]
@configuration = configuration
@configurations = if duplicates.empty?
[@configuration]
Expand Down Expand Up @@ -102,14 +102,14 @@ def write_manifest_file
# @since 0.1.0
# @api private
def assets
Dir.glob("#{ public_directory }#{ ::File::SEPARATOR }**#{ ::File::SEPARATOR }*")
Dir.glob("#{public_directory}#{::File::SEPARATOR}**#{::File::SEPARATOR}*")
end

# @since 0.1.0
# @api private
def _convert_to_url(path)
path.sub(public_directory.to_s, URL_REPLACEMENT).
gsub(File::SEPARATOR, URL_SEPARATOR)
path.sub(public_directory.to_s, URL_REPLACEMENT)
.gsub(File::SEPARATOR, URL_SEPARATOR)
end

# @since 0.1.0
Expand All @@ -132,7 +132,7 @@ def _set_permissions(path)
def _configuration_for(asset)
url = _convert_to_url(asset)

@configurations.find {|config| url.start_with?(config.prefix) } ||
@configurations.find { |config| url.start_with?(config.prefix) } ||
@configuration
end

Expand Down
2 changes: 1 addition & 1 deletion lib/hanami/assets/bundler/asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def expanded_path
# @since x.x.x
# @api private
def fingerprinted_target
::File.join(directory, "#{ filename }-#{ fingerprint }#{ extension }")
::File.join(directory, "#{filename}-#{fingerprint}#{extension}")
end

# @since x.x.x
Expand Down
6 changes: 3 additions & 3 deletions lib/hanami/assets/bundler/compressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ def _compress(compressor)
rescue => e
warn(
[
"Skipping compression of: `#{ @path }'",
"Reason: #{ e }\n",
"\t#{ e.backtrace.join("\n\t") }\n\n"
"Skipping compression of: `#{@path}'",
"Reason: #{e}\n",
"\t#{e.backtrace.join("\n\t")}\n\n"
].join("\n")
)
end
Expand Down
16 changes: 10 additions & 6 deletions lib/hanami/assets/bundler/manifest_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class Bundler
# @since x.x.x
# @api private
class ManifestEntry
# @since x.x.x
# @api private
SUBRESOURCE_INTEGRITY_SEPARATOR = '-'.freeze

# Return a new instance
#
# @since x.x.x
Expand All @@ -32,25 +36,25 @@ def name
# @since x.x.x
# @api private
def values
{
Hash[
target: _convert_to_url(@asset.expanded_fingerprinted_target),
subresource_integrity: subresource_integrity_values
}
sri: subresource_integrity_values
]
end

# @since x.x.x
# @api private
def subresource_integrity_values
@asset.configuration.subresource_integrity_algorithms.map do |algorithm|
[ algorithm, @asset.base64_digest(algorithm) ].join('-')
[algorithm, @asset.base64_digest(algorithm)].join(SUBRESOURCE_INTEGRITY_SEPARATOR)
end
end

# @since x.x.x
# @api private
def _convert_to_url(path)
path.sub(@asset.configuration.public_directory.to_s, URL_REPLACEMENT).
gsub(File::SEPARATOR, URL_SEPARATOR)
path.sub(@asset.configuration.public_directory.to_s, URL_REPLACEMENT)
.gsub(File::SEPARATOR, URL_SEPARATOR)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/hanami/assets/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Cache
#
# @return [Hanami::Assets::Cache] a new instance
def initialize
@data = Hash.new{|h,k| h[k] = 0 }
@data = Hash.new { |h, k| h[k] = 0 }
@mutex = Mutex.new
end

Expand Down
22 changes: 12 additions & 10 deletions lib/hanami/assets/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@

module Hanami
module Assets
# Missing Asset error
class MissingAsset < Error
def initialize(name, sources)
sources = sources.map(&:to_s).join(', ')
super("Missing asset: `#{ name }' (sources: #{ sources })")
super("Missing asset: `#{name}' (sources: #{sources})")
end
end

# Unknown Asset Engine error
class UnknownAssetEngine < Error
def initialize(source)
super("No asset engine registered for `#{ ::File.basename(source) }'")
super("No asset engine registered for `#{::File.basename(source)}'")
end
end

Expand All @@ -27,19 +29,19 @@ def initialize(source)
class Compiler
# @since 0.1.0
# @api private
DEFAULT_PERMISSIONS = 0644
DEFAULT_PERMISSIONS = 0o644

# @since 0.1.0
# @api private
COMPILE_PATTERN = '*.*.*'.freeze # Example hello.js.es6

# @since 0.1.0
# @api private
EXTENSIONS = {'.js' => true, '.css' => true, '.map' => true}.freeze
EXTENSIONS = { '.js' => true, '.css' => true, '.map' => true }.freeze

# @since 0.1.0
# @api private
SASS_CACHE_LOCATION = Pathname(Hanami.respond_to?(:root) ?
SASS_CACHE_LOCATION = Pathname(Hanami.respond_to?(:root) ? # rubocop:disable Style/MultilineTernaryOperator
Hanami.root : Dir.pwd).join('tmp', 'sass-cache')

# Compile the given asset
Expand All @@ -66,7 +68,7 @@ def self.compile(configuration, name)
#
# @see Hanami::Assets::Cache
def self.cache
@@cache ||= Assets::Cache.new
@@cache ||= Assets::Cache.new # rubocop:disable Style/ClassVars
end

# Return a new instance
Expand Down Expand Up @@ -111,7 +113,7 @@ def compile
# @api private
def source
@source ||= begin
@name.absolute? ? @name :
@name.absolute? ? @name : # rubocop:disable Style/MultilineTernaryOperator
@configuration.find(@name)
end
end
Expand Down Expand Up @@ -152,7 +154,7 @@ def fresh?
# @api private
def compile?
@compile ||= ::File.fnmatch(COMPILE_PATTERN, ::File.basename(source.to_s)) &&
!EXTENSIONS[::File.extname(source.to_s)]
!EXTENSIONS[::File.extname(source.to_s)]
end

# @since 0.1.0
Expand Down Expand Up @@ -197,7 +199,7 @@ def cache!
# @api private
def write
destination.dirname.mkpath
destination.open(File::WRONLY|File::TRUNC|File::CREAT, DEFAULT_PERMISSIONS) do |file|
destination.open(File::WRONLY | File::TRUNC | File::CREAT, DEFAULT_PERMISSIONS) do |file|
file.write(yield)
end
end
Expand All @@ -224,7 +226,7 @@ def sass_load_paths

# @since x.x.x
# @api private
alias_method :less_load_paths, :sass_load_paths
alias less_load_paths sass_load_paths

# @since 0.1.0
# @api private
Expand Down
4 changes: 2 additions & 2 deletions lib/hanami/assets/compressors/sass_stylesheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SassStylesheet < Stylesheet
# @api private
#
# FIXME This is the same logic that we have for Hanami::Assets::Compiler
SASS_CACHE_LOCATION = Pathname(Hanami.respond_to?(:root) ?
SASS_CACHE_LOCATION = Pathname(Hanami.respond_to?(:root) ? # rubocop:disable Style/MultilineTernaryOperator
Hanami.root : Dir.pwd).join('tmp', 'sass-cache')
# @since 0.1.0
# @api private
Expand All @@ -30,7 +30,7 @@ def initialize
# @api private
def compress(filename)
compressor.new(read(filename), filename: filename, syntax: :scss,
style: :compressed, cache_location: SASS_CACHE_LOCATION).render
style: :compressed, cache_location: SASS_CACHE_LOCATION).render
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/hanami/assets/config/global_sources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def push(*paths)

# @since 0.1.0
# @api private
alias_method :<<, :push
alias << push

private

Expand Down
Loading

0 comments on commit 414d466

Please sign in to comment.