Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extract class Cane::File to handle encoding in one place.

  • Loading branch information...
commit 354e509086d07aae3b01726c22510c9c9c9a695f 1 parent d1a7339
@xaviershay xaviershay authored
View
5 lib/cane/abc_check.rb
@@ -1,8 +1,9 @@
require 'ripper'
+require 'set'
+require 'cane/file'
require 'cane/abc_max_violation'
require 'cane/syntax_violation'
-require 'set'
module Cane
@@ -19,7 +20,7 @@ def violations
protected
def find_violations(file_name)
- ast = Ripper::SexpBuilder.new(File.open(file_name, 'r:utf-8').read).parse
+ ast = Ripper::SexpBuilder.new(Cane::File.contents(file_name)).parse
case ast
when nil
InvalidAst.new(file_name)
View
4 lib/cane/cli/spec.rb
@@ -42,8 +42,8 @@ def parse(args)
end
def get_default_options
- if File.exists?('./.cane')
- File.read('./.cane').gsub("\n", ' ').split(' ')
+ if ::File.exists?('./.cane')
+ ::File.read('./.cane').gsub("\n", ' ').split(' ')
else
[]
end
View
5 lib/cane/doc_check.rb
@@ -1,4 +1,4 @@
-require 'cane/encoding_aware_iterator'
+require 'cane/file'
module Cane
@@ -17,8 +17,7 @@ def violations
def find_violations(file_name)
last_line = ""
- i = EncodingAwareIterator.new(File.open(file_name, 'r:utf-8').lines)
- i.map_with_index do |line, number|
+ Cane::File.iterator(file_name).map_with_index do |line, number|
result = if class_definition?(line) && !comment?(last_line)
UndocumentedClassViolation.new(file_name, number + 1, line)
end
View
22 lib/cane/file.rb
@@ -0,0 +1,22 @@
+require 'cane/encoding_aware_iterator'
+
+module Cane
+
+ # An interface for interacting with files that ensures encoding is handled in
+ # a consistent manner.
+ class File
+ class << self
+ def iterator(path)
+ EncodingAwareIterator.new(open(path).lines)
+ end
+
+ def contents(path)
+ open(path).read
+ end
+
+ def open(path)
+ ::File.open(path, 'r:utf-8')
+ end
+ end
+ end
+end
View
6 lib/cane/style_check.rb
@@ -1,7 +1,7 @@
require 'set'
+require 'cane/file'
require 'cane/style_violation'
-require 'cane/encoding_aware_iterator'
module Cane
@@ -41,9 +41,7 @@ def measure
end
def map_lines(file_path, &block)
- EncodingAwareIterator
- .new(File.open(file_path, 'r:utf-8').lines)
- .map_with_index(&block)
+ Cane::File.iterator(file_path).map_with_index(&block)
end
def exclusions
View
3  lib/cane/threshold_check.rb
@@ -1,3 +1,4 @@
+require 'cane/file'
require 'cane/threshold_violation'
# Configurable check that allows the contents of a file to be compared against
@@ -15,7 +16,7 @@ def violations
def value_from_file(file)
begin
- contents = File.read(file).chomp.to_f
+ contents = Cane::File.contents(file).chomp.to_f
rescue Errno::ENOENT
UnavailableValue.new
end
Please sign in to comment.
Something went wrong with that request. Please try again.