opal-activesupport 0.0.1 → 0.0.2

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: 90b539607dcedd36bbcb5db0833542391f4a0ed9
4
- data.tar.gz: 7af139aa05432e39150879073bcb0d7b49c66286
3
+ metadata.gz: 80ed9db1f1011da2e3fe0d5a01fae88d10bc8d2c
4
+ data.tar.gz: e27c1a58171a423b0689ec6ab333d48c315d097a
5
5
  SHA512:
6
- metadata.gz: b74b483d8eac451ba6b634d3dd8e13919a95bf9f6daf0c4f0f0a939bef48e060a3a0f2c87dce3f68dcf0a2b223e9ff95955000bc847b0588149745e5277f154b
7
- data.tar.gz: 6e4814521411680f50b0d09de37b7657f0ecc729c680ee003f28f101f7b844a560be8dd234253f3b77fb9b7fd0b2493d4bd9d65b5ec9c8d95e8a3ba3a5dd9915
6
+ metadata.gz: b92124e5979ba8bc8f1a9663277fa6390268fb4071edf247329a22d78734f04aec5b1cb9558ecb4f22553666ab5fe21745a58887ec928fbc5611aacdc39900a5
7
+ data.tar.gz: b05eb740915b565f34d63eb712e9a9acd9303659fbfc152d61c5bd72bcfe875e02256ec68e57d1b940958e2456f983f385726754d43a26d1ceca17a6d7677488
@@ -1,5 +1,5 @@
1
1
  module Opal
2
2
  module Activesupport
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -0,0 +1 @@
1
+ require 'active_support/core_ext/object/blank'
@@ -0,0 +1,100 @@
1
+ # encoding: utf-8
2
+
3
+ class Object
4
+ # An object is blank if it's false, empty, or a whitespace string.
5
+ # For example, '', ' ', +nil+, [], and {} are all blank.
6
+ #
7
+ # This simplifies:
8
+ #
9
+ # if address.nil? || address.empty?
10
+ #
11
+ # ...to:
12
+ #
13
+ # if address.blank?
14
+ def blank?
15
+ respond_to?(:empty?) ? empty? : !self
16
+ end
17
+
18
+ # An object is present if it's not <tt>blank?</tt>.
19
+ def present?
20
+ !blank?
21
+ end
22
+
23
+ # Returns object if it's <tt>present?</tt> otherwise returns +nil+.
24
+ # <tt>object.presence</tt> is equivalent to <tt>object.present? ? object : nil</tt>.
25
+ #
26
+ # This is handy for any representation of objects where blank is the same
27
+ # as not present at all. For example, this simplifies a common check for
28
+ # HTTP POST/query parameters:
29
+ #
30
+ # state = params[:state] if params[:state].present?
31
+ # country = params[:country] if params[:country].present?
32
+ # region = state || country || 'US'
33
+ #
34
+ # ...becomes:
35
+ #
36
+ # region = params[:state].presence || params[:country].presence || 'US'
37
+ def presence
38
+ self if present?
39
+ end
40
+ end
41
+
42
+ class NilClass
43
+ # +nil+ is blank:
44
+ #
45
+ # nil.blank? # => true
46
+ def blank?
47
+ true
48
+ end
49
+ end
50
+
51
+ class Boolean
52
+ # +false+ is blank:
53
+ #
54
+ # false.blank? # => true
55
+ #
56
+ # +true+ is not blank:
57
+ #
58
+ # true.blank? # => false
59
+ def blank?
60
+ self == false
61
+ end
62
+ end
63
+
64
+ class Array
65
+ # An array is blank if it's empty:
66
+ #
67
+ # [].blank? # => true
68
+ # [1,2,3].blank? # => false
69
+ alias_method :blank?, :empty?
70
+ end
71
+
72
+ class Hash
73
+ # A hash is blank if it's empty:
74
+ #
75
+ # {}.blank? # => true
76
+ # { key: 'value' }.blank? # => false
77
+ alias_method :blank?, :empty?
78
+ end
79
+
80
+ class String
81
+ # A string is blank if it's empty or contains whitespaces only:
82
+ #
83
+ # ''.blank? # => true
84
+ # ' '.blank? # => true
85
+ # ' '.blank? # => true
86
+ # ' something here '.blank? # => false
87
+ def blank?
88
+ self !~ /[^\s ]/
89
+ end
90
+ end
91
+
92
+ class Numeric #:nodoc:
93
+ # No number is blank:
94
+ #
95
+ # 1.blank? # => false
96
+ # 0.blank? # => false
97
+ def blank?
98
+ false
99
+ end
100
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+ require 'active_support/core_ext/object/blank'
3
+ require 'empty_bool'
4
+
5
+ describe 'Object#blank?' do
6
+ BLANK = [ EmptyTrue.new, nil, false, '', ' ', " \n\t \r ", ' ', [], {} ]
7
+ NOT = [ EmptyFalse.new, Object.new, true, 0, 1, 'a', [nil], { nil => 0 } ]
8
+
9
+ BLANK.each do |v|
10
+ describe "The value of #{v.inspect}" do
11
+ it 'is #blank?' do
12
+ v.blank?.should == true
13
+ end
14
+
15
+ it 'is not #present?' do
16
+ v.present?.should == false
17
+ end
18
+
19
+ it 'has nil #presence' do
20
+ v.presence.should == nil
21
+ end
22
+ end
23
+ end
24
+
25
+ NOT.each do |v|
26
+ describe "The value of #{v.inspect}" do
27
+ it 'is not #blank?' do
28
+ v.blank?.should == false
29
+ end
30
+
31
+ it 'is not #present?' do
32
+ v.present?.should == true
33
+ end
34
+
35
+ it 'has self #presence' do
36
+ v.presence.should == v
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,7 @@
1
+ class EmptyTrue
2
+ def empty?() true; end
3
+ end
4
+
5
+ class EmptyFalse
6
+ def empty?() false; end
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opal-activesupport
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elia Schito
@@ -56,10 +56,14 @@ files:
56
56
  - opal-activesupport.gemspec
57
57
  - opal/active_support.rb
58
58
  - opal/active_support/core_ext.rb
59
+ - opal/active_support/core_ext/object.rb
60
+ - opal/active_support/core_ext/object/blank.rb
59
61
  - opal/active_support/core_ext/string.rb
60
62
  - opal/opal-activesupport.rb
63
+ - spec/core_ext/object/blank_spec.rb
64
+ - spec/core_ext/string_spec.rb
65
+ - spec/empty_bool.rb
61
66
  - spec/spec_helper.rb
62
- - spec/string_spec.rb
63
67
  homepage: http://opalrb.org
64
68
  licenses: []
65
69
  metadata: {}
@@ -84,5 +88,7 @@ signing_key:
84
88
  specification_version: 4
85
89
  summary: The port of the glorious ActiveSupport for Opal
86
90
  test_files:
91
+ - spec/core_ext/object/blank_spec.rb
92
+ - spec/core_ext/string_spec.rb
93
+ - spec/empty_bool.rb
87
94
  - spec/spec_helper.rb
88
- - spec/string_spec.rb