motion_support 0.0.4 → 0.0.6

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.
data/README.md CHANGED
@@ -34,12 +34,24 @@ are included at compile-time.
34
34
  require 'motion_support/all'
35
35
  ```
36
36
 
37
- If you wish to only include the `concern`
37
+ If you wish to only include `concern`
38
38
 
39
39
  ```ruby
40
40
  require 'motion_support/concern'
41
41
  ```
42
42
 
43
+ If you wish to only include the `Inflector`
44
+
45
+ ```ruby
46
+ require 'bubble-wrap/inflector'
47
+ ```
48
+
49
+ If you wish to only include `core_extensions`
50
+
51
+ ```ruby
52
+ require 'bubble-wrap/core_ext'
53
+ ```
54
+
43
55
  # Suggestions?
44
56
 
45
57
  Do you have a suggestions? Feel free to open an
data/Rakefile CHANGED
@@ -6,6 +6,7 @@ Bundler.setup
6
6
  Bundler.require
7
7
  require 'bubble-wrap/test'
8
8
 
9
+ require "motion_support"
9
10
  require "motion_support/all"
10
11
 
11
12
  # Bug RubyMotion 1.15
@@ -2,11 +2,12 @@ require 'bubble-wrap/loader'
2
2
 
3
3
  # Base
4
4
  require "motion_support/concern"
5
- require "motion_support/inflections"
6
5
 
7
6
  # Core Exts
8
7
  require "motion_support/core_ext"
9
8
 
10
9
  # Inflector
11
10
  require "motion_support/inflector"
11
+ require "motion_support/inflections"
12
+
12
13
 
@@ -0,0 +1,2 @@
1
+ require 'bubble-wrap/loader'
2
+ BubbleWrap.require('motion/core_ext/object/try.rb')
@@ -1,4 +1,5 @@
1
1
  require 'bubble-wrap/loader'
2
+
2
3
  BubbleWrap.require('motion/**/*.rb') do
3
4
  file('motion/inflections.rb').depends_on('motion/inflector/inflections.rb')
4
5
  end
@@ -1,6 +1,8 @@
1
1
  require 'bubble-wrap/loader'
2
+
2
3
  Dir["#{File.dirname(__FILE__)}/inflector/*.rb"].sort.each do |path|
3
4
  require path
4
5
  end
5
6
 
6
- BubbleWrap.require "motion/inflections"
7
+ #BubbleWrap.require "motion/inflections.rb"
8
+
@@ -1,4 +1,5 @@
1
1
  require 'bubble-wrap/loader'
2
+
2
3
  BubbleWrap.require('motion/**/*.rb') do
3
4
  file('motion/inflector/inflections.rb').depends_on('motion/core_ext/array/prepend_append.rb')
4
5
  end
@@ -1,4 +1,5 @@
1
1
  require 'bubble-wrap/loader'
2
+
2
3
  BubbleWrap.require('motion/inflector/*.rb') do
3
4
  file('motion/inflector/methods.rb').depends_on('motion/inflector/inflections.rb')
4
5
  end
@@ -1,3 +1,3 @@
1
1
  module MotionSupport
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.6"
3
3
  end
@@ -0,0 +1,55 @@
1
+ class Object
2
+ # Invokes the public method identified by the symbol +method+, passing it any arguments
3
+ # and/or the block specified, just like the regular Ruby <tt>Object#public_send</tt> does.
4
+ #
5
+ # *Unlike* that method however, a +NoMethodError+ exception will *not* be raised
6
+ # and +nil+ will be returned instead, if the receiving object is a +nil+ object or NilClass.
7
+ #
8
+ # If try is called without a method to call, it will yield any given block with the object.
9
+ #
10
+ # Please also note that +try+ is defined on +Object+, therefore it won't work with
11
+ # subclasses of +BasicObject+. For example, using try with +SimpleDelegator+ will
12
+ # delegate +try+ to target instead of calling it on delegator itself.
13
+ #
14
+ # Without +try+
15
+ # @person && @person.name
16
+ # or
17
+ # @person ? @person.name : nil
18
+ #
19
+ # With +try+
20
+ # @person.try(:name)
21
+ #
22
+ # +try+ also accepts arguments and/or a block, for the method it is trying
23
+ # Person.try(:find, 1)
24
+ # @people.try(:collect) {|p| p.name}
25
+ #
26
+ # Without a method argument try will yield to the block unless the receiver is nil.
27
+ # @person.try { |p| "#{p.first_name} #{p.last_name}" }
28
+ #
29
+ # +try+ behaves like +Object#public_send+, unless called on +NilClass+.
30
+ def try(*a, &b)
31
+ if a.empty? && block_given?
32
+ yield self
33
+ else
34
+ public_send(*a, &b)
35
+ end
36
+ end
37
+ end
38
+
39
+ class NilClass
40
+ # Calling +try+ on +nil+ always returns +nil+.
41
+ # It becomes specially helpful when navigating through associations that may return +nil+.
42
+ #
43
+ # nil.try(:name) # => nil
44
+ #
45
+ # Without +try+
46
+ # @person && !@person.children.blank? && @person.children.first.name
47
+ #
48
+ # With +try+
49
+ # @person.try(:children).try(:first).try(:name)
50
+ def try(*args)
51
+ nil
52
+ end
53
+ end
54
+
55
+
@@ -0,0 +1,60 @@
1
+ describe "ObjectTry"do
2
+ before do
3
+ @string = "Hello"
4
+ end
5
+
6
+ it "nonexisting method" do
7
+ method = :undefined_method
8
+ @string.respond_to?(method).should.not == true
9
+ lambda{ @string.try(method)}.should.raise(NoMethodError)
10
+ end
11
+
12
+ it "nonexisting method with arguments" do
13
+ method = :undefined_method
14
+ @string.respond_to?(method).should.not == true
15
+ lambda{ @string.try(method, 'llo', 'y') }.should.raise(NoMethodError)
16
+ end
17
+
18
+ it "valid method" do
19
+ @string.try(:size).should == 5
20
+ end
21
+
22
+ it "argument forwarding" do
23
+ @string.try(:sub, 'llo', 'y').should.equal("Hey")
24
+ end
25
+
26
+ it "block forwarding" do
27
+ @string.try(:sub, 'llo') { |match| 'y' }.should.equal("Hey")
28
+ end
29
+
30
+ it "nil to type" do
31
+ nil.try(:to_s).should == nil
32
+ nil.try(:to_i).should == nil
33
+ end
34
+
35
+ it "false try" do
36
+ false.try(:to_s).should == 'false'
37
+ end
38
+
39
+ it "try only block" do
40
+ @string.try { |s| s.reverse }.should == @string.reverse
41
+ end
42
+
43
+ it "try only block nil" do
44
+ ran = false
45
+ nil.try { ran = true }
46
+ ran.should == false
47
+ end
48
+
49
+ it "try with private method" do
50
+ klass = Class.new do
51
+ private
52
+
53
+ def private_method
54
+ 'private method'
55
+ end
56
+ end
57
+
58
+ lambda{ klass.new.try(:private_method) }.should.raise(NoMethodError)
59
+ end
60
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion_support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -67,6 +67,7 @@ files:
67
67
  - lib/motion_support/core_ext/object.rb
68
68
  - lib/motion_support/core_ext/object/deep_dup.rb
69
69
  - lib/motion_support/core_ext/object/duplicable.rb
70
+ - lib/motion_support/core_ext/object/try.rb
70
71
  - lib/motion_support/core_ext/string.rb
71
72
  - lib/motion_support/core_ext/string/inflections.rb
72
73
  - lib/motion_support/inflections.rb
@@ -80,6 +81,7 @@ files:
80
81
  - motion/core_ext/hash/keys.rb
81
82
  - motion/core_ext/object/deep_dup.rb
82
83
  - motion/core_ext/object/duplicable.rb
84
+ - motion/core_ext/object/try.rb
83
85
  - motion/core_ext/string/inflections.rb
84
86
  - motion/inflections.rb
85
87
  - motion/inflector/inflections.rb
@@ -89,6 +91,7 @@ files:
89
91
  - spec/core_ext/array/extract_options_spec.rb
90
92
  - spec/core_ext/array/prepend_append_spec.rb
91
93
  - spec/core_ext/hash/keys_spec.rb
94
+ - spec/core_ext/object/try_spec.rb
92
95
  - spec/helpers/constantize_test_cases.rb
93
96
  - spec/helpers/dup.rb
94
97
  - spec/helpers/inflector_test_cases.rb
@@ -107,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
107
110
  version: '0'
108
111
  segments:
109
112
  - 0
110
- hash: -4103877326357413480
113
+ hash: 2876151020708354689
111
114
  required_rubygems_version: !ruby/object:Gem::Requirement
112
115
  none: false
113
116
  requirements:
@@ -116,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
119
  version: '0'
117
120
  segments:
118
121
  - 0
119
- hash: -4103877326357413480
122
+ hash: 2876151020708354689
120
123
  requirements: []
121
124
  rubyforge_project:
122
125
  rubygems_version: 1.8.23
@@ -128,6 +131,7 @@ test_files:
128
131
  - spec/core_ext/array/extract_options_spec.rb
129
132
  - spec/core_ext/array/prepend_append_spec.rb
130
133
  - spec/core_ext/hash/keys_spec.rb
134
+ - spec/core_ext/object/try_spec.rb
131
135
  - spec/helpers/constantize_test_cases.rb
132
136
  - spec/helpers/dup.rb
133
137
  - spec/helpers/inflector_test_cases.rb