nifty-utils 1.0.5 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 19691448dc81e33142b1040ffc060db38e0d88c3
4
- data.tar.gz: e24d62684fc507a06d9f5b23cb993953b115b3d7
3
+ metadata.gz: 4df7f41b8faa6b1ae2488fb3cbf5ea7001c2ce79
4
+ data.tar.gz: da7b0cb4236bf439d6af24687108ba4d09523174
5
5
  SHA512:
6
- metadata.gz: 8ee820646005e012ebf402eb2ebbcf5c669005135339c1c83d56066bddb40fdc56cc3e3baf3b4cda943d51a3b03f6cb7eae376565eefe729a54a542a858cd449
7
- data.tar.gz: 44a944e51676bdda63e8bc681eeaab061dee98d41dabe0729988d54a88917646a4be3f0a665f10058db908e83bf623c8cecdfc8a737d4dcf61798536396ca08d
6
+ metadata.gz: 5b2c4e9ac34a1f994dda6056ff628be7736fe0bae500ec812d02d0cbd5c764f017981b5f3e466b62788a4433628dd5f626badf259c1a1ea0dd7aa8502019d1b2
7
+ data.tar.gz: 225c89be6607a4c64ffb589b2df72ae052c2121880b559802f662e8bde2e368c235340c1b65fb981bbbb41994f8f81ac79ef30532fc72691813552ac585857e4
@@ -0,0 +1,40 @@
1
+ module Nifty
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,75 @@
1
+ require 'securerandom'
2
+ require 'nifty/utils/random_string'
3
+
4
+ module Nifty
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
+ Nifty::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(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 opts[:unique]
47
+ until self.send(field)
48
+ proposed_string = RandomString.random_string(opts[:type], opts)
49
+ unless self.class.where(field => proposed_string).exists?
50
+ self.send("#{field}=", proposed_string)
51
+ end
52
+ end
53
+ else
54
+ self.send("#{field}=", RandomString.random_string(opts[:type], opts))
55
+ end
56
+ end
57
+ end
58
+
59
+ module ClassMethods
60
+
61
+ def random_string_fields
62
+ @random_string_fields ||= {}
63
+ end
64
+
65
+ def has_random_strings?
66
+ true
67
+ end
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+ end
74
+ end
75
+ end
@@ -1,7 +1,7 @@
1
1
  require 'nifty/utils/random_string'
2
2
 
3
3
  class String
4
-
4
+
5
5
  def self.random(options = {})
6
6
  Nifty::Utils::RandomString.generate(options)
7
7
  end
@@ -9,17 +9,21 @@ class String
9
9
  def ansi(code)
10
10
  "\e[#{code.to_s}m#{self}\e[0m"
11
11
  end
12
-
12
+
13
13
  def red
14
14
  self.ansi(31)
15
15
  end
16
-
16
+
17
17
  def green
18
18
  self.ansi(32)
19
19
  end
20
-
21
- def yello
20
+
21
+ def yellow
22
22
  self.ansi(33)
23
23
  end
24
-
24
+
25
+ def blue
26
+ self.ansi(34)
27
+ end
28
+
25
29
  end
@@ -1,23 +1,25 @@
1
1
  module Nifty
2
2
  module Utils
3
3
  class Railtie < Rails::Railtie #:nodoc:
4
-
4
+
5
5
  initializer 'nifty.utils.initialize' do
6
6
 
7
7
  # Load the Active Record extensions
8
8
  ActiveSupport.on_load(:active_record) do
9
- require 'nifty/utils/active_record'
10
- ::ActiveRecord::Base.send :include, Nifty::Utils::ActiveRecord
9
+ require 'nifty/utils/active_record/inquirer'
10
+ ::ActiveRecord::Base.send :include, Nifty::Utils::ActiveRecord::Inquirer
11
+ require 'nifty/utils/active_record/random_string'
12
+ ::ActiveRecord::Base.send :include, Nifty::Utils::ActiveRecord::RandomString
11
13
  end
12
-
14
+
13
15
  # load the Action View helpers
14
16
  ActiveSupport.on_load(:action_view) do
15
17
  require 'nifty/utils/view_helpers'
16
18
  ActionView::Base.send :include, Nifty::Utils::ViewHelpers
17
19
  end
18
-
20
+
19
21
  end
20
-
22
+
21
23
  end
22
24
  end
23
25
  end
@@ -1,5 +1,5 @@
1
1
  module Nifty
2
2
  module Utils
3
- VERSION = '1.0.5'
3
+ VERSION = '1.0.6'
4
4
  end
5
5
  end
@@ -1,10 +1,10 @@
1
1
  module Nifty
2
2
  module Utils
3
3
  module ViewHelpers
4
-
4
+
5
5
  # Displays the full contents of the `flash` hash within an appropriate <div>
6
6
  # element. The ID of the outputted div will be `flash-alert` where alert is the
7
- # type of flash.
7
+ # type of flash.
8
8
  #
9
9
  # If there are multiple flashes set, they will all be displayed.
10
10
  def display_flash
@@ -12,10 +12,10 @@ module Nifty
12
12
  content_tag :div, content_tag(:p, h(msg)), :id => "flash-#{key}"
13
13
  end.join.html_safe
14
14
  end
15
-
15
+
16
16
  # Renders an `<img>` containing a link to the gravatar for the given e-mail address.
17
17
  # Available options as follows:
18
- #
18
+ #
19
19
  # * <tt>:size</tt>: the size in pixels of the outputted gravatar. Defaults to 35.
20
20
  # * <tt>:default</tt>: the gravatar to fallback to if the user has no gravatar
21
21
  # (see gravatar for available options or pass a URL). Defaults to 'identicon'.
@@ -34,10 +34,10 @@ module Nifty
34
34
  path = "/avatar.php?gravatar_id=#{Digest::MD5.hexdigest(email.to_s.downcase)}&rating=#{options[:rating]}&size=#{options[:size] * 2}&d=#{options[:default]}"
35
35
  image_tag([host,path].join, :class => options[:class], :width => options[:size], :height => options[:size])
36
36
  end
37
-
37
+
38
38
  # Renders a tick or cross character based on the provided boolean. Additional options
39
39
  # can be passed if needed.
40
- #
40
+ #
41
41
  # * <tt>:true_text</tt> - text to display next to a tick
42
42
  # * <tt>:false_text</tt> - text to display next to a cross
43
43
  def boolean_tag(bool, tip = nil, options = {})
@@ -46,7 +46,7 @@ module Nifty
46
46
  false_text = " <b>#{options[:false_text]}</b>" if options[:false_text]
47
47
  content_tag :span, (bool ? "<span class='true'>&#x2714;#{true_text}</span>" : "<span class='false'>&#x2718;#{false_text}</span>").html_safe, :class => "boolean", :title => tip
48
48
  end
49
-
49
+
50
50
  end
51
51
  end
52
52
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nifty-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Cooke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-24 00:00:00.000000000 Z
11
+ date: 2014-11-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A set of useful utilties for Rails applications
14
14
  email:
@@ -18,7 +18,8 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/nifty/utils.rb
21
- - lib/nifty/utils/active_record.rb
21
+ - lib/nifty/utils/active_record/inquirer.rb
22
+ - lib/nifty/utils/active_record/random_string.rb
22
23
  - lib/nifty/utils/extensions/core.rb
23
24
  - lib/nifty/utils/extensions/string.rb
24
25
  - lib/nifty/utils/railtie.rb
@@ -1,38 +0,0 @@
1
- module Nifty
2
- module Utils
3
- module ActiveRecord
4
-
5
- #:nodoc:
6
- def self.included(base)
7
- base.extend ClassMethods
8
- end
9
-
10
- module ClassMethods
11
-
12
- # Allows you to automatically create inquiry methods for string field. For example, if you have
13
- # an Order model which has a status field containing 'approved' or 'delivered' you may wish to
14
- # have a #approved? or #delivered? method on the model.
15
- #
16
- # class Order < ActiveRecord::Baser
17
- # STATUSES = ['approved', 'delivered']
18
- # inquirer :status, *STATUSES
19
- # end
20
- #
21
- # order = Order.new(:status => 'approved')
22
- # order.approved? #=> true
23
- # order.delivered? #=> false
24
- #
25
- #
26
- def inquirer(field, *options)
27
- options.each do |option|
28
- define_method "#{option}?" do
29
- self.read_attribute(field).to_s == option.to_s
30
- end
31
- end
32
- end
33
-
34
- end
35
-
36
- end
37
- end
38
- end