dashed_map 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.
Files changed (4) hide show
  1. data/LICENSE.txt +20 -0
  2. data/README.md +28 -0
  3. data/lib/dashed_map.rb +41 -0
  4. metadata +50 -0
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Instructure, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,28 @@
1
+ # DashedMap
2
+
3
+ Do you ever find yourself wishing you had a method that takes an array of
4
+ words and then return an array of words, some of them combined by a dash?
5
+ Well you're in luck!
6
+
7
+ ## Installation
8
+
9
+ Add the following to your Gemfile:
10
+
11
+ gem 'dashed_map'
12
+
13
+ ## Usage
14
+
15
+ Just call `dashed_map` anywhere. ANYWHERE. It's on Object.
16
+
17
+ If you need it on BasicObject as well (which you probably do), you'll want
18
+ to run the following code:
19
+
20
+ BasicObject.send :include, DashedMap
21
+
22
+ ## Why does it return an array?
23
+
24
+ I don't know.
25
+
26
+ ## How does it decide where to put dashes?
27
+
28
+ Good question.
@@ -0,0 +1,41 @@
1
+ module DashedMap
2
+ # Accepts an array of words and returns an array of words, some of them
3
+ # combined by a dash.
4
+ def dashed_map(words, n=30)
5
+ line_length = 0
6
+ words.inject([]) do |list, word|
7
+
8
+ # Get the length of the word
9
+ word_size = word.size
10
+ # Add 1 for the space preceding the word
11
+ # There is no space added before the first word
12
+ word_size += 1 unless list.empty?
13
+
14
+ # If adding a word takes us over our limit,
15
+ # join two words by a dash and insert that
16
+ if word_size >= n
17
+ word_pieces = []
18
+ ((word_size / 15) + 1).times do |i|
19
+ word_pieces << word[(i * 15)..(((i+1) * 15)-1)]
20
+ end
21
+ word = word_pieces.compact.select{|p| p.length > 0}.join('-')
22
+ list << word
23
+ line_length = word.size
24
+ elsif (line_length + word_size >= n) and not list.empty?
25
+ previous = list.pop
26
+ previous ||= ''
27
+ list << previous + '-' + word
28
+ line_length = word_size
29
+ # Otherwise just add the word to the list
30
+ else
31
+ list << word
32
+ line_length += word_size
33
+ end
34
+
35
+ # Return the list so that inject works
36
+ list
37
+ end
38
+ end
39
+ end
40
+
41
+ Object.send :include, DashedMap
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dashed_map
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Instructure
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-12 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Accepts an array of words and returns an array of words, some of them
15
+ combined by a dash.
16
+ email:
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - LICENSE.txt
22
+ - README.md
23
+ - lib/dashed_map.rb
24
+ homepage: http://github.com/instructure/dashed_map
25
+ licenses:
26
+ - MIT
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: 1.9.3
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: 1.3.5
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 1.8.25
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: Everyone could use some dashed_map
49
+ test_files: []
50
+ has_rdoc: