fi 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0f1456ebcfaf4ecfddca6d8c6daae088898eb1e2
4
- data.tar.gz: 858eb571c719bbf3824606ff6b8d247f9d16d529
3
+ metadata.gz: 454a505bd26409dfc6582a01e89534d98967dcab
4
+ data.tar.gz: be1adf58b2b9f40276a6ed960e67ae7d2e981ed2
5
5
  SHA512:
6
- metadata.gz: 9db53c445f942e3852736d6423095bd71d58e493003e54a2c0806a2abd9ba2921471cb2021008153ef4eeebbb4dfad4dd515dc3d5435b6d668070d051d871a71
7
- data.tar.gz: e256aecb986402e559d49c405d1c15a9a6f9cf39a801986740bda28a56e64db6b75a8abc61976222e8600e127187c2465db58fc7e48e22e451c3a89c3c5dca38
6
+ metadata.gz: aae9e1719ace11bb274081859923163daa02d1eb94b59ae44be47683cc482fc018f46f968aa47694787c5d6948297a2f5dfb6b919bb8fd3ab7d07d04fbed18e1
7
+ data.tar.gz: 5097dd4364bb39208b5fbef4effe2cecfabf863367603f6027da087d8167a4e4abbcfc195c3ce7cecf7ec0f13f53e00428350eafdb581135d3adcd46882bde70
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
data/Gemfile CHANGED
@@ -1,4 +1,8 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in fi.gemspec
4
3
  gemspec
4
+
5
+ gem 'rspec'
6
+
7
+ gem 'guard-rspec', require: false
8
+ gem 'growl', require: false
data/Guardfile ADDED
@@ -0,0 +1,18 @@
1
+ # directories %w(app lib config test spec feature)
2
+ # clearing :on
3
+ notification :growl
4
+
5
+ guard :rspec, cmd: "bundle exec rspec" do
6
+ require "guard/rspec/dsl"
7
+ dsl = Guard::RSpec::Dsl.new(self)
8
+
9
+ # RSpec files
10
+ rspec = dsl.rspec
11
+ watch(rspec.spec_helper) { rspec.spec_dir }
12
+ watch(rspec.spec_support) { rspec.spec_dir }
13
+ watch(rspec.spec_files)
14
+
15
+ # Ruby files
16
+ ruby = dsl.ruby
17
+ dsl.watch_spec_files_for(ruby.lib_files)
18
+ end
@@ -0,0 +1,5 @@
1
+ Feature: Manipulating file paths
2
+
3
+ Ruby doesn't provide a consistent API to work with file paths. In order to
4
+ eliminate this problem we need to develop a library that facilitate reading,
5
+ creating, and editing files.
data/fi.gemspec CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Fi::VERSION
9
9
  spec.authors = ["Slava Pavlutin"]
10
10
  spec.email = ["sl.pavlutin@gmail.com"]
11
- spec.summary = %q{Working with files never was easier}
11
+ spec.summary = %q{Working with files, easier.}
12
12
  spec.description = %q{Fi - File Interaction}
13
13
  spec.homepage = ""
14
14
  spec.license = "MIT"
data/lib/fi/dir.rb ADDED
@@ -0,0 +1,55 @@
1
+ require "fi/path"
2
+ require "fileutils"
3
+
4
+ module Fi
5
+ class Dir
6
+ def self.current
7
+ ::Dir.pwd
8
+ end
9
+
10
+ def self.change(dir=home)
11
+ ::Dir.chdir dir
12
+ end
13
+
14
+ def self.list(path=current)
15
+ directories = list! path
16
+ directories.reject { |d| d =~ /^\./ }
17
+ end
18
+
19
+ def self.list!(path=current)
20
+ ::Dir.entries Path.expand(path)
21
+ end
22
+
23
+ def self.exist?(dirname)
24
+ ::Dir.exist? dirname
25
+ end
26
+
27
+ def self.delete(dir)
28
+ FileUtils.rm_r dir
29
+ end
30
+
31
+ def self.delete!(dir)
32
+ FileUtils.rm_rf dir
33
+ end
34
+
35
+ def self.rename(dirname, name)
36
+ FileUtils.mv dirname, name
37
+ end
38
+
39
+ def self.home(user=nil)
40
+ return ::Dir.home user if user
41
+ ::Dir.home
42
+ end
43
+
44
+ def initialize(*names)
45
+ names.each do |name|
46
+ dirs = Path.split name
47
+ base_dir = Dir.current
48
+ dirs.each do |dir|
49
+ base_dir = Path.join base_dir, dir
50
+ ::Dir.mkdir base_dir
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
data/lib/fi/file.rb ADDED
@@ -0,0 +1,14 @@
1
+ module Fi
2
+ class File < ::File
3
+ def initialize(path, content='')
4
+ @path = path
5
+ ::File.open(@path, 'w+') do |f|
6
+ f.write content
7
+ end
8
+ end
9
+
10
+ def content
11
+ Fi.read @path
12
+ end
13
+ end
14
+ end
data/lib/fi/path.rb ADDED
@@ -0,0 +1,15 @@
1
+ module Fi
2
+ class Path
3
+ def self.expand(*path)
4
+ File.expand_path File.join(*path)
5
+ end
6
+
7
+ def self.join(*paths)
8
+ File.join *paths
9
+ end
10
+
11
+ def self.split(path)
12
+ path.split '/'
13
+ end
14
+ end
15
+ end
data/lib/fi/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Fi
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/fi.rb CHANGED
@@ -1,5 +1,24 @@
1
+ require "fi/path"
2
+ require "fi/dir"
3
+ require "fi/file"
1
4
  require "fi/version"
2
5
 
3
6
  module Fi
4
- # Your code goes here...
7
+ class << self
8
+ def new(path, content='')
9
+ Fi::File.new path, content
10
+ end
11
+
12
+ def open(path)
13
+ ::File.open path
14
+ end
15
+
16
+ def read(path)
17
+ ::File.read path
18
+ end
19
+
20
+ def delete(path)
21
+ ::File.delete path
22
+ end
23
+ end
5
24
  end
@@ -0,0 +1,112 @@
1
+ require "fi/dir"
2
+
3
+ module Fi
4
+ RSpec.describe Dir do
5
+ let(:root) { Dir.current }
6
+ let(:hidden_files) { /^\./ }
7
+ let(:testdir) { 'testdir' }
8
+
9
+ it "can get current directory" do
10
+ expect(Dir.current).to eq ::Dir.pwd
11
+ end
12
+
13
+ it "can get home directory" do
14
+ expect(Dir.home).to eq ENV['HOME']
15
+ end
16
+
17
+ it "can determine whether a directory exist" do
18
+ expect(Dir.exist? '.').to be true
19
+ expect(Dir.exist? 'nope').to be false
20
+ end
21
+
22
+ it "can rename a directory" do
23
+ Dir.new 't'
24
+ Dir.rename 't', 'temp'
25
+
26
+ expect('temp').to be_directory
27
+ expect('t').not_to be_directory
28
+
29
+ Dir.delete! 'temp'
30
+ end
31
+
32
+ # specify "change current directory" do
33
+ # Dir.change Dir.home
34
+ # expect(Dir.current).to eq Dir.home
35
+ # Dir.change root
36
+ # end
37
+
38
+ describe ".list and .list!" do
39
+ it "lists files in current directory by default" do
40
+ expect(Dir.list).to eq Dir.list Dir.current
41
+ expect(Dir.list!).to eq Dir.list! Dir.current
42
+ end
43
+
44
+ it "lists non-hidden files in a directory" do
45
+ list = Dir.list '~/'
46
+ expect(list).not_to be_empty
47
+ expect(list).not_to include hidden_files
48
+ end
49
+
50
+ it "lists all files in a directory" do
51
+ list = Dir.list! '~/'
52
+ expect(list).not_to be_empty
53
+ expect(list).to include hidden_files
54
+ end
55
+ end
56
+
57
+
58
+ describe ".new" do
59
+ after { Dir.delete! testdir }
60
+
61
+ it "can create a directory" do
62
+ dir = Dir.new testdir
63
+ expect(testdir).to be_directory
64
+ end
65
+
66
+ it "can create nested directories" do
67
+ Dir.new 'new/yet/another'
68
+
69
+ expect('new').to be_directory
70
+ expect('new/yet').to be_directory
71
+ expect('new/yet/another').to be_directory
72
+
73
+ Dir.delete! 'new'
74
+ end
75
+
76
+ it "can create multiply directories" do
77
+ Dir.new testdir, "#{testdir}1"
78
+
79
+ expect(testdir).to be_directory
80
+ expect("#{testdir}1").to be_directory
81
+
82
+ Dir.delete! "#{testdir}1"
83
+ end
84
+
85
+ it "raises an error when directory already exists" do
86
+ Dir.new testdir
87
+
88
+ expect(testdir).to be_directory
89
+ expect { Dir.new testdir }.to raise_error
90
+ end
91
+ end
92
+
93
+
94
+ describe ".delete and .delete!" do
95
+ specify "`.delete` deletes an empty directory" do
96
+ Dir.new 'temp'
97
+ expect('temp').to be_directory
98
+
99
+ Dir.delete 'temp'
100
+ expect('temp').not_to be_directory
101
+ end
102
+
103
+ specify "`.delete!` deletes a non-empty directory" do
104
+ Dir.new 'temp'
105
+ expect('temp').to be_directory
106
+
107
+ Dir.delete! 'temp'
108
+ expect('temp').not_to be_directory
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,20 @@
1
+ require "fi/path"
2
+
3
+ module Fi
4
+ RSpec.describe Path do
5
+ it "expands a relative path" do
6
+ path = Path.expand '~/'
7
+ expect(path).to eq "#{ENV['HOME']}"
8
+ end
9
+
10
+ it "joins paths" do
11
+ path = Path.join '~/', 'dir', 'file'
12
+ expect(path).to eq '~/dir/file'
13
+ end
14
+
15
+ it "joins then expands a path" do
16
+ path = Path.expand '~/', 'dir', 'file'
17
+ expect(path).to eq "#{ENV['HOME']}/dir/file"
18
+ end
19
+ end
20
+ end
data/spec/fi_spec.rb ADDED
@@ -0,0 +1,36 @@
1
+ require "fi"
2
+
3
+ RSpec.describe Fi do
4
+ include SpecHelper::TempFiles
5
+
6
+ before(:each) { create_tempfiles }
7
+ after(:each) { delete_tempfiles }
8
+
9
+ specify "open a file" do
10
+ file = Fi.open tempfile
11
+ expect(file).to be_a File
12
+ end
13
+
14
+ specify "open a file that doesn't exist" do
15
+ expect { Fi.open 'null.tmp' }.to raise_error
16
+ end
17
+
18
+ specify "read a file's content" do
19
+ content = Fi.read tempfile
20
+ expect(content).to eq 'test content'
21
+ end
22
+
23
+ specify "create a new file" do
24
+ file = Fi.new 'newfile.tmp', 'test content'
25
+ expect(file.content).to eq 'test content'
26
+ end
27
+
28
+ specify "delete a file" do
29
+ file = Fi.delete "newfile.tmp"
30
+ expect(file).to eq 1
31
+ end
32
+
33
+ specify "copy a file"
34
+ specify "create a link to a file"
35
+ specify "determine a type of a file"
36
+ end
@@ -0,0 +1,103 @@
1
+ RSpec::Matchers.define :be_directory do |expected|
2
+ match do |actual|
3
+ Dir.exist? actual
4
+ end
5
+ end
6
+
7
+ RSpec::Matchers.define :with_hidden_files do |expected|
8
+ match do |actual|
9
+ true
10
+ end
11
+ end
12
+
13
+ module SpecHelper
14
+ module TempFiles
15
+ def tempfile
16
+ 'testfile.tmp'
17
+ end
18
+
19
+ def create_tempfiles
20
+ ::File.open(tempfile, 'w+') do |f|
21
+ f.write "test content"
22
+ end
23
+ end
24
+
25
+ def delete_tempfiles
26
+ ::File.delete tempfile
27
+ end
28
+ end
29
+ end
30
+
31
+ RSpec.configure do |config|
32
+ # rspec-expectations config goes here. You can use an alternate
33
+ # assertion/expectation library such as wrong or the stdlib/minitest
34
+ # assertions if you prefer.
35
+ config.expect_with :rspec do |expectations|
36
+ # This option will default to `true` in RSpec 4. It makes the `description`
37
+ # and `failure_message` of custom matchers include text for helper methods
38
+ # defined using `chain`, e.g.:
39
+ # be_bigger_than(2).and_smaller_than(4).description
40
+ # # => "be bigger than 2 and smaller than 4"
41
+ # ...rather than:
42
+ # # => "be bigger than 2"
43
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
44
+ end
45
+
46
+ # rspec-mocks config goes here. You can use an alternate test double
47
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
48
+ config.mock_with :rspec do |mocks|
49
+ # Prevents you from mocking or stubbing a method that does not exist on
50
+ # a real object. This is generally recommended, and will default to
51
+ # `true` in RSpec 4.
52
+ mocks.verify_partial_doubles = true
53
+ end
54
+
55
+ # The settings below are suggested to provide a good initial experience
56
+ # with RSpec, but feel free to customize to your heart's content.
57
+ =begin
58
+ # These two settings work together to allow you to limit a spec run
59
+ # to individual examples or groups you care about by tagging them with
60
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
61
+ # get run.
62
+ config.filter_run :focus
63
+ config.run_all_when_everything_filtered = true
64
+
65
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
66
+ # For more details, see:
67
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
68
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
69
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
70
+ config.disable_monkey_patching!
71
+
72
+ # This setting enables warnings. It's recommended, but in some cases may
73
+ # be too noisy due to issues in dependencies.
74
+ config.warnings = true
75
+
76
+ # Many RSpec users commonly either run the entire suite or an individual
77
+ # file, and it's useful to allow more verbose output when running an
78
+ # individual spec file.
79
+ if config.files_to_run.one?
80
+ # Use the documentation formatter for detailed output,
81
+ # unless a formatter has already been configured
82
+ # (e.g. via a command-line flag).
83
+ config.default_formatter = 'doc'
84
+ end
85
+
86
+ # Print the 10 slowest examples and example groups at the
87
+ # end of the spec run, to help surface which specs are running
88
+ # particularly slow.
89
+ config.profile_examples = 10
90
+
91
+ # Run specs in random order to surface order dependencies. If you find an
92
+ # order dependency and want to debug it, you can fix the order by providing
93
+ # the seed, which is printed after each run.
94
+ # --seed 1234
95
+ config.order = :random
96
+
97
+ # Seed global randomization in this process using the `--seed` CLI option.
98
+ # Setting this allows you to use `--seed` to deterministically reproduce
99
+ # test failures related to randomization by passing the same `--seed` value
100
+ # as the one that triggered the failure.
101
+ Kernel.srand config.seed
102
+ =end
103
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Slava Pavlutin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-10 00:00:00.000000000 Z
11
+ date: 2015-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -46,13 +46,23 @@ extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
48
  - ".gitignore"
49
+ - ".rspec"
49
50
  - Gemfile
51
+ - Guardfile
50
52
  - LICENSE.txt
51
53
  - README.md
52
54
  - Rakefile
55
+ - features/path.feature
53
56
  - fi.gemspec
54
57
  - lib/fi.rb
58
+ - lib/fi/dir.rb
59
+ - lib/fi/file.rb
60
+ - lib/fi/path.rb
55
61
  - lib/fi/version.rb
62
+ - spec/fi/dir_spec.rb
63
+ - spec/fi/path_spec.rb
64
+ - spec/fi_spec.rb
65
+ - spec/spec_helper.rb
56
66
  homepage: ''
57
67
  licenses:
58
68
  - MIT
@@ -76,6 +86,11 @@ rubyforge_project:
76
86
  rubygems_version: 2.2.2
77
87
  signing_key:
78
88
  specification_version: 4
79
- summary: Working with files never was easier
80
- test_files: []
89
+ summary: Working with files, easier.
90
+ test_files:
91
+ - features/path.feature
92
+ - spec/fi/dir_spec.rb
93
+ - spec/fi/path_spec.rb
94
+ - spec/fi_spec.rb
95
+ - spec/spec_helper.rb
81
96
  has_rdoc: