stringtrain 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in stringtrain.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Mark Seymour
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,49 @@
1
+ # Stringtrain
2
+
3
+ Stringtrain is a handy internal Ruby DSL for creating dynamically pieced-together strings.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'stringtrain'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install stringtrain
18
+
19
+ ## Usage
20
+
21
+ To use Stringtrain, create a new instance of the `Stringtrain::Cab` class, and pass a block to it. Or you can instantize it into a variable, and use the `#part` method separately.
22
+
23
+ ### Global options
24
+
25
+ - `:part_separator`: A string that will serve as the delimiter between parts. Defaults to a whitespace character. (Defaults to `' '`)
26
+
27
+ ### Individual part options
28
+
29
+ - `:condition`: An expression that returns true or false (or just an object). If the expression returns false, then the part does not get added. (Defaults to `true`)
30
+ - `:prefix`: A string that appears before the part. (Defaults to `nil`)
31
+ - `:suffix`: A string that appears after the part. (Defaults to `nil`)
32
+ - `:surround_with`: A string or array that will be used around `string`. If it is an array, the first and last elements will be used for the left and right side of `string`. (Defaults to `' '`)
33
+
34
+ ### A simple example
35
+ *See the [documentation](http://rubydoc.info/github/mseymour/stringtrain/Stringtrain/Cab:part) for more examples.*
36
+
37
+ Stringtrain::Cab.new do
38
+ part 'A monkey loves to eat', suffix: '...'
39
+ part 'bananas', suffix: '!'
40
+ end
41
+ #=> "A monkey loves to eat... bananas!"
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,2 @@
1
+ require "stringtrain/version"
2
+ require "stringtrain/cab"
@@ -0,0 +1,62 @@
1
+ require "active_support/core_ext/object/blank"
2
+
3
+ module Stringtrain
4
+ class Cab
5
+ attr_accessor :parts, :part_separator
6
+
7
+ # @param [Hash] opts Global options for the processor.
8
+ # @option opts [String] :part_separator (' ') A string that will be used as the delimiter between parts.
9
+ def initialize(opts={}, &block)
10
+ opts = { part_separator: ' ' }.merge(opts)
11
+ self.parts = []
12
+ self.part_separator = opts[:part_separator]
13
+ if block_given?
14
+ block.arity == 1 ? yield(self) : instance_eval(&block)
15
+ end
16
+ end
17
+
18
+ # @param [String] string A part of the resulting string.
19
+ # @param [Hash] opts Processing options for the part.
20
+ # @option opts [Boolean] :condition (true) An expression that returns true or false (or just an object). If the expression returns false, then the part does not get added.
21
+ # @option opts [String] :prefix (nil) A string that appears before the part.
22
+ # @option opts [String] :suffix (nil) A string that appears after the part.
23
+ # @option opts [String,Array] :surround_with (nil) A string or array that will be used around +string+. If it is an array, the first and last elements will be used for the left and right side of +string+.
24
+ # @example Adding two parts together with suffices
25
+ # Stringtrain::Cab.new do
26
+ # part 'A monkey loves to eat', suffix: '...'
27
+ # part 'bananas', suffix: '!'
28
+ # end
29
+ # #=> "A monkey loves to eat... bananas!"
30
+ # @example A more complex usage example, making use of `:condition`, `:prefix`, `:suffix`, and `:surround_with`.
31
+ # Stringtrain::Cab.new {|a|
32
+ # a.part cd.current.tracknumber.to_s, surround_with: %w{# :}
33
+ # a.part cd.current.artist, suffix: '-'
34
+ # a.part cd.current.title
35
+ # if cd.current.date
36
+ # a.part '%s (%s)' % [cd.current.album, cd.current.date], surround_with: %w{[ ]}, condition: cd.current.album?
37
+ # else
38
+ # a.part cd.current.album, surround_with: %w{[ ]}, condition: cd.current.album?
39
+ # end
40
+ # }
41
+ # #=> "#08: Loreena McKennitt - Dante's Prayer [The Book Of Secrets (1997)]"
42
+ def part(string, opts={})
43
+ opts = { condition: true, prefix: nil, suffix: nil, surround_with: nil }.merge(opts)
44
+ pieces = [opts[:prefix], string, opts[:suffix]]
45
+ part = if opts[:surround_with].respond_to?(:fetch)
46
+ pieces.insert(-1,opts[:surround_with].last)
47
+ pieces.insert(1,opts[:surround_with].first)
48
+ pieces.compact.join
49
+ else
50
+ pieces.compact.join(opts[:surround_with])
51
+ end
52
+ if !string.blank?
53
+ parts << part if opts[:condition]
54
+ end
55
+ end
56
+
57
+ # @return [String] The resultant string.
58
+ def to_s
59
+ parts.join(self.part_separator)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,4 @@
1
+ # @author Mark Seymour <mark.seymour.ns@gmail.com>
2
+ module Stringtrain
3
+ VERSION = "0.0.1"
4
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/stringtrain/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem "active_support"
6
+
7
+ gem.authors = ["Mark Seymour"]
8
+ gem.email = ["mark.seymour.ns@gmail.com"]
9
+ gem.description = %q{A handly DSL for creating conditionally formatted strings.}
10
+ gem.summary = %q{A cleaner way of making dynamically generated strings with conditions.}
11
+ gem.homepage = "https://github.com/mseymour/stringtrain"
12
+
13
+ gem.files = `git ls-files`.split($\)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.name = "stringtrain"
17
+ gem.require_paths = ["lib"]
18
+ gem.version = Stringtrain::VERSION
19
+ end
data/test/tc_output.rb ADDED
@@ -0,0 +1,31 @@
1
+ require "stringtrain"
2
+ require "test/unit"
3
+
4
+ class TestStringtrain < Test::Unit::TestCase
5
+
6
+ def test_simple
7
+ simple = Stringtrain::Cab.new {
8
+ part "Artist"
9
+ part "Song name"
10
+ part "A time value"
11
+ part "Server Title"
12
+ part ''
13
+ }
14
+ assert_equal('Artist Song name A time value Server Title', simple.to_s)
15
+ end
16
+
17
+ def test_advanced
18
+ a_false_condition = false
19
+
20
+ advanced = Stringtrain::Cab.new(part_separator: '+') {|b|
21
+ b.part "Artist", suffix: '-'
22
+ b.part "Song name"
23
+ b.part "A time value", surround_with: ['(',')'], condition: a_false_condition
24
+ b.part "Server Title", prefix: '/'
25
+ b.part '', prefix: '/'
26
+ }
27
+
28
+ assert_equal('Artist -+Song name+/ Server Title', advanced.to_s)
29
+ end
30
+
31
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stringtrain
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mark Seymour
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-21 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A handly DSL for creating conditionally formatted strings.
15
+ email:
16
+ - mark.seymour.ns@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - lib/stringtrain.rb
27
+ - lib/stringtrain/cab.rb
28
+ - lib/stringtrain/version.rb
29
+ - stringtrain.gemspec
30
+ - test/tc_output.rb
31
+ homepage: https://github.com/mseymour/stringtrain
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.24
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: A cleaner way of making dynamically generated strings with conditions.
55
+ test_files:
56
+ - test/tc_output.rb
57
+ has_rdoc: