sentencify 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZjU1NmJiYzIwYjE4OGQ3YWVlOTdlNmI3MGVhODgwMTgwMGU1MWFkMQ==
5
+ data.tar.gz: !binary |-
6
+ YjNjMTRhZDI1YThmNGRiNTM5ODEwMWNlOGQ1ZDQzMmFmMTQyYWViMA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NjA0M2RhZmE3YWJiZTYxYzlkZTdkMWFiMDI1YzlhMjVjOTZhZGQzODlmY2Ey
10
+ YjhlMTI3OTY2NDYxODQyZmIzZjliODcxYTIwZDA5ZDJlMDkxZDU4ZmQxMjIw
11
+ Mjc4MWIzYzQ2M2UxOWFmY2QyMTc4YTQyMzNhMDExOGQ5NDQ1OGM=
12
+ data.tar.gz: !binary |-
13
+ MmUxYTc2NGRlZjE2NDU3NjJhMmI3NjBiNDIzNTY0OTJiMzg3MTlkYzIwMDU1
14
+ ZThkNGI2NzE4MzJjNTM5OGU0ZmZkYjYzMTA2M2E3YmM2YzIxZGRjYWIyNWQ3
15
+ NzlhMDU5NGYwNGVlMjU5ZWYyZmZhNzI1MDA0NWNhZjMxOGU5MGE=
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.3.0 (25/07/13)
2
+
3
+ * Sentencize images with `sentencize_images` helper
4
+ * Refactor specs
5
+
1
6
  ## 0.2.0 (11/07/13)
2
7
 
3
8
  * Add specs
data/README.md CHANGED
@@ -26,11 +26,13 @@ Or install it yourself as:
26
26
  $ gem install sentencify
27
27
 
28
28
  ## Usage
29
+
29
30
  ### Default
30
31
 
31
32
  Considering that you have an `User` model with a `login` field:
32
33
 
33
34
  In your controller:
35
+
34
36
  ```rb
35
37
  def index
36
38
  @users = User.all
@@ -38,22 +40,28 @@ end
38
40
  ```
39
41
 
40
42
  In your `index.html.erb` view:
43
+
41
44
  ```rb
42
45
  @users.sentencize(on: :login)
43
- @users.sentencize(on: :avatar, image: true)
44
46
  ```
45
47
 
46
48
  It will display all your users by login with a default limit of 5.
47
49
 
48
50
  You can change this value by passing a limit parameter as following: `limit: 10`
49
51
 
50
- **Extra**: You can display images by turning the image option to true.
51
-
52
52
  ### Advanced
53
+
54
+ #### Images
55
+
56
+ You can use the `sentencize_images` helper if you are in a Rails app.
57
+
58
+ ```rb
59
+ sentencize_images(@users, on: :avatar)
60
+ ```
61
+
53
62
  #### Parameters
54
63
 
55
64
  Others parameters:
56
- * `image`, default value: false
57
65
  * `empty`, default value: 'No element found'
58
66
  * `words_connector`, default value: ', '
59
67
  * `two_words_connector`, default value: ' and '
@@ -64,6 +72,7 @@ Others parameters:
64
72
  #### I18n
65
73
 
66
74
  You can also set the connectors via I18n by passing a dictionary into the form:
75
+
67
76
  ```yml
68
77
  fr:
69
78
  sentencify:
@@ -0,0 +1,9 @@
1
+ require 'sentencify/sentencify_helper'
2
+
3
+ module Sentencify
4
+ class Railtie < Rails::Railtie
5
+ initializer 'sentencize.sentencify_helper' do
6
+ ActionView::Base.send :include, SentencifyHelper
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ module Sentencify
2
+ module SentencifyHelper
3
+ def sentencize_images(to_sentencify = [], options = {})
4
+ will_sentencized = to_sentencify.map { |o| image_tag(o.send(options[:on])) }
5
+ raw(will_sentencized.sentencize(options.merge({ image: true })))
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,51 @@
1
+ require 'active_support/core_ext/array'
2
+
3
+ class Array
4
+ def sentencize(options = {})
5
+ assert_sentenciable options
6
+
7
+ default_connectors = {
8
+ on: :title,
9
+ image: false,
10
+ limit: 5,
11
+ empty: 'No element found',
12
+ words_connector: ', ',
13
+ two_words_connector: ' and ',
14
+ last_word_connector: ' and ',
15
+ final_singular_connector: ' other',
16
+ final_plural_connector: ' others'
17
+ }
18
+
19
+ if defined?(I18n)
20
+ i18n_connectors = I18n.translate(:sentencify, default: {})
21
+ default_connectors.merge! i18n_connectors
22
+ end
23
+
24
+ options = default_connectors.merge! options
25
+ will_sentencized = self.dup
26
+ will_sentencized = will_sentencized.map! { |o| o[options[:on]] } unless options[:image]
27
+
28
+ case length
29
+ when 0
30
+ options[:empty]
31
+ when 1
32
+ will_sentencized[0]
33
+ when 2
34
+ "#{will_sentencized[0]}#{options[:two_words_connector]}#{will_sentencized[1]}"
35
+ else
36
+ if options[:limit] >= length
37
+ "#{will_sentencized[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{will_sentencized[-1]}"
38
+ else
39
+ nb_others = length - options[:limit]
40
+ others = (nb_others != 1) ? options[:final_plural_connector] : options[:final_singular_connector]
41
+ "#{will_sentencized[0..options[:limit]-1].join(options[:words_connector])}#{options[:last_word_connector]}#{nb_others}#{others}"
42
+ end
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def assert_sentenciable(options = {})
49
+ options.assert_valid_keys(:on, :image, :limit, :empty, :words_connector, :two_words_connector, :last_word_connector, :final_singular_connector, :final_plural_connector)
50
+ end
51
+ end
@@ -1,3 +1,3 @@
1
1
  module Sentencify
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
data/lib/sentencify.rb CHANGED
@@ -1,46 +1,2 @@
1
- require 'active_support/core_ext/array'
2
-
3
- class Array
4
- def sentencize(options = {})
5
- options.assert_valid_keys(:on, :image, :limit, :empty, :words_connector, :two_words_connector, :last_word_connector, :final_singular_connector, :final_plural_connector)
6
-
7
- default_connectors = {
8
- on: :title,
9
- image: false,
10
- limit: 5,
11
- empty: 'No element found',
12
- words_connector: ', ',
13
- two_words_connector: ' and ',
14
- last_word_connector: ' and ',
15
- final_singular_connector: ' other',
16
- final_plural_connector: ' others'
17
- }
18
-
19
- if defined?(I18n)
20
- i18n_connectors = I18n.translate(:sentencify, default: {})
21
- default_connectors.merge!(i18n_connectors)
22
- end
23
-
24
- options = default_connectors.merge!(options)
25
-
26
- will_sentencized = self.map { |o| o[options[:on]] }
27
- will_sentencized.map! { |s| "<img src='#{s}' />" } if options[:image]
28
-
29
- case length
30
- when 0
31
- options[:empty]
32
- when 1
33
- will_sentencized[0]
34
- when 2
35
- "#{will_sentencized[0]}#{options[:two_words_connector]}#{will_sentencized[1]}"
36
- else
37
- if options[:limit] >= length
38
- "#{will_sentencized[0...-1].join(options[:words_connector])}#{options[:last_word_connector]}#{will_sentencized[-1]}"
39
- else
40
- nb_others = length-options[:limit]
41
- others = (nb_others != 1) ? options[:final_plural_connector] : options[:final_singular_connector]
42
- "#{will_sentencized[0..options[:limit]-1].join(options[:words_connector])}#{options[:last_word_connector]}#{nb_others}#{others}"
43
- end
44
- end
45
- end
46
- end
1
+ require 'sentencify/sentencize'
2
+ require 'sentencify/railtie' if defined?(Rails)
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+ require 'sentencify/sentencize'
3
+
4
+ describe Array do
5
+ subject { [] }
6
+
7
+ describe '#assert_sentenciable' do
8
+ it { expect { subject.send(:assert_sentenciable, on: :field) }.to_not raise_error }
9
+
10
+ it { expect { subject.send(:assert_sentenciable, image: :field) }.to_not raise_error }
11
+
12
+ it { expect { subject.send(:assert_sentenciable, limit: :field) }.to_not raise_error }
13
+
14
+ it { expect { subject.send(:assert_sentenciable, empty: :field) }.to_not raise_error }
15
+
16
+ it { expect { subject.send(:assert_sentenciable, words_connector: :field) }.to_not raise_error }
17
+
18
+ it { expect { subject.send(:assert_sentenciable, two_words_connector: :field) }.to_not raise_error }
19
+
20
+ it { expect { subject.send(:assert_sentenciable, last_word_connector: :field) }.to_not raise_error }
21
+
22
+ it { expect { subject.send(:assert_sentenciable, final_singular_connector: :field) }.to_not raise_error }
23
+
24
+ it { expect { subject.send(:assert_sentenciable, final_plural_connector: :field) }.to_not raise_error }
25
+
26
+ it { expect { subject.send(:assert_sentenciable, dummy: :field) }.to raise_error }
27
+ end
28
+
29
+ describe '#sentencize' do
30
+ let(:options) { {} }
31
+
32
+ it 'calls #assert_sentenciable with options' do
33
+ subject.should_receive(:assert_sentenciable).with(options)
34
+ subject.sentencize(options)
35
+ end
36
+
37
+ context 'when there is no object' do
38
+ it 'returns a no element found string' do
39
+ subject.sentencize(on: :login).should == 'No element found'
40
+ end
41
+ end
42
+
43
+ context 'when there is one object' do
44
+ subject { [
45
+ { name: 'Baptiste Lecocq', login: 'Tiste' }
46
+ ] }
47
+
48
+ it 'returns a one element string' do
49
+ subject.sentencize(on: :login).should == 'Tiste'
50
+ end
51
+ end
52
+
53
+ context 'when there is two objects' do
54
+ subject { [
55
+ { name: 'Baptiste Lecocq', login: 'Tiste' },
56
+ { name: 'Chuck Norris', login: 'Chucky' }
57
+ ] }
58
+
59
+ it 'returns a two elements string' do
60
+ subject.sentencize(on: :login).should == 'Tiste and Chucky'
61
+ end
62
+ end
63
+
64
+ context 'when there is a number of objects less than the limit' do
65
+ subject { [
66
+ { name: 'Baptiste Lecocq', login: 'Tiste' },
67
+ { name: 'Chuck Norris', login: 'Chucky' },
68
+ { name: 'Van Damme', login: 'JCVD' }
69
+ ] }
70
+
71
+ it 'returns a two elements string' do
72
+ subject.sentencize(on: :login).should == 'Tiste, Chucky and JCVD'
73
+ end
74
+ end
75
+
76
+ context 'when there is a number of objects greater than the limit' do
77
+ subject { [
78
+ { name: 'Baptiste Lecocq', login: 'Tiste' },
79
+ { name: 'Chuck Norris', login: 'Chucky' },
80
+ { name: 'Van Damme', login: 'JCVD' }
81
+ ] }
82
+
83
+ it 'returns a two elements string' do
84
+ subject.sentencize(on: :login, limit: 2).should == 'Tiste, Chucky and 1 other'
85
+ end
86
+ end
87
+ end
88
+ end
data/spec/spec_helper.rb CHANGED
@@ -2,8 +2,8 @@ require 'coveralls'
2
2
  Coveralls.wear!
3
3
 
4
4
  RSpec.configure do |config|
5
- config.filter_run :focus
6
- config.order = 'random'
7
- config.run_all_when_everything_filtered = true
8
- config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.filter_run :focus
6
+ config.order = 'random'
7
+ config.run_all_when_everything_filtered = true
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
9
  end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sentencify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
5
- prerelease:
4
+ version: 0.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Baptiste Lecocq
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-07-11 00:00:00.000000000 Z
11
+ date: 2013-07-25 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activesupport
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: bundler
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ! '>='
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ! '>='
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: coveralls
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ! '>='
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ! '>='
60
53
  - !ruby/object:Gem::Version
@@ -62,7 +55,6 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rake
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - ! '>='
68
60
  - !ruby/object:Gem::Version
@@ -70,7 +62,6 @@ dependencies:
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - ! '>='
76
67
  - !ruby/object:Gem::Version
@@ -78,7 +69,6 @@ dependencies:
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: rspec
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
73
  - - ! '>='
84
74
  - !ruby/object:Gem::Version
@@ -86,7 +76,6 @@ dependencies:
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
80
  - - ! '>='
92
81
  - !ruby/object:Gem::Version
@@ -111,35 +100,37 @@ files:
111
100
  - README.md
112
101
  - Rakefile
113
102
  - lib/sentencify.rb
103
+ - lib/sentencify/railtie.rb
104
+ - lib/sentencify/sentencify_helper.rb
105
+ - lib/sentencify/sentencize.rb
114
106
  - lib/sentencify/version.rb
115
107
  - sentencify.gemspec
116
- - spec/sentencify_spec.rb
108
+ - spec/sentencify/sentencize_spec.rb
117
109
  - spec/spec_helper.rb
118
110
  homepage: https://github.com/baptistelecocq/sentencify
119
111
  licenses:
120
112
  - MIT
113
+ metadata: {}
121
114
  post_install_message:
122
115
  rdoc_options: []
123
116
  require_paths:
124
117
  - lib
125
118
  required_ruby_version: !ruby/object:Gem::Requirement
126
- none: false
127
119
  requirements:
128
120
  - - ! '>='
129
121
  - !ruby/object:Gem::Version
130
122
  version: '0'
131
123
  required_rubygems_version: !ruby/object:Gem::Requirement
132
- none: false
133
124
  requirements:
134
125
  - - ! '>='
135
126
  - !ruby/object:Gem::Version
136
127
  version: '0'
137
128
  requirements: []
138
129
  rubyforge_project:
139
- rubygems_version: 1.8.25
130
+ rubygems_version: 2.0.5
140
131
  signing_key:
141
- specification_version: 3
132
+ specification_version: 4
142
133
  summary: Create sentences with array of Active Record objects
143
134
  test_files:
144
- - spec/sentencify_spec.rb
135
+ - spec/sentencify/sentencize_spec.rb
145
136
  - spec/spec_helper.rb
@@ -1,99 +0,0 @@
1
- require 'spec_helper'
2
- require 'sentencify'
3
-
4
- describe Array do
5
- let(:to_sentence) { [] }
6
-
7
- describe '#sentencize' do
8
- it 'allows :on option' do
9
- expect { to_sentence.sentencize(on: :field) }.to_not raise_error
10
- end
11
-
12
- it 'allows :image option' do
13
- expect { to_sentence.sentencize(image: :field) }.to_not raise_error
14
- end
15
-
16
- it 'allows :limit option' do
17
- expect { to_sentence.sentencize(limit: :field) }.to_not raise_error
18
- end
19
-
20
- it 'allows :empty option' do
21
- expect { to_sentence.sentencize(empty: :field) }.to_not raise_error
22
- end
23
-
24
- it 'allows :words_connector option' do
25
- expect { to_sentence.sentencize(words_connector: :field) }.to_not raise_error
26
- end
27
-
28
- it 'allows :two_words_connector option' do
29
- expect { to_sentence.sentencize(two_words_connector: :field) }.to_not raise_error
30
- end
31
-
32
- it 'allows :last_word_connector option' do
33
- expect { to_sentence.sentencize(last_word_connector: :field) }.to_not raise_error
34
- end
35
-
36
- it 'allows :final_singular_connector option' do
37
- expect { to_sentence.sentencize(final_singular_connector: :field) }.to_not raise_error
38
- end
39
-
40
- it 'allows :final_plural_connector option' do
41
- expect { to_sentence.sentencize(final_plural_connector: :field) }.to_not raise_error
42
- end
43
-
44
- it 'denies other options' do
45
- expect { to_sentence.sentencize(test: :field) }.to raise_error
46
- end
47
-
48
- context 'when there is no object' do
49
- it 'returns a no element found string' do
50
- to_sentence.sentencize(on: :login).should == 'No element found'
51
- end
52
- end
53
-
54
- context 'when there is one object' do
55
- let(:to_sentence) { [
56
- { name: 'Baptiste Lecocq', login: 'Tiste' }
57
- ] }
58
-
59
- it 'returns a one element string' do
60
- to_sentence.sentencize(on: :login).should == 'Tiste'
61
- end
62
- end
63
-
64
- context 'when there is two objects' do
65
- let(:to_sentence) { [
66
- { name: 'Baptiste Lecocq', login: 'Tiste' },
67
- { name: 'Chuck Norris', login: 'Chucky' }
68
- ] }
69
-
70
- it 'returns a two elements string' do
71
- to_sentence.sentencize(on: :login).should == 'Tiste and Chucky'
72
- end
73
- end
74
-
75
- context 'when there is a number of objects less than the limit' do
76
- let(:to_sentence) { [
77
- { name: 'Baptiste Lecocq', login: 'Tiste' },
78
- { name: 'Chuck Norris', login: 'Chucky' },
79
- { name: 'Van Damme', login: 'JCVD' }
80
- ] }
81
-
82
- it 'returns a two elements string' do
83
- to_sentence.sentencize(on: :login).should == 'Tiste, Chucky and JCVD'
84
- end
85
- end
86
-
87
- context 'when there is a number of objects greater than the limit' do
88
- let(:to_sentence) { [
89
- { name: 'Baptiste Lecocq', login: 'Tiste' },
90
- { name: 'Chuck Norris', login: 'Chucky' },
91
- { name: 'Van Damme', login: 'JCVD' }
92
- ] }
93
-
94
- it 'returns a two elements string' do
95
- to_sentence.sentencize(on: :login, limit: 2).should == 'Tiste, Chucky and 1 other'
96
- end
97
- end
98
- end
99
- end