maini-utils 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/maini/utils.rb +13 -0
- data/lib/maini/utils/active_record/default_value.rb +56 -0
- data/lib/maini/utils/active_record/inquirer.rb +40 -0
- data/lib/maini/utils/active_record/random_string.rb +77 -0
- data/lib/maini/utils/error.rb +23 -0
- data/lib/maini/utils/extensions/array.rb +12 -0
- data/lib/maini/utils/extensions/string.rb +64 -0
- data/lib/maini/utils/railtie.rb +27 -0
- data/lib/maini/utils/random_string.rb +24 -0
- data/lib/maini/utils/version.rb +7 -0
- data/lib/maini/utils/view_helpers.rb +94 -0
- data/maini-utils.gemspec +30 -0
- metadata +152 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a24935d7d86ba3356e256fdadeee0dcf351fe091
|
4
|
+
data.tar.gz: 4722c64afe5bc6f737e50c4c97531d0a0650bcc5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 65a88bdab890c70eb382d4319e48ec2c6409bb2bac6a1a80fce372786b57008df7af3dc048e559b91ead18a9e20355c5b3008f08f5607c67acd9b677e6f72c64
|
7
|
+
data.tar.gz: acbeca5a455c2ad0d3e190a05510069a71567a5b27a889dbe9cca2d3c2b2a27ffe11523e390f20dd7d231b1afeeba0cf10438d4392cb8eb052debb51229d1c62
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
maini
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.2
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Marcos Junior
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Maini::Utils
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/maini/utils`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'maini-utils'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install maini-utils
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it ( https://github.com/[my-github-username]/maini-utils/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 a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "maini/utils"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/lib/maini/utils.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
|
3
|
+
require "maini/utils/version"
|
4
|
+
require "maini/utils/extensions/array"
|
5
|
+
require "maini/utils/extensions/string"
|
6
|
+
require "maini/utils/error"
|
7
|
+
require "maini/utils/railtie" if defined?(Rails)
|
8
|
+
|
9
|
+
module Maini
|
10
|
+
module Utils
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Maini
|
2
|
+
module Utils
|
3
|
+
module ActiveRecord
|
4
|
+
module DefaultValue
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
def has_default_values?
|
13
|
+
false
|
14
|
+
end
|
15
|
+
|
16
|
+
def default_value(field, block, options = {})
|
17
|
+
self.send :include, ModelExtensions unless self.has_default_values?
|
18
|
+
self.default_value_definitions[field] = options.merge(:block => block)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
module ModelExtensions
|
24
|
+
|
25
|
+
def self.included(base)
|
26
|
+
base.extend ClassMethods
|
27
|
+
base.before_validation :_set_default_values
|
28
|
+
end
|
29
|
+
|
30
|
+
def _set_default_values
|
31
|
+
self.class.default_value_definitions.each do |field, opts|
|
32
|
+
if self.send(field).blank?
|
33
|
+
proposed_value = self.instance_exec(&opts[:block])
|
34
|
+
self.send("#{field}=", proposed_value)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
module ClassMethods
|
40
|
+
|
41
|
+
def default_value_definitions
|
42
|
+
@default_value_definitions ||= {}
|
43
|
+
end
|
44
|
+
|
45
|
+
def has_default_values?
|
46
|
+
true
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Maini
|
2
|
+
module Utils
|
3
|
+
module ActiveRecord
|
4
|
+
module Inquirer
|
5
|
+
|
6
|
+
#:nodoc:
|
7
|
+
def self.included(base)
|
8
|
+
base.extend ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
|
13
|
+
# Allows you to automatically create inquiry methods for string field. For example, if you have
|
14
|
+
# an Order model which has a status field containing 'approved' or 'delivered' you may wish to
|
15
|
+
# have a #approved? or #delivered? method on the model.
|
16
|
+
#
|
17
|
+
# class Order < ActiveRecord::Baser
|
18
|
+
# STATUSES = ['approved', 'delivered']
|
19
|
+
# inquirer :status, *STATUSES
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# order = Order.new(:status => 'approved')
|
23
|
+
# order.approved? #=> true
|
24
|
+
# order.delivered? #=> false
|
25
|
+
#
|
26
|
+
#
|
27
|
+
def inquirer(field, *options)
|
28
|
+
options.each do |option|
|
29
|
+
define_method "#{option}?" do
|
30
|
+
self.read_attribute(field).to_s == option.to_s
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
require 'maini/utils/random_string'
|
3
|
+
|
4
|
+
module Maini
|
5
|
+
module Utils
|
6
|
+
module ActiveRecord
|
7
|
+
module RandomString
|
8
|
+
|
9
|
+
def self.included(base)
|
10
|
+
base.extend ClassMethods
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.random_string(type, opts = {})
|
14
|
+
case type.to_sym
|
15
|
+
when :uuid
|
16
|
+
SecureRandom.uuid
|
17
|
+
when :chars
|
18
|
+
Maini::Utils::RandomString.generate(opts)
|
19
|
+
else
|
20
|
+
SecureRandom.hex(opts[:length] || 24)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module ClassMethods
|
25
|
+
|
26
|
+
def has_random_strings?
|
27
|
+
false
|
28
|
+
end
|
29
|
+
|
30
|
+
def random_string(field, options = {})
|
31
|
+
self.send :include, ModelExtensions unless self.has_random_strings?
|
32
|
+
self.random_string_fields[field] = options
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
module ModelExtensions
|
38
|
+
|
39
|
+
def self.included(base)
|
40
|
+
base.extend ClassMethods
|
41
|
+
base.before_validation :generate_random_strings
|
42
|
+
end
|
43
|
+
|
44
|
+
def generate_random_strings
|
45
|
+
self.class.random_string_fields.each do |field, opts|
|
46
|
+
if self.send(field).blank?
|
47
|
+
if opts[:unique]
|
48
|
+
until self.send(field)
|
49
|
+
proposed_string = RandomString.random_string(opts[:type], opts)
|
50
|
+
unless self.class.where(field => proposed_string).exists?
|
51
|
+
self.send("#{field}=", proposed_string)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
else
|
55
|
+
self.send("#{field}=", RandomString.random_string(opts[:type], opts))
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
module ClassMethods
|
62
|
+
|
63
|
+
def random_string_fields
|
64
|
+
@random_string_fields ||= {}
|
65
|
+
end
|
66
|
+
|
67
|
+
def has_random_strings?
|
68
|
+
true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Maini
|
4
|
+
class Error
|
5
|
+
|
6
|
+
def initialize(type, message, custom_json = nil)
|
7
|
+
@type = type
|
8
|
+
@message = I18n.t("maini.errors.#{type}", :default => message)
|
9
|
+
@custom_json = custom_json
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_json(options={})
|
13
|
+
@obj = {:errors => { @type => [@message] }}
|
14
|
+
|
15
|
+
if @custom_json
|
16
|
+
@obj[:errors][@type] = [@message,@custom_json]
|
17
|
+
end
|
18
|
+
|
19
|
+
@obj.to_json
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'digest'
|
2
|
+
|
3
|
+
class String
|
4
|
+
|
5
|
+
def to_bool
|
6
|
+
return true if ['true', '1', 'yes', 'on', 't'].include? self
|
7
|
+
return false if ['false', '0', 'no', 'off', 'f'].include? self
|
8
|
+
return nil
|
9
|
+
end
|
10
|
+
|
11
|
+
def black; ansi(30) end
|
12
|
+
def red; ansi(31) end
|
13
|
+
def green; ansi(32) end
|
14
|
+
def brown; ansi(33) end
|
15
|
+
def blue; ansi(34) end
|
16
|
+
def magenta; ansi(35) end
|
17
|
+
def cyan; ansi(36) end
|
18
|
+
def gray; ansi(37) end
|
19
|
+
def bg_black; ansi(40) end
|
20
|
+
def bg_red; ansi(41) end
|
21
|
+
def bg_green; ansi(42) end
|
22
|
+
def bg_brown; ansi(43) end
|
23
|
+
def bg_blue; ansi(44) end
|
24
|
+
def bg_magenta; ansi(45) end
|
25
|
+
def bg_cyan; ansi(46) end
|
26
|
+
def bg_gray; ansi(47) end
|
27
|
+
def bold; "\033[1m#{self}\033[22m" end
|
28
|
+
def reverse_color; "\033[7m#{self}\033[27m" end
|
29
|
+
|
30
|
+
def ansi(code)
|
31
|
+
"\e[#{code.to_s}m#{self}\e[0m"
|
32
|
+
end
|
33
|
+
|
34
|
+
## Returns the hex-encoded SHA1 hash value of the current string. Optionally, pass
|
35
|
+
## a value to limit the length of the returned SHA.
|
36
|
+
def to_sha1(length = 40)
|
37
|
+
Digest::SHA1.hexdigest(self)[0,length]
|
38
|
+
end
|
39
|
+
|
40
|
+
## Returns the hex-encoded MD5 hash value of the current string. Optionally, pass
|
41
|
+
## a value to limit the length of the returned SHA.
|
42
|
+
def to_md5(length = 32)
|
43
|
+
Digest::MD5.hexdigest(self)[0,length]
|
44
|
+
end
|
45
|
+
|
46
|
+
class << self
|
47
|
+
|
48
|
+
def random(options = {})
|
49
|
+
Maini::Utils::RandomString.generate(options)
|
50
|
+
end
|
51
|
+
|
52
|
+
## Returns a psuedo-random UUID which sepearated by hyphens in standard UUID
|
53
|
+
## format. It's worth noting this doesn't actually follow the specification
|
54
|
+
## for Version 4 UUIDs.
|
55
|
+
def generate_token(options = {})
|
56
|
+
values = [rand(0x0010000), rand(0x0010000), rand(0x0010000), rand(0x0010000), rand(0x0010000), rand(0x1000000), rand(0x1000000)]
|
57
|
+
"%04x%04x-%04x-%04x-%04x-%06x%06x" % values
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Maini
|
2
|
+
module Utils
|
3
|
+
class Railtie < Rails::Railtie #:nodoc:
|
4
|
+
|
5
|
+
initializer 'maini.utils.initialize' do |app|
|
6
|
+
|
7
|
+
# Load the Active Record extensions
|
8
|
+
ActiveSupport.on_load(:active_record) do
|
9
|
+
require 'maini/utils/active_record/inquirer'
|
10
|
+
::ActiveRecord::Base.send :include, Maini::Utils::ActiveRecord::Inquirer
|
11
|
+
require 'maini/utils/active_record/random_string'
|
12
|
+
::ActiveRecord::Base.send :include, Maini::Utils::ActiveRecord::RandomString
|
13
|
+
require 'maini/utils/active_record/default_value'
|
14
|
+
::ActiveRecord::Base.send :include, Maini::Utils::ActiveRecord::DefaultValue
|
15
|
+
end
|
16
|
+
|
17
|
+
# load the Action View helpers
|
18
|
+
ActiveSupport.on_load(:action_view) do
|
19
|
+
require 'maini/utils/view_helpers'
|
20
|
+
ActionView::Base.send :include, Maini::Utils::ViewHelpers
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Maini
|
2
|
+
module Utils
|
3
|
+
class RandomString
|
4
|
+
|
5
|
+
LETTERS = ('A'..'Z').to_a + ('a'..'z').to_a
|
6
|
+
NUMBERS = (0..9).to_a
|
7
|
+
WORD_CHARS = LETTERS + NUMBERS
|
8
|
+
SYMBOLS = ['-', '!', '_', '@', '+', '*', '?', '%', '&', '/']
|
9
|
+
|
10
|
+
def self.generate(options = {})
|
11
|
+
options[:length] ||= 30
|
12
|
+
options[:symbols] ||= false
|
13
|
+
chars = WORD_CHARS
|
14
|
+
chars += SYMBOLS if options[:symbols]
|
15
|
+
(
|
16
|
+
[LETTERS[rand(LETTERS.size)]] +
|
17
|
+
(0...options[:length]-2).map{ chars[rand(chars.size)] } +
|
18
|
+
[LETTERS[rand(LETTERS.size)]]
|
19
|
+
).join
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
module Maini
|
2
|
+
module Utils
|
3
|
+
module ViewHelpers
|
4
|
+
|
5
|
+
# Displays the full contents of the `flash` hash within an appropriate <div>
|
6
|
+
# element. The ID of the outputted div will be `flash-alert` where alert is the
|
7
|
+
# type of flash.
|
8
|
+
#
|
9
|
+
# * <tt>:types</tt>: the names of flash messages to display with this helper
|
10
|
+
def display_flash(options = {})
|
11
|
+
options[:types] ||= [:alert, :warning, :notice]
|
12
|
+
options[:types].map do |key|
|
13
|
+
if flash[key]
|
14
|
+
content_tag :div, content_tag(:p, h(flash[key])), :id => "flash-#{key}", :class => "flashMessage flashMessage--#{key}"
|
15
|
+
end
|
16
|
+
end.join.html_safe
|
17
|
+
end
|
18
|
+
|
19
|
+
# Renders an `<img>` containing a link to the gravatar for the given e-mail address.
|
20
|
+
# Available options as follows:
|
21
|
+
#
|
22
|
+
# * <tt>:size</tt>: the size in pixels of the outputted gravatar. Defaults to 35.
|
23
|
+
# * <tt>:default</tt>: the gravatar to fallback to if the user has no gravatar
|
24
|
+
# (see gravatar for available options or pass a URL). Defaults to 'identicon'.
|
25
|
+
# * <tt>:rating</tt>: the maximum rating to use. Defaults to PG.
|
26
|
+
# * <tt>:class</tt>: the value for the class attribute for the outputted img tag.
|
27
|
+
# Defaults to 'gravatar'.
|
28
|
+
# * <tt>:secure</tt>: wherher or not to output an HTTPS version of the gravatar or not.
|
29
|
+
# Defaults to the value of `request.ssl?`.
|
30
|
+
def gravatar(email, options = {})
|
31
|
+
options[:size] ||= 35
|
32
|
+
options[:default] ||= 'identicon'
|
33
|
+
options[:rating] ||= 'PG'
|
34
|
+
options[:class] ||= 'gravatar'
|
35
|
+
options[:secure] ||= request.ssl?
|
36
|
+
host = (options[:secure] ? 'https://secure.gravatar.com' : 'http://gravatar.com')
|
37
|
+
path = "/avatar.php?gravatar_id=#{Digest::MD5.hexdigest(email.to_s.downcase)}&rating=#{options[:rating]}&size=#{options[:size] * 2}&d=#{options[:default]}"
|
38
|
+
image_tag([host,path].join, :class => options[:class], :width => options[:size], :height => options[:size])
|
39
|
+
end
|
40
|
+
|
41
|
+
# Renders a tick or cross character based on the provided boolean. Additional options
|
42
|
+
# can be passed if needed.
|
43
|
+
#
|
44
|
+
# * <tt>:true_text</tt> - text to display next to a tick
|
45
|
+
# * <tt>:false_text</tt> - text to display next to a cross
|
46
|
+
def boolean_tag(bool, tip = nil, options = {})
|
47
|
+
true_text, false_text = "", ""
|
48
|
+
true_text = " <b>#{options[:true_text]}</b>" if options[:true_text]
|
49
|
+
false_text = " <b>#{options[:false_text]}</b>" if options[:false_text]
|
50
|
+
content_tag :span, (bool ? "<span class='true'>✔#{true_text}</span>" : "<span class='false'>✘#{false_text}</span>").html_safe, :class => "boolean", :title => tip
|
51
|
+
end
|
52
|
+
|
53
|
+
# Returns a URL to share some content on Twitter
|
54
|
+
#
|
55
|
+
# * <tt>:text</tt> - the text you wish to tweet
|
56
|
+
# * <tt>:url</tt> - the URL you want to tweet
|
57
|
+
def twitter_share_url(options = {})
|
58
|
+
"https://twitter.com/share?text=#{CGI.escape(options[:text])}&url=#{CGI.escape(options[:url])}"
|
59
|
+
end
|
60
|
+
|
61
|
+
# Returns a length of time in works (no I18N)
|
62
|
+
#
|
63
|
+
def length_of_time_in_words(seconds, options = {})
|
64
|
+
days = (seconds / 60 / 60 / 24).floor
|
65
|
+
hours = ((seconds / 60 / 60) - (days * 24)).floor
|
66
|
+
minutes = ((seconds / 60) - (days * 24 * 60) - (hours * 60)).floor
|
67
|
+
seconds = (seconds - (days * 24 * 60 * 60) - (hours * 60 * 60) - (minutes * 60)).floor
|
68
|
+
Array.new.tap do |s|
|
69
|
+
if options[:short]
|
70
|
+
s << "#{days}d" if days > 0
|
71
|
+
s << "#{hours}h" if hours > 0
|
72
|
+
s << "#{minutes}m" if minutes > 0
|
73
|
+
s << "#{seconds}s" if seconds > 0
|
74
|
+
else
|
75
|
+
s << pluralize(days, 'day') if days > 0
|
76
|
+
s << pluralize(hours, 'hour') if hours > 0
|
77
|
+
s << pluralize(minutes, 'minute') if minutes > 0
|
78
|
+
s << pluralize(seconds, 'second') if seconds > 0
|
79
|
+
end
|
80
|
+
end.join(options[:short] ? ' ' : ', ')
|
81
|
+
end
|
82
|
+
|
83
|
+
# Return an image path for an RFC4226 QR code for a tiven RTP token
|
84
|
+
def rfc4226_qrcode(token)
|
85
|
+
data = "otpauth://totp/#{request.host}?secret=#{token}"
|
86
|
+
data = Rack::Utils.escape(data)
|
87
|
+
url = "https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=#{data}"
|
88
|
+
image_tag(url, :alt => 'Google Authenticator QRCode', :width => 200, :height => 200)
|
89
|
+
end
|
90
|
+
|
91
|
+
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
data/maini-utils.gemspec
ADDED
@@ -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 'maini/utils/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "maini-utils"
|
8
|
+
spec.version = Maini::Utils::VERSION
|
9
|
+
spec.authors = ["Marcos Junior"]
|
10
|
+
spec.email = ["marcos@maini.com.br"]
|
11
|
+
|
12
|
+
spec.summary = "Gem with Maini utils"
|
13
|
+
spec.description = "This gem contains Maini libs utils"
|
14
|
+
spec.homepage = "https://github.com/mainilabs/maini-utils"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_development_dependency "rspec-nc"
|
26
|
+
spec.add_development_dependency "rack-test"
|
27
|
+
|
28
|
+
spec.add_dependency "activesupport"
|
29
|
+
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: maini-utils
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marcos Junior
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
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: rspec-nc
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
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: rack-test
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activesupport
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: This gem contains Maini libs utils
|
98
|
+
email:
|
99
|
+
- marcos@maini.com.br
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".ruby-gemset"
|
107
|
+
- ".ruby-version"
|
108
|
+
- ".travis.yml"
|
109
|
+
- CODE_OF_CONDUCT.md
|
110
|
+
- Gemfile
|
111
|
+
- LICENSE.txt
|
112
|
+
- README.md
|
113
|
+
- Rakefile
|
114
|
+
- bin/console
|
115
|
+
- bin/setup
|
116
|
+
- lib/maini/utils.rb
|
117
|
+
- lib/maini/utils/active_record/default_value.rb
|
118
|
+
- lib/maini/utils/active_record/inquirer.rb
|
119
|
+
- lib/maini/utils/active_record/random_string.rb
|
120
|
+
- lib/maini/utils/error.rb
|
121
|
+
- lib/maini/utils/extensions/array.rb
|
122
|
+
- lib/maini/utils/extensions/string.rb
|
123
|
+
- lib/maini/utils/railtie.rb
|
124
|
+
- lib/maini/utils/random_string.rb
|
125
|
+
- lib/maini/utils/version.rb
|
126
|
+
- lib/maini/utils/view_helpers.rb
|
127
|
+
- maini-utils.gemspec
|
128
|
+
homepage: https://github.com/mainilabs/maini-utils
|
129
|
+
licenses:
|
130
|
+
- MIT
|
131
|
+
metadata: {}
|
132
|
+
post_install_message:
|
133
|
+
rdoc_options: []
|
134
|
+
require_paths:
|
135
|
+
- lib
|
136
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '0'
|
141
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
requirements: []
|
147
|
+
rubyforge_project:
|
148
|
+
rubygems_version: 2.4.8
|
149
|
+
signing_key:
|
150
|
+
specification_version: 4
|
151
|
+
summary: Gem with Maini utils
|
152
|
+
test_files: []
|