filename-helper 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 115866ca42211c7107dbaf11e7c02b806fa54c5f
4
+ data.tar.gz: d94301070718a6064c77cdbb9813814a04a50102
5
+ SHA512:
6
+ metadata.gz: a5e274242fac396d8f4dc76f3e93b1b2564db1eb6330a60e525b0a93343df585f9b22e7fe0156ee6801b155605a94006eb01bf03b3e5364311065271973afef0
7
+ data.tar.gz: f75f3c97d50f652472b01c0a2b9a7221596f6b4c5a5d5fb6c60b39faa20dd3e69c3b12231f94af345d1f540c4fb5d3020dca9dacfa47d4f322117bb27e4fa5f0
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in filename.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Max Power
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # Filename
2
+
3
+ Simple Filename helper. Works well with Pathname.
4
+
5
+ Pathname.new('ruby') + Filename.new('love', :rb)
6
+
7
+ [![GitHub version](https://badge.fury.io/gh/max-power%2Ffilename.png)](http://badge.fury.io/gh/max-power%2Ffilename)
8
+ [![Build Status](https://travis-ci.org/max-power/filename.png?branch=master)](https://travis-ci.org/max-power/filename)
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'filename', github: 'max-power/filename'
15
+
16
+ ## Usage
17
+
18
+ Filename.new('Gemfile').to_s
19
+ # 'Gemfile'
20
+
21
+ Filename.new('image', 'jpg').to_s
22
+ # 'image.jpg'
23
+
24
+ Filename.new('index', ['html', 'erb']).to_s
25
+ #'index.html.erb'
26
+
27
+ Filename.new('index', 'html', nil, 'erb', nil).to_s
28
+ # 'index.html.erb'
29
+
30
+ Filename.new(nil, 'htaccess').to_s
31
+ # '.htaccess'
32
+
33
+ Filename.parse('landscape.jpg').suffix('400x300', 'greyscale', 'inverted').to_s
34
+ # 'landscape_400x300_greyscale_inverted.jpg'
35
+
36
+ Filename.parse('landscape.jpg').prefix('very', 'nice', seperator: '--').to_s
37
+ # 'very--nice--landscape.jpg'
38
+
39
+ Filename is build to work well with Pathname:
40
+
41
+ Pathname.new('/this/is-a') + Filename.new('big', 'test', 'file')
42
+ # '/this/is-a/big.test.file'
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it ( http://github.com/max-power/filename/fork )
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.pattern = 'spec/**/*_spec.rb'
6
+ t.libs << 'spec'
7
+ end
8
+
9
+ desc "Run tests"
10
+ task :default => :test
data/filename.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'filename/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "filename-helper"
8
+ spec.version = Filename::VERSION
9
+ spec.authors = ["Max Power"]
10
+ spec.email = ["kevin.melchert@gmail.com"]
11
+ spec.summary = %q{Simple Filename helper. Works well with Pathname.}
12
+ spec.description = %q{Simple Filename helper. Works well with Pathname.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+ end
data/lib/filename.rb ADDED
@@ -0,0 +1,31 @@
1
+ require "filename/version"
2
+
3
+ class Filename
4
+ def self.parse(value)
5
+ new *value.to_s.split('.')
6
+ end
7
+
8
+ def initialize(base, *extensions)
9
+ @base, @extensions = clean(base), clean(extensions)
10
+ end
11
+
12
+ def suffix(*values, seperator: '_')
13
+ @base.push(clean(values).unshift('').join(seperator)) && self
14
+ end
15
+
16
+ def prefix(*values, seperator: '_')
17
+ @base.unshift(clean(values).push('').join(seperator)) && self
18
+ end
19
+
20
+ def to_s
21
+ @extensions.unshift(@base.join).join('.')
22
+ end
23
+
24
+ alias_method :to_path, :to_s
25
+
26
+ private
27
+
28
+ def clean(input)
29
+ Array(input).flatten.map { |v| v.to_s.strip }.reject(&:empty?)
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ class Filename
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,91 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'rubygems'
3
+ require 'bundler/setup'
4
+ require 'minitest/autorun'
5
+ require 'minitest/spec'
6
+
7
+ require 'filename'
8
+ require 'pathname'
9
+
10
+ describe Filename do
11
+ it "should create a filename without extension" do
12
+ Filename.new('Gemfile').to_s.must_equal 'Gemfile'
13
+ end
14
+
15
+ it "should create a filename with extension" do
16
+ Filename.new('image', 'jpg').to_s.must_equal 'image.jpg'
17
+ end
18
+
19
+ it "should create a filename with more than one extension" do
20
+ Filename.new('index', ['html', 'erb']).to_s.must_equal 'index.html.erb'
21
+ end
22
+
23
+ it "should create a filename with more than one extension as array and remove nil values" do
24
+ Filename.new('index', ['html', nil, 'erb', nil]).to_s.must_equal 'index.html.erb'
25
+ end
26
+
27
+ it "should create a filename with more than one extension as arguments and remove nil values" do
28
+ Filename.new('index', 'html', nil, 'erb', nil).to_s.must_equal 'index.html.erb'
29
+ end
30
+
31
+ it "should create a filename with extension, but without base" do
32
+ Filename.new(nil, 'htaccess').to_s.must_equal '.htaccess'
33
+ end
34
+
35
+ it "should respond to_path" do
36
+ Filename.new('Gemfile').to_path.must_equal 'Gemfile'
37
+ end
38
+
39
+ it "should work in combination with Pathname" do
40
+ pathname = Pathname.new('/this/is/a') + Filename.new('test', 'file')
41
+ pathname.to_s.must_equal '/this/is/a/test.file'
42
+ end
43
+
44
+ it "should parse a string" do
45
+ f = Filename.parse('index.html.erb')
46
+ f.instance_variable_get(:@base).must_equal ['index']
47
+ f.instance_variable_get(:@extensions).must_equal ['html', 'erb']
48
+ end
49
+
50
+ it "should parse a string without extension" do
51
+ f = Filename.parse('Gemfile')
52
+ f.instance_variable_get(:@base).must_equal ['Gemfile']
53
+ f.instance_variable_get(:@extensions).must_equal []
54
+ end
55
+
56
+ it "should parse a string without basename" do
57
+ f = Filename.parse('.htaccess')
58
+ f.instance_variable_get(:@base).must_equal []
59
+ f.instance_variable_get(:@extensions).must_equal ['htaccess']
60
+ f.to_s.must_equal '.htaccess'
61
+ end
62
+
63
+ it "should extend the basename" do
64
+ f = Filename.parse('image.jpg').suffix('200x200', 'greyscale')
65
+ f.to_s.must_equal "image_200x200_greyscale.jpg"
66
+ end
67
+
68
+ it "should extend the basename with custom seperator" do
69
+ f = Filename.parse('image.jpg').suffix('200x200', 'greyscale', seperator: '--')
70
+ f.to_s.must_equal "image--200x200--greyscale.jpg"
71
+ end
72
+
73
+ it "should extend the basename and strip stupid values" do
74
+ f = Filename.parse('image.jpg').suffix(' 200x200 ', nil, 'greyscale', ' ', 1)
75
+ f.to_s.must_equal "image_200x200_greyscale_1.jpg"
76
+ end
77
+
78
+ it "should prefix the basename" do
79
+ f = Filename.parse('image.jpg').prefix('thumbnail', 'japan-trip')
80
+ f.to_s.must_equal "thumbnail_japan-trip_image.jpg"
81
+ end
82
+
83
+ it "should chain prefix und suffix method calls" do
84
+ Filename.parse('cheese.jpg').prefix('old').suffix('cake').to_s.must_equal "old_cheese_cake.jpg"
85
+ end
86
+
87
+ it "should chain prefix und suffix method calls with custom seperators" do
88
+ Filename.parse('cheese.jpg').prefix('very', seperator: 'old').suffix('cake', 2, seperator: '').to_s.must_equal "veryoldcheesecake2.jpg"
89
+ end
90
+
91
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: filename-helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Max Power
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Simple Filename helper. Works well with Pathname.
56
+ email:
57
+ - kevin.melchert@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - filename.gemspec
69
+ - lib/filename.rb
70
+ - lib/filename/version.rb
71
+ - spec/filename_spec.rb
72
+ homepage: ''
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.2.0
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Simple Filename helper. Works well with Pathname.
96
+ test_files:
97
+ - spec/filename_spec.rb
98
+ has_rdoc: