starlight_helpers 0.999

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8688281a73acbca107c30b410a34421c72b17082
4
+ data.tar.gz: 5cf7cda036a7709c06a1fa624ddd234388d0ff0c
5
+ SHA512:
6
+ metadata.gz: 12ec82b224b7b0eda96486bc59a5aef3b4d6630b464a2652402900eeacdb148f8635fd360a29e3e52f6bcb3bdcfd076bae1897da673e3816381276e3bb1e4072
7
+ data.tar.gz: f6dbf1dafbd2865e693869beff5984253e7eaba74aa090a63f7bd934a4f186afabc4c6ee64257dabbcd11b7aa9df1b5f3031e0124b8dd98ef72a979cf26b89a3
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 starlight_helpers.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ivan Stana
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,39 @@
1
+ # StarlightHelpers
2
+
3
+ ## Why
4
+
5
+ I needed common used methods when I'm programming websites in one place, not in /lib directories across more projects and with different updates. And this time I've added unit tests. Finally the feeling, that my code is falling apart, is gone.
6
+
7
+ And I'm sharing it with YOU! Enjoy.
8
+
9
+ ## Usage
10
+
11
+ Read the source or tests. They are simple. Of course I add documentation later.
12
+
13
+ ## Roadmap
14
+
15
+ - better rendering of the content (allow options for markdown), add textile, escape entities in plaintext
16
+ - add stylistic filters to the content renderer - change of quotes, remove double spaces and such eye candy things
17
+
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ gem 'starlight_helpers'
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install starlight_helpers
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( http://github.com/istana/starlight_helpers/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList['test/*_test.rb']
7
+ t.verbose = true
8
+ end
@@ -0,0 +1,6 @@
1
+ module StarlightHelpers
2
+ require_relative './content'
3
+ require_relative './flash'
4
+ require_relative './locale'
5
+ require_relative './model'
6
+ end
@@ -0,0 +1,28 @@
1
+ require 'redcarpet'
2
+ require 'action_view'
3
+
4
+ module StarlightHelpers
5
+ module Content
6
+
7
+ def link_to_selected(selected = false, name = nil, options = nil, html_options = {}, &block)
8
+ if selected
9
+ html_options.merge!(class: 'selected')
10
+ end
11
+ link_to(name, options, html_options, &block)
12
+ end
13
+
14
+ def convert_content(content, markup)
15
+ if markup == 'plain'
16
+ content.gsub("\n", '<br>')
17
+ elsif markup == 'markdown'
18
+ markdown = ::Redcarpet::Markdown.new(::Redcarpet::Render::HTML)
19
+ markdown.render(content)
20
+ elsif markup == 'html'
21
+ # nothing
22
+ content
23
+ end
24
+ end
25
+
26
+ end
27
+ end
28
+
@@ -0,0 +1,48 @@
1
+ require 'rack'
2
+ require 'action_dispatch/middleware/flash'
3
+
4
+ module StarlightHelpers
5
+ module Flash
6
+
7
+ def flash_alert(message)
8
+ flash_something(:alert, message)
9
+ end
10
+
11
+ def flash_notice(message)
12
+ flash_something(:notice, message)
13
+ end
14
+
15
+ def flash_something(what, message)
16
+ type = what.to_sym
17
+ if flash[type].is_a?(String)
18
+ flash[type] = [flash[type], message]
19
+ else
20
+ flash[type] ||= []
21
+ flash[type] << message
22
+ end
23
+ end
24
+
25
+ def display_flash(what, &code)
26
+ type = what.to_sym
27
+ # something like Devise has overwritten flash
28
+ if flash[type].is_a?(String)
29
+ flash[type] = [flash[type]]
30
+ end
31
+
32
+ if flash[type]
33
+ result = flash[type].inject([]) do |res, msg|
34
+ res << (block_given? ? code.call(msg) : msg)
35
+ end
36
+ end
37
+
38
+ # sometimes flash isn't cleared after action
39
+ # "Please create or set 'default_locale'" is shown
40
+ # multiple times
41
+ # better clear flash, it was already shown anyway
42
+ flash.delete(type)
43
+ result ? result.join(" "): nil
44
+ end
45
+
46
+ end
47
+ end
48
+
@@ -0,0 +1,58 @@
1
+ module StarlightHelpers
2
+ module Locale
3
+ # en_US
4
+ # the first segment is language, the second the country
5
+ # isocode has only "-", allow "_" too like in gettext
6
+ def format_inline
7
+ /([a-zA-Z]{2})(?:([-_])([a-zA-Z]{2}))?/
8
+ end
9
+
10
+ def format
11
+ Regexp.new("\\A#{format_inline}\\z")
12
+ end
13
+
14
+ # function parses user agent string from web browser (Accept-Language)
15
+ # and returns sorted array of {quality => code} segments
16
+ def parse_accept_language(ualangs)
17
+ # quality? format
18
+ qf = "q=(1|0\.[1-9])"
19
+ lang_format = "#{format_inline}(; ?#{qf})?"
20
+ langs_format = /\A(#{lang_format}(,#{lang_format}){0,7})\z/
21
+
22
+ if ualangs =~ langs_format
23
+ langs = ualangs.split(',').map(&:strip)
24
+ langs = langs.inject({}) do |result, language|
25
+ segment = language.split(';').map(&:strip)
26
+ # add q=1 for language if not defined
27
+ if segment.size == 1
28
+ segment << 1
29
+ elsif segment.size == 2
30
+ segment[1] = segment[1].gsub('q=', '').to_f
31
+ end
32
+ result.merge!(segment[0] => segment[1])
33
+ end
34
+ Hash[langs.sort_by { |code, quality| -quality }]
35
+ else
36
+ {}
37
+ end
38
+ end
39
+
40
+ def valid_locale?(loc)
41
+ loc =~ format ? true : false
42
+ end
43
+
44
+ def language(loc)
45
+ loc.match(format)[1]
46
+ end
47
+
48
+ def country(loc)
49
+ loc.match(format)[3]
50
+ end
51
+
52
+ def country?(loc)
53
+ loc.match(format)[3] != nil
54
+ end
55
+ alias_method :full_locale?, :country?
56
+ end
57
+ end
58
+
@@ -0,0 +1,12 @@
1
+ require 'active_support/inflector'
2
+
3
+ module StarlightHelpers
4
+ module Model
5
+ # converts text to nice uri
6
+ def make_uri(string)
7
+ uri = ActiveSupport::Inflector.transliterate(string)
8
+ uri.parameterize
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,3 @@
1
+ module StarlightHelpers
2
+ VERSION = "0.999"
3
+ end
@@ -0,0 +1,4 @@
1
+ require "starlight_helpers/version"
2
+
3
+ module StarlightHelpers
4
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'starlight_helpers/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "starlight_helpers"
8
+ spec.version = StarlightHelpers::VERSION
9
+ spec.authors = ["Ivan Stana"]
10
+ spec.email = ["stiipa@centrum.sk"]
11
+ spec.summary = %q{Helper modules for building websites. Flash, content, locale and model.}
12
+ spec.description = %q{Helper modules for building websites. Relax and use often used methods in flash, content, locale and model areas. Used and tests available.}
13
+ spec.homepage = "http://github.com/istana/starlight_helpers"
14
+ spec.license = "MIT"
15
+ spec.post_install_message = "'Enjoy starlight helpers!' With love your Star..."
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "actionpack"
23
+ spec.add_dependency "redcarpet"
24
+ spec.add_dependency "rack"
25
+ spec.add_dependency "action_view"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.5"
28
+ spec.add_development_dependency "rake"
29
+ spec.add_development_dependency "minitest"
30
+ end
@@ -0,0 +1,26 @@
1
+ require_relative './test_helper'
2
+ require 'action_view'
3
+
4
+ class LesterHelper
5
+ extend ActionView::Helpers::TagHelper
6
+ extend ActionView::Helpers::UrlHelper
7
+ extend StarlightHelpers::Content
8
+ end
9
+
10
+ describe StarlightHelpers::Content do
11
+ it '#link_to_selected' do
12
+ assert_equal '<a class="selected" href="#">Ahoy</a>',
13
+ LesterHelper.link_to_selected(true, 'Ahoy', '#')
14
+ assert_equal '<a href="#">Ahoy</a>',
15
+ LesterHelper.link_to_selected(false, 'Ahoy', '#')
16
+ end
17
+
18
+ it '#render_text' do
19
+ assert_equal 'Hello<br>world',
20
+ LesterHelper.convert_content("Hello\nworld", 'plain')
21
+ assert_equal "<p><em>Hello</em></p>\n",
22
+ LesterHelper.convert_content("*Hello*", 'markdown')
23
+ assert_equal '<font color="navy">Hello</font>',
24
+ LesterHelper.convert_content('<font color="navy">Hello</font>', 'html')
25
+ end
26
+ end
@@ -0,0 +1,72 @@
1
+ require 'erb'
2
+ require_relative './test_helper'
3
+
4
+ class Lester
5
+ attr_accessor :flash
6
+ include StarlightHelpers::Flash
7
+
8
+ def initialize
9
+ @flash = ActionDispatch::Flash::FlashHash.new({})
10
+ end
11
+ end
12
+
13
+ describe StarlightHelpers::Flash do
14
+ before(:each) do
15
+ @lester = Lester.new
16
+ end
17
+
18
+ it "#flash_alert" do
19
+ @lester.flash_alert(":-O")
20
+ assert_equal [":-O"], @lester.flash[:alert]
21
+ assert_nil @lester.flash[:another]
22
+ end
23
+
24
+ it "#flash_notice" do
25
+ @lester.flash_notice(";-)")
26
+ assert_equal [";-)"], @lester.flash[:notice]
27
+ assert_nil @lester.flash[:another]
28
+ end
29
+
30
+ it "#flash_something" do
31
+ @lester.flash_something(:world, "I'm the best")
32
+ @lester.flash_something(:world, "Am I good or am I good?")
33
+ assert_equal ["I'm the best", "Am I good or am I good?"], @lester.flash[:world]
34
+ assert_nil @lester.flash[:another]
35
+ end
36
+
37
+ it "#flash_something handles preexisting string" do
38
+ @lester.flash[:kayla] = "<3"
39
+ @lester.flash_something(:kayla, "^_^")
40
+ assert_equal ["<3", "^_^"], @lester.flash[:kayla]
41
+ end
42
+
43
+ it "#display_flash without block" do
44
+ @lester.flash_notice(";-)")
45
+ @lester.flash_notice("8-)")
46
+
47
+ result = ERB.new(<<-EOF
48
+ <%= display_flash(:notice) %>
49
+ EOF
50
+ # this is weird, and public method or .send doesn't work
51
+ ).result(@lester.instance_eval { binding })
52
+ assert_equal ";-) 8-)\n", result
53
+ assert_nil @lester.flash[:notice]
54
+ end
55
+
56
+ it "#display_flash with the block" do
57
+ @lester.flash_notice("hello")
58
+ @lester.flash_notice("world")
59
+
60
+ # ERB is bit weird, cannot do "_#{msg}_", becase
61
+ # NameError: undefined local variable or method `msg'
62
+ # must be concatenated
63
+ template = <<-EOF
64
+ <% display_flash(:notice) do |msg| %>
65
+ <%= "_"+msg+"_" %>
66
+ <% end %>
67
+ EOF
68
+ result_with_block = ERB.new(template, nil, '<>').result(@lester.instance_eval { binding })
69
+ assert_equal "_hello__world_", result_with_block
70
+ assert_nil @lester.flash[:notice]
71
+ end
72
+ end
@@ -0,0 +1,46 @@
1
+ require_relative './test_helper'
2
+
3
+ class Lester
4
+ extend StarlightHelpers::Locale
5
+ end
6
+
7
+ describe StarlightHelpers::Locale do
8
+ it '#valid_locale?' do
9
+ assert Lester.valid_locale?('sk')
10
+ assert Lester.valid_locale?('sk_SK')
11
+ assert Lester.valid_locale?('sk-SK')
12
+
13
+ assert !Lester.valid_locale?('sks')
14
+ assert !Lester.valid_locale?('sks_SK')
15
+ assert !Lester.valid_locale?('sk_SKs')
16
+ assert !Lester.valid_locale?('sks_SKs')
17
+ assert !Lester.valid_locale?('0a_SK')
18
+ assert !Lester.valid_locale?('0')
19
+ assert !Lester.valid_locale?('0_SK')
20
+ end
21
+
22
+ it '#country?, #full_locale?' do
23
+ assert Lester.country?('sk_SK')
24
+ assert !Lester.country?('sk')
25
+ assert Lester.full_locale?('sk_SK')
26
+ assert !Lester.full_locale?('sk')
27
+ end
28
+
29
+ it '#country' do
30
+ assert_equal 'US', Lester.country('en_US')
31
+ assert_nil Lester.country('sk')
32
+ end
33
+
34
+ it '#language' do
35
+ assert_equal 'en', Lester.language('en_US')
36
+ assert_equal 'sk', Lester.language('sk')
37
+ end
38
+
39
+ it '#parse_accept_language' do
40
+ assert_equal({'sk' => 1, 'cs' => 0.8, 'en-US' => 0.5, 'en' => 0.3}, Lester.parse_accept_language("sk,cs;q=0.8,en-US;q=0.5,en;q=0.3"))
41
+ assert_equal({'sk' => 1, 'cs' => 0.8, 'en-US' => 0.5, 'en' => 0.3}, Lester.parse_accept_language("cs;q=0.8,en-US;q=0.5,sk,en;q=0.3"))
42
+ assert_equal({}, Lester.parse_accept_language("sk;q=9000"))
43
+ assert_equal({}, Lester.parse_accept_language("sk;q=0"))
44
+ assert_equal({}, Lester.parse_accept_language("I can has?"))
45
+ end
46
+ end
@@ -0,0 +1,13 @@
1
+ require_relative './test_helper'
2
+
3
+ class Lester
4
+ extend StarlightHelpers::Model
5
+ end
6
+
7
+ describe StarlightHelpers::Model do
8
+ it '#make_uri' do
9
+ assert "cucoriedky-nase-rastu-v-lese",
10
+ Lester.make_uri("Čučoriedky naše rastú v lese! \n")
11
+ end
12
+ end
13
+
@@ -0,0 +1,5 @@
1
+ #require 'bundler/setup'
2
+ require_relative '../lib/starlight_helpers/all'
3
+ require 'minitest/autorun'
4
+
5
+ I18n.enforce_available_locales = false
metadata ADDED
@@ -0,0 +1,167 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: starlight_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.999'
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Stana
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionpack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: redcarpet
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: rack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: action_view
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Helper modules for building websites. Relax and use often used methods
112
+ in flash, content, locale and model areas. Used and tests available.
113
+ email:
114
+ - stiipa@centrum.sk
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - lib/starlight_helpers.rb
125
+ - lib/starlight_helpers/all.rb
126
+ - lib/starlight_helpers/content.rb
127
+ - lib/starlight_helpers/flash.rb
128
+ - lib/starlight_helpers/locale.rb
129
+ - lib/starlight_helpers/model.rb
130
+ - lib/starlight_helpers/version.rb
131
+ - starlight_helpers.gemspec
132
+ - test/content_test.rb
133
+ - test/flash_test.rb
134
+ - test/locale_test.rb
135
+ - test/model_test.rb
136
+ - test/test_helper.rb
137
+ homepage: http://github.com/istana/starlight_helpers
138
+ licenses:
139
+ - MIT
140
+ metadata: {}
141
+ post_install_message: "'Enjoy starlight helpers!' With love your Star..."
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.2.0
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: Helper modules for building websites. Flash, content, locale and model.
161
+ test_files:
162
+ - test/content_test.rb
163
+ - test/flash_test.rb
164
+ - test/locale_test.rb
165
+ - test/model_test.rb
166
+ - test/test_helper.rb
167
+ has_rdoc: