safe_nested_calls 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ "--color"
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in nested_respond_to.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'guard'
8
+ gem 'guard-rspec'
9
+ end
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
data/README.md ADDED
@@ -0,0 +1,58 @@
1
+ # safe_nested_calls #
2
+
3
+ This gem allows you to safely call nested methods
4
+ on an object, and not worry about whether or not
5
+ the methods are defined. It is very useful for
6
+ managing dynamically created objects.
7
+
8
+ # Installation #
9
+
10
+ gem install 'safe_nested_calls'
11
+
12
+ If you are using Rails, then it will automatically load
13
+ on server start, but if not, somewhere in your code, put:
14
+
15
+ require 'safe_nested_calls'
16
+
17
+ # Usage #
18
+
19
+ safe_nested_calls adds two extra methods to Object,
20
+
21
+ `nested_respond_to?` and `safe_nested_method`.
22
+
23
+ ### nested_respond_to? ###
24
+
25
+ if object.nested_respond_to?(:one, :two, :three)
26
+
27
+ which is equivalent to
28
+
29
+ if object.respond_to? :one
30
+ if object.one.respond_to? :two
31
+ object.one.two.respond_to? :three
32
+
33
+
34
+ ### safe_nested_method ###
35
+
36
+ This method can call the actual nested method desired, with optional parameters
37
+
38
+ object.safe_nested_method(:one, :two, :three) => returns object.one.two.three
39
+
40
+ To use parameters, use a hash of :method => args, where args are the method arguments
41
+
42
+ object.safe_nested_method(:one => 1, :two => [1, 2], :three => [1,2,3])
43
+
44
+ which is equivalent to
45
+
46
+ object.one(1).two(1,2).three(1,2,3)
47
+
48
+
49
+ #### Parameters #####
50
+
51
+ If you need to call of mix of methods with and without parameters, set the arguments for
52
+ the methods without parameters to :none
53
+
54
+ object.safe_nested_method(:get => :none, :increment => 4, :save => :none)
55
+
56
+ which is equivalent to
57
+
58
+ object.get().increment(4).save()
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,56 @@
1
+ require "safe_nested_calls/version"
2
+ require "safe_nested_calls/railtie" if defined? Rails
3
+
4
+ module NestedRespondTo
5
+ def nested_respond_to?(*methods)
6
+ if nil? || methods.empty?
7
+ true
8
+ else
9
+ method = methods.shift
10
+ if self.respond_to?(method)
11
+ self.send(method).nested_respond_to?(*methods)
12
+ else
13
+ false
14
+ end
15
+ end
16
+ end
17
+
18
+ def safe_nested_method(*params)
19
+ p = params
20
+ if params.respond_to?(:size) && params.size == 1 && params[0].is_a?(Hash)
21
+ p = params[0]
22
+ end
23
+
24
+ if p.empty?
25
+ return self
26
+ end
27
+
28
+ if p.is_a? Array
29
+ name = p.shift
30
+ if name.class != Symbol
31
+ raise ArgumentError, 'Parameters must be symbols'
32
+ end
33
+ if self.respond_to?(name)
34
+ self.send(name).safe_nested_method(*p)
35
+ else
36
+ return nil
37
+ end
38
+ elsif p.is_a? Hash
39
+ name, args = p.shift
40
+ if name.class != Symbol
41
+ raise ArgumentError, 'Parameters must be symbols'
42
+ end
43
+ if self.respond_to?(name)
44
+ if args == :none
45
+ self.send(name).safe_nested_method(*[p])
46
+ else
47
+ self.send(name, *args).safe_nested_method(*p)
48
+ end
49
+ else
50
+ return nil
51
+ end
52
+ end
53
+ end
54
+ end
55
+
56
+ Object.send(:include, NestedRespondTo)
@@ -0,0 +1,7 @@
1
+ module SafeNestedCalls
2
+ class Railtie < Rails::Railtie
3
+ initializer "SafeNestedCalls" do |app|
4
+ require 'safe_nested_calls'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module SafeNestedCalls
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "safe_nested_calls/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "safe_nested_calls"
7
+ s.version = SafeNestedCalls::VERSION
8
+ s.authors = ["Rick Button"]
9
+ s.email = ["rickb@extemprep.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Safely call nested methods in Ruby}
12
+ s.description = %q{Allows you to safely call nested methods on an object, returning nil if they are undefined. }
13
+
14
+ s.rubyforge_project = "safe_nested_calls"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+
25
+ s.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+ class A
3
+ def do_magic(var)
4
+ 'magic ' << var
5
+ end
6
+ end
7
+ class B
8
+ attr_accessor :a
9
+ end
10
+ class C
11
+ attr_accessor :b
12
+ end
13
+ class D
14
+ attr_accessor :c
15
+ end
16
+
17
+
18
+ describe 'SafeNestedCalls' do
19
+ d = D.new
20
+ d.c = C.new
21
+ d.c.b = B.new
22
+ d.c.b.a = A.new
23
+
24
+ describe 'nested_respond_to?' do
25
+ it 'should return true when the methods all exist' do
26
+ d.nested_respond_to?(:c, :b, :a).should eq(true)
27
+ end
28
+
29
+ it 'should return false when a method does not exist' do
30
+ d.nested_respond_to?(:c, :b, :LOL_DOESNT_EXIST).should eq(false)
31
+ end
32
+ end
33
+
34
+ describe 'safe_nested_method' do
35
+ it 'should return the nested object when the methods all exist (with no params)' do
36
+ d.safe_nested_method(:c, :b, :a).should eq(d.c.b.a)
37
+ end
38
+
39
+ it 'should return nil when a method does not exist (with no params)' do
40
+ d.safe_nested_method(:c, :b, :LOL_DOESNT_EXIST).should eq(nil)
41
+ end
42
+
43
+ it 'should return the nested object when the methods all exist (with params)' do
44
+ d.safe_nested_method(:c=>:none, :b=>:none, :a => :none, :do_magic => ['tree house']).should eq('magic tree house')
45
+ end
46
+
47
+ it 'should return nil when a method does not exist (with params)' do
48
+ d.safe_nested_method(:c=>:none, :b=>:none, :LOL_DOESNT_EXIST => ['death house']).should eq(nil)
49
+ end
50
+
51
+ it 'should raise an ArguementError when an incorrect parameter is sent' do
52
+ expect { d.safe_nested_method Object.new }.should raise_error(ArgumentError)
53
+ end
54
+ it 'should raise an ArguementError when an incorrect parameter is sent (inside a hash)' do
55
+ expect { d.safe_nested_method Object.new => :NOPE }.should raise_error(ArgumentError)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1 @@
1
+ require 'safe_nested_calls'
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: safe_nested_calls
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rick Button
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &19346436 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *19346436
25
+ description: ! 'Allows you to safely call nested methods on an object, returning nil
26
+ if they are undefined. '
27
+ email:
28
+ - rickb@extemprep.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - .rspec
35
+ - Gemfile
36
+ - Guardfile
37
+ - README.md
38
+ - Rakefile
39
+ - lib/safe_nested_calls.rb
40
+ - lib/safe_nested_calls/railtie.rb
41
+ - lib/safe_nested_calls/version.rb
42
+ - safe_nested_calls.gemspec
43
+ - spec/safe_nested_calls/safe_nested_calls_spec.rb
44
+ - spec/spec_helper.rb
45
+ homepage: ''
46
+ licenses: []
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project: safe_nested_calls
65
+ rubygems_version: 1.8.17
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Safely call nested methods in Ruby
69
+ test_files: []