motion_support 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,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rake_tasks~
19
+ pkg/*
20
+ build/
21
+ .DS_Store
22
+ .repl_history
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'bubble-wrap', github: 'rubymotion/BubbleWrap'
4
+
5
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 hookercookerman
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Active Support for RubyMotion aka MotionSupport
2
+
3
+ There is alot of active support stuff that you just dont need in the
4
+ motion rubyverse however there are extension that are more then useful.
5
+
6
+ Remember there is no require in motion land;
7
+
8
+ There is a dependency on bubblewrap at present as this is using their loader.
9
+
10
+ Code credit will goes to whomever did the original active support code;
11
+ this is just me moving it so it plays nice in RubyMotion;
12
+
13
+ ## Installation
14
+
15
+ ```ruby
16
+ gem install motion_support
17
+ ```
18
+
19
+ ## Setup
20
+
21
+ In your Rakefile
22
+
23
+ ```ruby
24
+ require 'motion_support'
25
+ ```
26
+
27
+ The idea is that you can choose from a multiple modules so that you can easily choose which parts
28
+ are included at compile-time.
29
+
30
+ ```ruby
31
+ require 'motion_support/all'
32
+ ```
33
+
34
+ If you wish to only include the `concern`
35
+
36
+ ```ruby
37
+ require 'motion_support/concern'
38
+ ```
39
+
40
+ # Suggestions?
41
+
42
+ Do you have a suggestions? Feel free to open an
43
+ issue/ticket and tell us about what you are after. If you have a
44
+ helper you are using and are thinking that others might enjoy,
45
+ please send a pull request (with tests if possible).
46
+
47
+
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ $:.unshift("/Library/RubyMotion/lib")
3
+ require 'motion/project'
4
+ require "bundler/gem_tasks"
5
+ Bundler.setup
6
+ Bundler.require
7
+ require 'bubble-wrap/test'
@@ -0,0 +1,6 @@
1
+ require 'bubble-wrap/loader'
2
+
3
+ require File.expand_path('../concern', __FILE__)
4
+
5
+ # Core Ext Array
6
+ require File.expand_path('../core_ext/array/extract_options', __FILE__)
@@ -0,0 +1,2 @@
1
+ require 'bubble-wrap/loader'
2
+ BubbleWrap.require('motion/concern.rb')
@@ -0,0 +1,2 @@
1
+ require 'bubble-wrap/loader'
2
+ BubbleWrap.require('motion/core_ext/array/extract_options.rb')
@@ -0,0 +1,3 @@
1
+ module MotionSupport
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "This file must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+ require "motion_support/version"
6
+
7
+ require File.expand_path('../motion_support/all', __FILE__)
data/motion/concern.rb ADDED
@@ -0,0 +1,125 @@
1
+ module MotionSupport
2
+ # A typical module looks like this:
3
+ #
4
+ # module M
5
+ # def self.included(base)
6
+ # base.extend ClassMethods
7
+ # scope :disabled, where(:disabled => true)
8
+ # end
9
+ #
10
+ # module ClassMethods
11
+ # ...
12
+ # end
13
+ # end
14
+ #
15
+ # By using <tt>MotionSupport::Concern</tt> the above module could instead be written as:
16
+ #
17
+ # require 'active_support/concern'
18
+ #
19
+ # module M
20
+ # extend MotionSupport::Concern
21
+ #
22
+ # included do
23
+ # scope :disabled, where(:disabled => true)
24
+ # end
25
+ #
26
+ # module ClassMethods
27
+ # ...
28
+ # end
29
+ # end
30
+ #
31
+ # Moreover, it gracefully handles module dependencies. Given a +Foo+ module and a +Bar+
32
+ # module which depends on the former, we would typically write the following:
33
+ #
34
+ # module Foo
35
+ # def self.included(base)
36
+ # base.class_eval do
37
+ # def self.method_injected_by_foo
38
+ # ...
39
+ # end
40
+ # end
41
+ # end
42
+ # end
43
+ #
44
+ # module Bar
45
+ # def self.included(base)
46
+ # base.method_injected_by_foo
47
+ # end
48
+ # end
49
+ #
50
+ # class Host
51
+ # include Foo # We need to include this dependency for Bar
52
+ # include Bar # Bar is the module that Host really needs
53
+ # end
54
+ #
55
+ # But why should +Host+ care about +Bar+'s dependencies, namely +Foo+? We could try to hide
56
+ # these from +Host+ directly including +Foo+ in +Bar+:
57
+ #
58
+ # module Bar
59
+ # include Foo
60
+ # def self.included(base)
61
+ # base.method_injected_by_foo
62
+ # end
63
+ # end
64
+ #
65
+ # class Host
66
+ # include Bar
67
+ # end
68
+ #
69
+ # Unfortunately this won't work, since when +Foo+ is included, its <tt>base</tt> is the +Bar+ module,
70
+ # not the +Host+ class. With <tt>MotionSupport::Concern</tt>, module dependencies are properly resolved:
71
+ #
72
+ # require 'active_support/concern'
73
+ #
74
+ # module Foo
75
+ # extend MotionSupport::Concern
76
+ # included do
77
+ # class_eval do
78
+ # def self.method_injected_by_foo
79
+ # ...
80
+ # end
81
+ # end
82
+ # end
83
+ # end
84
+ #
85
+ # module Bar
86
+ # extend MotionSupport::Concern
87
+ # include Foo
88
+ #
89
+ # included do
90
+ # self.method_injected_by_foo
91
+ # end
92
+ # end
93
+ #
94
+ # class Host
95
+ # include Bar # works, Bar takes care now of its dependencies
96
+ # end
97
+ module Concern
98
+ def self.extended(base) #:nodoc:
99
+ base.instance_variable_set("@_dependencies", [])
100
+ end
101
+
102
+ def append_features(base)
103
+ if base.instance_variable_defined?("@_dependencies")
104
+ base.instance_variable_get("@_dependencies") << self
105
+ return false
106
+ else
107
+ return false if base < self
108
+ @_dependencies.each { |dep| base.send(:include, dep) }
109
+ super
110
+ base.extend const_get("ClassMethods") if const_defined?("ClassMethods")
111
+ base.class_eval(&@_included_block) if instance_variable_defined?("@_included_block")
112
+ end
113
+ end
114
+
115
+ def included(base = nil, &block)
116
+ if base.nil?
117
+ @_included_block = block
118
+ else
119
+ super
120
+ end
121
+ end
122
+ end
123
+ end
124
+
125
+
@@ -0,0 +1,30 @@
1
+ class Hash
2
+ # By default, only instances of Hash itself are extractable.
3
+ # Subclasses of Hash may implement this method and return
4
+ # true to declare themselves as extractable. If a Hash
5
+ # is extractable, Array#extract_options! pops it from
6
+ # the Array when it is the last element of the Array.
7
+ def extractable_options?
8
+ instance_of?(Hash)
9
+ end
10
+ end
11
+
12
+ class Array
13
+ # Extracts options from a set of arguments. Removes and returns the last
14
+ # element in the array if it's a hash, otherwise returns a blank hash.
15
+ #
16
+ # def options(*args)
17
+ # args.extract_options!
18
+ # end
19
+ #
20
+ # options(1, 2) # => {}
21
+ # options(1, 2, :a => :b) # => {:a=>:b}
22
+ def extract_options!
23
+ if last.is_a?(Hash) && last.extractable_options?
24
+ pop
25
+ else
26
+ {}
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/motion_support/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["hookercookerman"]
6
+ gem.email = ["hookercookerman@gmail.com"]
7
+ gem.description = %q{A toolkit of support libraries and RubyMotion Core extensions}
8
+ gem.summary = %q{A toolkit of support libraries and RubyMotion Core extensions}
9
+ gem.homepage = "http://www.thehitchhikerprinciple"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "motion_support"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = MotionSupport::VERSION
17
+
18
+ gem.add_dependency 'bubble-wrap'
19
+ end
@@ -0,0 +1,93 @@
1
+ class ConcernTest
2
+ module Baz
3
+ extend MotionSupport::Concern
4
+
5
+ module ClassMethods
6
+ def baz
7
+ "baz"
8
+ end
9
+
10
+ def included_ran=(value)
11
+ @@included_ran = value
12
+ end
13
+
14
+ def included_ran
15
+ @@included_ran
16
+ end
17
+ end
18
+
19
+ included do
20
+ self.included_ran = true
21
+ end
22
+
23
+ def baz
24
+ "baz"
25
+ end
26
+ end
27
+
28
+ module Bar
29
+ extend MotionSupport::Concern
30
+
31
+ include Baz
32
+
33
+ def bar
34
+ "bar"
35
+ end
36
+
37
+ def baz
38
+ "bar+" + super
39
+ end
40
+ end
41
+
42
+ module Foo
43
+ extend MotionSupport::Concern
44
+
45
+ include Bar, Baz
46
+ end
47
+ end
48
+
49
+ describe "Concern" do
50
+ before do
51
+ @klass = Class.new
52
+ end
53
+
54
+ it "should include modules normally" do
55
+ @klass.send(:include, ConcernTest::Baz)
56
+ "baz".should.equal @klass.new.baz
57
+ @klass.included_modules.should.include?(ConcernTest::Baz)
58
+ @klass.included_modules.should.include?(ConcernTest::Baz)
59
+
60
+ @klass.send(:include, ConcernTest::Baz)
61
+ "baz".should.equal(@klass.new.baz)
62
+ @klass.included_modules.should.include?(ConcernTest::Baz)
63
+ end
64
+
65
+ it "should extend class methods" do
66
+ @klass.send(:include, ConcernTest::Baz)
67
+ ConcernTest::Baz::ClassMethods.should.equal((class << @klass; self.included_modules; end)[0])
68
+ end
69
+
70
+ it "should include instance methods" do
71
+ @klass.send(:include, ConcernTest::Baz)
72
+ "baz".should.equal @klass.new.baz
73
+ @klass.included_modules.should.include?(ConcernTest::Baz)
74
+ end
75
+
76
+ it "should run the include block" do
77
+ @klass.send(:include, ConcernTest::Baz)
78
+ @klass.included_ran.should.equal(true)
79
+ end
80
+
81
+ it "should meet module dependencies" do
82
+ @klass.send(:include, ConcernTest::Bar)
83
+ "bar".should.equal @klass.new.bar
84
+ "bar+baz".should.equal @klass.new.baz
85
+ "baz".should.equal @klass.baz
86
+ @klass.included_modules.should.include?(ConcernTest::Bar)
87
+ end
88
+
89
+ it "should be cool with multiply modules" do
90
+ @klass.send(:include, ConcernTest::Foo)
91
+ [ConcernTest::Foo, ConcernTest::Bar, ConcernTest::Baz].should.equal(@klass.included_modules[0..2])
92
+ end
93
+ end
@@ -0,0 +1,46 @@
1
+ describe "Array" do
2
+ describe "Extract Options" do
3
+ class HashSubclass < Hash
4
+ end
5
+
6
+ class ExtractableHashSubclass < Hash
7
+ def extractable_options?
8
+ true
9
+ end
10
+ end
11
+
12
+ it "should extract those options" do
13
+ {}.should.equal([].extract_options!)
14
+ {}.should.equal([1].extract_options!)
15
+ {:a=>:b}.should.equal([{:a=>:b}].extract_options!)
16
+ {:a=>:b}.should.equal([1, {:a=>:b}].extract_options!)
17
+ end
18
+
19
+ it "should extract hash subclasses" do
20
+ hash = HashSubclass.new
21
+ hash[:foo] = 1
22
+ array = [hash]
23
+ options = array.extract_options!
24
+ options.should.equal({})
25
+ [hash].should.equal(array)
26
+ end
27
+
28
+ it "should extract options extracts extractable subclasses" do
29
+ hash = ExtractableHashSubclass.new
30
+ hash[:foo] = 1
31
+ array = [hash]
32
+ options = array.extract_options!
33
+ {:foo => 1}.should.equal(options)
34
+ [].should.equal(array)
35
+ end
36
+
37
+ # @todo awaiting with indifferent access
38
+ #it "should extracts hwia" do
39
+ #hash = [{:foo => 1}.with_indifferent_access]
40
+ #options = hash.extract_options!
41
+ #options[:foo].should.equal(1)
42
+ #end
43
+
44
+ end
45
+ end
46
+
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: motion_support
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - hookercookerman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bubble-wrap
16
+ requirement: &70250974339360 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70250974339360
25
+ description: A toolkit of support libraries and RubyMotion Core extensions
26
+ email:
27
+ - hookercookerman@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - lib/motion_support.rb
38
+ - lib/motion_support/all.rb
39
+ - lib/motion_support/concern.rb
40
+ - lib/motion_support/core_ext/array/extract_options.rb
41
+ - lib/motion_support/version.rb
42
+ - motion/concern.rb
43
+ - motion/core_ext/array/extract_options.rb
44
+ - motion_support.gemspec
45
+ - spec/concern_spec.rb
46
+ - spec/core_ext/array/extract_options_spec.rb
47
+ homepage: http://www.thehitchhikerprinciple
48
+ licenses: []
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ segments:
60
+ - 0
61
+ hash: 2236000310539088031
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ segments:
69
+ - 0
70
+ hash: 2236000310539088031
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.11
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: A toolkit of support libraries and RubyMotion Core extensions
77
+ test_files:
78
+ - spec/concern_spec.rb
79
+ - spec/core_ext/array/extract_options_spec.rb