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,9 @@
1
+ .repl_history
2
+ build
3
+ resources/*.nib
4
+ resources/*.momd
5
+ resources/*.storyboardc
6
+ *.gem
7
+ .bundle
8
+ Gemfile.lock
9
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "bubble-wrap"
4
+
5
+ # Specify your gem's dependencies in motion-support.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # MotionSupport
2
+
3
+ This is a very small library that sits loosely on top of BubbleWrap to extend the core RubyMotion classes, i.e. no user interface classes. It is somewhat inspired by ActiveSupport, but right now, not a lot is implemented.
4
+
5
+ To see what's there, look into the lib folder. There is documentation, also most everything is tested.
6
+
7
+ # Forking
8
+
9
+ Feel free to fork and submit pull requests!
data/Rakefile ADDED
@@ -0,0 +1,14 @@
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
+
8
+ $:.unshift("./lib/")
9
+ require './lib/motion-support'
10
+
11
+ Motion::Project::App.setup do |app|
12
+ # Use `rake config' to see complete project settings.
13
+ app.name = 'MotionSupport'
14
+ end
@@ -0,0 +1,7 @@
1
+ class AppDelegate
2
+ def application(application, didFinishLaunchingWithOptions:launchOptions)
3
+ @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
4
+ @window.rootViewController = UIViewController.alloc.init
5
+ true
6
+ end
7
+ end
@@ -0,0 +1,24 @@
1
+ class Object
2
+ # True for objects that are considered blank or empty, such as nil, empty string, empty array
3
+ # or the empty hash. When the object responds to empty?, then blank? delegates to empty?,
4
+ # otherwise it returns false, except for nil.
5
+ def blank?
6
+ if respond_to?(:empty?)
7
+ empty?
8
+ else
9
+ false
10
+ end
11
+ end
12
+
13
+ # Opposite of blank?
14
+ def present?
15
+ !blank?
16
+ end
17
+ end
18
+
19
+ class NilClass
20
+ # nil is always blank. See Object#blank?
21
+ def blank?
22
+ true
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ class Class
2
+ # Defines class-level attribute readers as specified in fields. Fields can be one or more
3
+ # symbols specifying the names of the readers.
4
+ def cattr_reader(*fields)
5
+ metaclass.send :attr_reader, *fields
6
+ end
7
+
8
+ # Defines class-level attribute writers as specified in fields. Fields can be one or more
9
+ # symbols specifying the names of the writers.
10
+ def cattr_writer(*fields)
11
+ metaclass.send :attr_writer, *fields
12
+ end
13
+
14
+ # Defines class-level attribute readers and writers as specified in fields. Fields can be
15
+ # one or more symbols specifying the names of the accessors.
16
+ def cattr_accessor(*fields)
17
+ metaclass.send :attr_accessor, *fields
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ class Class
2
+ # Defines class-level inheritable accessors as specified in fields. Fields can be one or more
3
+ # symbols specifying the names of the accessors.
4
+ #
5
+ # The inheritance semantics of inheritable accessors are similar to methods. Consider an inheritable
6
+ # accessor :acc defined in class Base and a class Derived, inheriting from class Base.
7
+ #
8
+ # 0. :acc is never set. Then Base.acc and Derived.acc are both nil
9
+ # 1. :acc is set in class Base. Then Base.acc and Derived.acc are equal
10
+ # 2. :acc is set in class Derived. Then Base.acc is nil and Derived.acc is set
11
+ # 3. :acc is set in both Base and Derived, but to different values. Then Base.acc and Derived.acc
12
+ # have their respective set values.
13
+ def class_inheritable_accessor(*fields)
14
+ fields.each do |field|
15
+ metaclass.send :define_method, field do
16
+ ivar = instance_variable_get("@#{field}")
17
+ ivar || (superclass.send(field) if superclass.respond_to?(field))
18
+ end
19
+ end
20
+
21
+ cattr_writer *fields
22
+ end
23
+ end
@@ -0,0 +1,29 @@
1
+ class Class
2
+ # Defines class-level inheritable array accessors as specified in fields. Fields can be one or more
3
+ # symbols specifying the names of the accessors.
4
+ #
5
+ # Consider an inheritable array accessor :acc defined in class Base and a class Derived, inheriting
6
+ # from class Base.
7
+ #
8
+ # 0. :acc is never set. Then Base.acc and Derived.acc are both empty []
9
+ # 1. :acc is set in class Base. Then Base.acc and Derived.acc both contain the same elements
10
+ # 2. :acc is set in class Derived. Then Base.acc is empty [] and Derived.acc is not
11
+ # 3. :acc is set in both Base and Derived, but with different values. Then Base.acc returns the set
12
+ # value(s) and Derived.acc returns an array containing both the values set to Base.acc as well as
13
+ # the values set to Derived.acc, in that order.
14
+ #
15
+ # Inheritable array setters accept both single values or arrays. When setters are used, they overwrite
16
+ # any previously set values for this class within the hierarchy (and therefore influencing any subclasses),
17
+ # but do not modify any base classes.
18
+ def class_inheritable_array(*fields)
19
+ fields.each do |field|
20
+ metaclass.send :define_method, field do
21
+ array = instance_variable_get("@#{field}") || []
22
+ super_array = (superclass.send(field) if superclass.respond_to?(field)) || []
23
+ [super_array, array].flatten
24
+ end
25
+ end
26
+
27
+ cattr_writer *fields
28
+ end
29
+ end
@@ -0,0 +1,8 @@
1
+ class Object
2
+ # Returns an object's metaclass.
3
+ def metaclass
4
+ class << self
5
+ self
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,27 @@
1
+ class String
2
+ # Returns a pluralized version of the string, if it is not already pluralized. This implementation
3
+ # is rather naive, as it only appends an 's' to the string, unless it already ends with 's'.
4
+ def pluralize
5
+ if self[-1] == 's'
6
+ self
7
+ else
8
+ self + "s"
9
+ end
10
+ end
11
+
12
+ # Returns a singularized version of the string, if it is not already pluralized. This implementation
13
+ # is rather naive, as it only removes a trailing 's' from the string, unless it does not end in 's'.
14
+ def singularize
15
+ if self[-1] == 's'
16
+ self[0..-2]
17
+ else
18
+ self
19
+ end
20
+ end
21
+
22
+ # This method makes a string look like a class name. This means an under_scored string will become
23
+ # a CamelizedString, also it will be singularized, if it is plural.
24
+ def classify
25
+ singularize.camelize
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module MotionSupport
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "motion-support/version"
2
+ require 'bubble-wrap'
3
+ Dir.glob(File.join(File.dirname(__FILE__), 'motion-support/*.rb')).each do |file|
4
+ BW.require file
5
+ end
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/motion-support/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "motion-support"
6
+ s.version = MotionSupport::VERSION
7
+ s.authors = ["Thomas Kadauke"]
8
+ s.email = ["thomas.kadauke@googlemail.com"]
9
+ s.homepage = "https://github.com/tkadauke/motion-support"
10
+ s.summary = "Commonly useful extensions to the standard library for RubyMotion"
11
+ s.description = "Commonly useful extensions to the standard library for RubyMotion. Inspired by ActiveSupport."
12
+
13
+ s.files = `git ls-files`.split($\)
14
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
15
+ s.require_paths = ["lib"]
16
+
17
+ s.add_development_dependency 'rake'
18
+ end
@@ -0,0 +1,41 @@
1
+ describe "blank" do
2
+ describe "Object" do
3
+ it "should be blank when responds to empty and is empty" do
4
+ [].respond_to?(:empty?).should == true
5
+ [].blank?.should == true
6
+ end
7
+
8
+ it "should not be blank when responds to empty and is not empty" do
9
+ "Teststring".respond_to?(:empty?).should == true
10
+ "Teststring".blank?.should == false
11
+ end
12
+
13
+ it "should be present if not blank" do
14
+ "Hello".present?.should == true
15
+ end
16
+
17
+ it "should not be present if blank" do
18
+ [].present?.should == false
19
+ end
20
+ end
21
+
22
+ describe "common objects" do
23
+ it "should be blank for empty array" do
24
+ [].blank?.should == true
25
+ end
26
+
27
+ it "should be blank for empty string" do
28
+ "".blank?.should == true
29
+ end
30
+
31
+ it "should be blank for empty hash" do
32
+ {}.blank?.should == true
33
+ end
34
+ end
35
+
36
+ describe "Nil" do
37
+ it "should always be blank" do
38
+ nil.blank?.should == true
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,49 @@
1
+ class CAttrAccessorBase
2
+ cattr_accessor :empty_accessor
3
+ cattr_accessor :base_accessor, :derived_accessor, :both_accessor
4
+ end
5
+
6
+ class CAttrAccessorDerived < CAttrAccessorBase
7
+ end
8
+
9
+ describe "cattr accessor" do
10
+ it "should be accessible in the base class and the derived class" do
11
+ CAttrAccessorBase.respond_to?(:empty_accessor).should == true
12
+ CAttrAccessorDerived.respond_to?(:empty_accessor).should == true
13
+ end
14
+
15
+ it "should return nil for an unset accessor in the base class" do
16
+ CAttrAccessorBase.empty_accessor.should == nil
17
+ end
18
+
19
+ it "should return nil for an unset accessor in the derived class" do
20
+ CAttrAccessorDerived.empty_accessor.should == nil
21
+ end
22
+
23
+ it "should return a value for an accessor set in the base class in the base class" do
24
+ CAttrAccessorBase.base_accessor = 10
25
+ CAttrAccessorBase.base_accessor.should == 10
26
+ end
27
+
28
+ it "should return nil for an accessor set in the base class in the derived class" do
29
+ CAttrAccessorBase.base_accessor = 10
30
+ CAttrAccessorDerived.base_accessor.should == nil
31
+ end
32
+
33
+ it "should return nil for the base class if set for the derived class" do
34
+ CAttrAccessorDerived.derived_accessor = 20
35
+ CAttrAccessorBase.derived_accessor.should == nil
36
+ end
37
+
38
+ it "should return a value for an accessor set in the derived class in the derived class" do
39
+ CAttrAccessorDerived.derived_accessor = 20
40
+ CAttrAccessorDerived.derived_accessor.should == 20
41
+ end
42
+
43
+ it "should return different values for base and derived class if both were set independently" do
44
+ CAttrAccessorBase.both_accessor = 30
45
+ CAttrAccessorDerived.both_accessor = 40
46
+ CAttrAccessorBase.both_accessor.should == 30
47
+ CAttrAccessorDerived.both_accessor.should == 40
48
+ end
49
+ end
@@ -0,0 +1,49 @@
1
+ class ClassInheritableAccessorBase
2
+ class_inheritable_accessor :empty_accessor
3
+ class_inheritable_accessor :base_accessor, :derived_accessor, :both_accessor
4
+ end
5
+
6
+ class ClassInheritableAccessorDerived < ClassInheritableAccessorBase
7
+ end
8
+
9
+ describe "class inheritable accessor" do
10
+ it "should be accessible in the base class and the derived class" do
11
+ ClassInheritableAccessorBase.respond_to?(:empty_accessor).should == true
12
+ ClassInheritableAccessorDerived.respond_to?(:empty_accessor).should == true
13
+ end
14
+
15
+ it "should return nil for an unset accessor in the base class" do
16
+ ClassInheritableAccessorBase.empty_accessor.should == nil
17
+ end
18
+
19
+ it "should return nil for an unset accessor in the derived class" do
20
+ ClassInheritableAccessorDerived.empty_accessor.should == nil
21
+ end
22
+
23
+ it "should return a value for an accessor set in the base class in the base class" do
24
+ ClassInheritableAccessorBase.base_accessor = 10
25
+ ClassInheritableAccessorBase.base_accessor.should == 10
26
+ end
27
+
28
+ it "should return a value for an accessor set in the base class in the derived class" do
29
+ ClassInheritableAccessorBase.base_accessor = 10
30
+ ClassInheritableAccessorDerived.base_accessor.should == 10
31
+ end
32
+
33
+ it "should return nil for the base class if only set for the derived class" do
34
+ ClassInheritableAccessorDerived.derived_accessor = 20
35
+ ClassInheritableAccessorBase.derived_accessor.should == nil
36
+ end
37
+
38
+ it "should return a value for an accessor set in the derived class in the derived class" do
39
+ ClassInheritableAccessorDerived.derived_accessor = 20
40
+ ClassInheritableAccessorDerived.derived_accessor.should == 20
41
+ end
42
+
43
+ it "should return different values for base and derived class if both were set independently" do
44
+ ClassInheritableAccessorBase.both_accessor = 30
45
+ ClassInheritableAccessorDerived.both_accessor = 40
46
+ ClassInheritableAccessorBase.both_accessor.should == 30
47
+ ClassInheritableAccessorDerived.both_accessor.should == 40
48
+ end
49
+ end
@@ -0,0 +1,61 @@
1
+ class ClassInheritableArrayBase
2
+ class_inheritable_array :empty_array
3
+ class_inheritable_array :value_array, :multi_array, :reset_array
4
+ class_inheritable_array :base_array, :derived_array, :both_array
5
+ end
6
+
7
+ class ClassInheritableArrayDerived < ClassInheritableArrayBase
8
+ end
9
+
10
+ describe "class inheritable array" do
11
+ it "should be accessible in the base class and the derived class" do
12
+ ClassInheritableArrayBase.respond_to?(:empty_array).should == true
13
+ ClassInheritableArrayDerived.respond_to?(:empty_array).should == true
14
+ end
15
+
16
+ it "should return empty array for an unset array in the base class" do
17
+ ClassInheritableArrayBase.empty_array.should == []
18
+ end
19
+
20
+ it "should return empty array for an unset array in the derived class" do
21
+ ClassInheritableArrayDerived.empty_array.should == []
22
+ end
23
+
24
+ it "should allow setting array with one value" do
25
+ ClassInheritableArrayBase.value_array = 10
26
+ ClassInheritableArrayBase.value_array.should == [10]
27
+ end
28
+
29
+ it "should allow setting array with multi values" do
30
+ ClassInheritableArrayBase.multi_array = [10, 20, 30]
31
+ ClassInheritableArrayBase.multi_array.should == [10, 20, 30]
32
+ end
33
+
34
+ it "should allow resetting array" do
35
+ ClassInheritableArrayBase.reset_array = 10
36
+ ClassInheritableArrayBase.reset_array = 20
37
+ ClassInheritableArrayBase.reset_array.should == [20]
38
+ end
39
+
40
+ it "should return a value for an array set in the base class in the derived class" do
41
+ ClassInheritableArrayBase.base_array = 10
42
+ ClassInheritableArrayDerived.base_array.should == [10]
43
+ end
44
+
45
+ it "should return nil for the base class if only set for the derived class" do
46
+ ClassInheritableArrayDerived.derived_array = 20
47
+ ClassInheritableArrayBase.derived_array.should == []
48
+ end
49
+
50
+ it "should return a value for an array set in the derived class in the derived class" do
51
+ ClassInheritableArrayDerived.derived_array = 20
52
+ ClassInheritableArrayDerived.derived_array.should == [20]
53
+ end
54
+
55
+ it "should return values from both the base and derived arrays for derived array if both were set independently" do
56
+ ClassInheritableArrayBase.both_array = 30
57
+ ClassInheritableArrayDerived.both_array = 40
58
+ ClassInheritableArrayBase.both_array.should == [30]
59
+ ClassInheritableArrayDerived.both_array.should == [30, 40]
60
+ end
61
+ end
@@ -0,0 +1,9 @@
1
+ describe "metaclass" do
2
+ it "should return the metaclass for a class" do
3
+ String.metaclass.is_a?(Class).should == true
4
+ end
5
+
6
+ it "should return the metaclass for an object" do
7
+ "string".metaclass.is_a?(Class).should == true
8
+ end
9
+ end
@@ -0,0 +1,43 @@
1
+ describe "string" do
2
+ describe "pluralize" do
3
+ it "should return self if string is already plural" do
4
+ "houses".pluralize.should == "houses"
5
+ "trains".pluralize.should == "trains"
6
+ end
7
+
8
+ it "should return plural version for singular string" do
9
+ "house".pluralize.should == "houses"
10
+ "train".pluralize.should == "trains"
11
+ end
12
+ end
13
+
14
+ describe "singularize" do
15
+ it "should return self if string is already singular" do
16
+ "house".singularize.should == "house"
17
+ "train".singularize.should == "train"
18
+ end
19
+
20
+ it "should return singular version for plural string" do
21
+ "houses".singularize.should == "house"
22
+ "trains".singularize.should == "train"
23
+ end
24
+ end
25
+
26
+ describe "classify" do
27
+ it "should return classified string for underscored singular string" do
28
+ "search_controller".classify.should == "SearchController"
29
+ end
30
+
31
+ it "should return classified string for underscored plural string" do
32
+ "search_controllers".classify.should == "SearchController"
33
+ end
34
+
35
+ it "should return classified string for camelized singular string" do
36
+ "SearchController".classify.should == "SearchController"
37
+ end
38
+
39
+ it "should return classified string for camelized plural string" do
40
+ "SearchControllers".classify.should == "SearchController"
41
+ end
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: motion-support
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Thomas Kadauke
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-11-26 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ description: Commonly useful extensions to the standard library for RubyMotion. Inspired by ActiveSupport.
27
+ email:
28
+ - thomas.kadauke@googlemail.com
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files: []
34
+
35
+ files:
36
+ - .gitignore
37
+ - Gemfile
38
+ - README.md
39
+ - Rakefile
40
+ - app/app_delegate.rb
41
+ - lib/motion-support.rb
42
+ - lib/motion-support/blank.rb
43
+ - lib/motion-support/cattr_accessor.rb
44
+ - lib/motion-support/class_inheritable_accessor.rb
45
+ - lib/motion-support/class_inheritable_array.rb
46
+ - lib/motion-support/metaclass.rb
47
+ - lib/motion-support/string.rb
48
+ - lib/motion-support/version.rb
49
+ - motion-support.gemspec
50
+ - spec/motion-support/blank_spec.rb
51
+ - spec/motion-support/cattr_accessor_spec.rb
52
+ - spec/motion-support/class_inheritable_accessor_spec.rb
53
+ - spec/motion-support/class_inheritable_array_spec.rb
54
+ - spec/motion-support/metaclass_spec.rb
55
+ - spec/motion-support/string_spec.rb
56
+ homepage: https://github.com/tkadauke/motion-support
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options: []
61
+
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ hash: -4408099305659717951
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: -4408099305659717951
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ requirements: []
83
+
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.17
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Commonly useful extensions to the standard library for RubyMotion
89
+ test_files:
90
+ - spec/motion-support/blank_spec.rb
91
+ - spec/motion-support/cattr_accessor_spec.rb
92
+ - spec/motion-support/class_inheritable_accessor_spec.rb
93
+ - spec/motion-support/class_inheritable_array_spec.rb
94
+ - spec/motion-support/metaclass_spec.rb
95
+ - spec/motion-support/string_spec.rb