alegscogs_helpers 0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,8 @@
1
+ Copyright (c) October 6 2011, by Alex Cox
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
8
+
data/README.markdown ADDED
@@ -0,0 +1,77 @@
1
+ Alegscogs Helpers
2
+ -----------------
3
+
4
+ Some views helpers for Rails3 apps that I've found useful.
5
+
6
+ phrasify()
7
+ ----------
8
+
9
+ Takes collections of strings and arrays of strings as arguments, and tries to output meaningful phrases.
10
+
11
+ This is particularly useful when you have dynamic content that has to be rendered in well-constructed phrases.
12
+
13
+
14
+ phrasify 'on', 'jan 4th'
15
+ # => 'on jan 4th'
16
+
17
+ phrasify 'on', ['jan 4th']
18
+ # => 'on jan 4th'
19
+
20
+ phrasify 'on', ['jan 4th', 'dec 8th']
21
+ # => 'on jan 4th and dec 8th'
22
+
23
+ phrasify 'on', ['jan 4th', 'dec 8th', 'march 20th']
24
+ # => 'on jan 4th, dec 8th, and march 20th'
25
+
26
+ phrasify ['on', 'between'], ['jan 4th']
27
+ # => 'on jan 4th'
28
+
29
+ phrasify ['on', 'between'], ['jan 4th', 'dec 8th']
30
+ # => 'between jan 4th and dec 8th'
31
+
32
+ Phrasify invokes rails' Array#to_sentence, and takes its options:
33
+
34
+ phrasify 'on', ['jan 4th', 'dec 8th'], {:last_word_connector => ' or ', :two_words_connector => ' or '}
35
+ # => 'on jan 4th or dec 8th'
36
+
37
+ phrasify ['on', 'on either'], ['jan 4th', 'dec 8th', 'march 20th'], {:last_word_connector => ' or ', :two_words_connector => ' or '}
38
+ # => 'on either jan 4th, dec 8th, or march 20th'
39
+
40
+ Nested arrays will be exploded and passed recursively as arguments:
41
+
42
+ phrasify 'on', ['jan 4th', [['on the morning of', 'on the mornings of'], ['jan 5th', 'jan 6th']]]
43
+ # => 'on jan 4th and on the mornings of jan 5th and jan 6th'
44
+
45
+ An introductory string followed by an empty array returns nil, or gets ignored in the case of nested arguments:
46
+
47
+ phrasify 'on', []
48
+ # => nil
49
+
50
+ phrasify 'on', ['jan 4th', []]
51
+ # => 'on jan 4th'
52
+
53
+ When phrasify is passed a single array, optionally followed by an options hash, after attempting to resolve any nested phrases, it will invoke #to_sentence on the resulting array of strings. In this case, passing a :join option will cause Array#join to be used instead of Array#to_sentence, with the value of the join option passed as parameters to the join call.
54
+
55
+ use_or = {:last_word_connector => ' or ', :two_words_connector => ' or '}
56
+ phrasify( [
57
+ 'Sorry! There are no search results for',
58
+ [['Theater', 'Art & Entertainment'], use_or],
59
+ 'events',
60
+ [['on', 'between'], ['jan 4th', 'jan 15th']],
61
+ ['that', [
62
+ ['are good for', [], use_or],
63
+ ['happen in the', ['morning', 'afternoon'], use_or],
64
+ ['cost', [], use_or]
65
+ ]],
66
+ ], {:join => ' '})
67
+ # => 'Sorry! There are no search results for Theater or Art & Entertainment
68
+ # events between jan 4th and jan 15th that happen in the morning or afternoon'
69
+
70
+
71
+ Copyright (c) October 6 2011, by Alex Cox
72
+
73
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
74
+
75
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
76
+
77
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,9 @@
1
+ require 'alegscogs_helpers/view_helpers'
2
+
3
+ module AlegsCogsHelpers
4
+ class Railtie < Rails::Railtie
5
+ initializer "alegscogs_helpers.view_helpers" do
6
+ ActionView::Base.send :include, ViewHelpers
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,31 @@
1
+ module AlegscogsHelpers
2
+ module ViewHelpers
3
+ def phrasify(*args)
4
+ options = args.extract_options!
5
+ join = options.delete(:join)
6
+ args.map! do |arg|
7
+ if arg.is_a?(Array)
8
+ arg.map!{|e| e.is_a?(Array) ? phrasify(*e) : e }
9
+ else
10
+ arg
11
+ end
12
+ end
13
+
14
+ if args.length == 1
15
+ word = args[0]
16
+ if word.is_a?(String)
17
+ return word
18
+ elsif word.is_a?(Array)
19
+ join ? word.join(*join) : word.to_sentence(options)
20
+ end
21
+ elsif args[1].present?
22
+ word, ary = args[0], [*args[1]]
23
+ if word.is_a?(Array)
24
+ word = word[ary.length - 1] || word.last
25
+ end
26
+ ary.delete_if{|e| e.blank?}
27
+ word + ' ' + ary.to_sentence(options)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1 @@
1
+ require 'alegscogs_helpers/railtie' if defined?(Rails)
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alegscogs_helpers
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: "0"
6
+ platform: ruby
7
+ authors:
8
+ - Alex Cox
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-10-10 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: includes a helper method to generate phrases from dynamic content
17
+ email:
18
+ - alegscogs@gmail.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - lib/alegscogs_helpers/railtie.rb
27
+ - lib/alegscogs_helpers/view_helpers.rb
28
+ - lib/alegscogs_helpers.rb
29
+ - LICENSE
30
+ - README.markdown
31
+ homepage: http://github.com/alegscogs/rails-helpers
32
+ licenses: []
33
+
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.10
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Some useful views helpers for rails3 apps
58
+ test_files: []
59
+