motify 0.0.1

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.
@@ -0,0 +1,16 @@
1
+ .repl_history
2
+ build
3
+ tags
4
+ app/pixate_code.rb
5
+ resources/*.nib
6
+ resources/*.momd
7
+ resources/*.storyboardc
8
+ .DS_Store
9
+ nbproject
10
+ .redcar
11
+ #*#
12
+ *~
13
+ *.sw[po]
14
+ .eprj
15
+ .sass-cache
16
+ .idea
@@ -0,0 +1,39 @@
1
+ Motify
2
+
3
+ A minimal DSL for doing notifications in RubyMotion (using NSNotificationCenter)
4
+ Just mix in the module to send or receive notifications
5
+
6
+ ```ruby
7
+ #handle notifications
8
+ class Soldier
9
+ include Motify
10
+
11
+ def initialize
12
+ on :new_mission_started do |location|
13
+ puts "ready to go to #{location}!"
14
+ end
15
+ end
16
+
17
+ def become_disillusioned
18
+ forget :new_mission_started
19
+ end
20
+ end
21
+
22
+ #fire notifications
23
+ class Commander
24
+ include Motify
25
+
26
+ def order_mission
27
+ fire :new_mission_started, 'russia'
28
+ end
29
+ end
30
+ ```
31
+
32
+ ## Installation
33
+ Add it to your Gemfile:
34
+
35
+ `gem 'motify'`
36
+
37
+ Or install it manually:
38
+
39
+ `gem install motify`
@@ -0,0 +1,9 @@
1
+ # -*- coding: utf-8 -*-
2
+ $:.unshift("/Library/RubyMotion/lib")
3
+ require 'motion/project'
4
+ require './lib/motify'
5
+
6
+ Motion::Project::App.setup do |app|
7
+ # Use `rake config' to see complete project settings.
8
+ app.name = 'motify'
9
+ end
@@ -0,0 +1,5 @@
1
+ class AppDelegate
2
+ def application(application, didFinishLaunchingWithOptions:launchOptions)
3
+ true
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ .repl_history
2
+ build
3
+ tags
4
+ app/pixate_code.rb
5
+ resources/*.nib
6
+ resources/*.momd
7
+ resources/*.storyboardc
8
+ .DS_Store
9
+ nbproject
10
+ .redcar
11
+ #*#
12
+ *~
13
+ *.sw[po]
14
+ .eprj
15
+ .sass-cache
16
+ .idea
@@ -0,0 +1,10 @@
1
+ # -*- coding: utf-8 -*-
2
+ $:.unshift("/Library/RubyMotion/lib")
3
+ require 'motion/project'
4
+ $:.unshift("../lib")
5
+ require '../lib/motify'
6
+
7
+ Motion::Project::App.setup do |app|
8
+ # Use `rake config' to see complete project settings.
9
+ app.name = 'example'
10
+ end
@@ -0,0 +1,12 @@
1
+ class AppDelegate
2
+ def application(application, didFinishLaunchingWithOptions:launchOptions)
3
+ soldier = Soldier.new
4
+ commander = Commander.new
5
+ p "Commander starting mission"
6
+ commander.order_mission #should prompt soldier to state readiness
7
+
8
+ soldier.become_disillusioned
9
+ commander.order_mission #soldier will be silent
10
+ true
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ class Commander
2
+ include Motify
3
+
4
+ def order_mission
5
+ fire :new_mission_started, 'russia'
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ class Soldier
2
+ include Motify
3
+
4
+ def initialize
5
+ on(:new_mission_started) {|location| puts "ready to go to #{location}!" }
6
+ end
7
+
8
+ def become_disillusioned
9
+ forget :new_mission_started
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ describe "Application 'example'" do
2
+ before do
3
+ @app = UIApplication.sharedApplication
4
+ end
5
+
6
+ it "has one window" do
7
+ @app.windows.size.should == 1
8
+ end
9
+ end
@@ -0,0 +1,24 @@
1
+ #blatently stole this code from sugarcube
2
+ unless defined?(Motion::Project::Config)
3
+ raise "The motify gem must be required within a RubyMotion project Rakefile."
4
+ end
5
+
6
+
7
+ Motion::Project::App.setup do |app|
8
+ # scans app.files until it finds app/ (the default)
9
+ # if found, it inserts just before those files, otherwise it will insert to
10
+ # the end of the list
11
+ insert_point = 0
12
+ app.files.each_index do |index|
13
+ file = app.files[index]
14
+ if file =~ /^(?:\.\/)?app\//
15
+ # found app/, so stop looking
16
+ break
17
+ end
18
+ insert_point = index + 1
19
+ end
20
+
21
+ Dir.glob(File.join(File.dirname(__FILE__), '/motify/**/*.rb')).reverse.each do |file|
22
+ app.files.insert(insert_point, file)
23
+ end
24
+ end
@@ -0,0 +1,44 @@
1
+ module Motify
2
+ def observers
3
+ @observers ||= {}
4
+ end
5
+
6
+ def on(state,&block)
7
+ observers[state] = add_observer(state,nil) do |notification|
8
+ if block.arity == 0
9
+ block.call
10
+ else
11
+ block.call notification.object
12
+ end
13
+ end
14
+ end
15
+
16
+ def fire(state, obj=nil)
17
+ default_center.postNotificationName(state, object: obj, userInfo: obj)
18
+ end
19
+
20
+ def observes?(state)
21
+ observers.keys.include? state
22
+ end
23
+
24
+ def forget(state)
25
+ default_center.removeObserver(observers.delete(state))
26
+ end
27
+
28
+ def forget_all
29
+ observers.each_key{|state| forget(state)}
30
+ end
31
+
32
+ private
33
+ def add_observer(state, object, &block)
34
+ default_center.addObserverForName(state, object:nil, queue:main_queue, usingBlock:block)
35
+ end
36
+
37
+ def default_center
38
+ NSNotificationCenter.defaultCenter
39
+ end
40
+
41
+ def main_queue
42
+ NSOperationQueue.mainQueue
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ module Motify
2
+
3
+ VERSION = '0.0.1'
4
+
5
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/motify/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "motify"
6
+ s.version = Motify::VERSION
7
+ s.authors = ["Michael Erasmus"]
8
+ s.email = ["hi@michaelerasm.us"]
9
+ s.homepage = "https://github.com/michael-erasmus/motify"
10
+ s.summary = "A minimal DSL for doing notifications in RubyMotion (using NSNotificationCenter)"
11
+ s.description = "A minimal DSL for doing notifications in RubyMotion (using NSNotificationCenter)"
12
+ s.files = `git ls-files`.split($\)
13
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
14
+ s.require_paths = ["lib"]
15
+
16
+ s.add_development_dependency 'rake'
17
+ end
@@ -0,0 +1,56 @@
1
+ describe "motify" do
2
+ before do
3
+ @hn = HasNotifications.new
4
+ @hn.extend Motify
5
+ end
6
+
7
+ describe 'on' do
8
+ it 'observes notifications' do
9
+ @hn.on :bla {|object| observed = true}
10
+ @hn.should.observes :bla
11
+ end
12
+
13
+ it 'doesnt need to care about callback args' do
14
+ @hn.on :yolo { puts :yolo }
15
+ @hn.fire :yolo
16
+ true.should == true #shouldnt raise error
17
+ end
18
+ end
19
+
20
+ it 'fires notifications' do
21
+ fired = false
22
+ @hn.on :foo {|object| fired = true}
23
+ @hn.fire :foo
24
+ fired.should == true
25
+ end
26
+
27
+ it 'fires notifications, passing in data' do
28
+ passed = nil
29
+ @hn.on :baz {|object| passed = object}
30
+ @hn.fire :baz, "baz"
31
+ passed.should == "baz"
32
+ end
33
+
34
+ it 'forgets notifications' do
35
+ fired = false
36
+ @hn.on :bar {|o| fired = true}
37
+ @hn.forget :bar
38
+ @hn.fire :bar
39
+ @hn.should.not.observes :bar
40
+ fired.should == false
41
+ end
42
+
43
+ it 'forgets all notifications' do
44
+ @hn.on :foo {|o| }
45
+ @hn.on :bar {|o| }
46
+
47
+ @hn.forget_all
48
+
49
+ @hn.should.not.observes :foo
50
+ @hn.should.not.observes :bar
51
+ end
52
+ end
53
+
54
+
55
+ class HasNotifications
56
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: motify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Erasmus
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !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: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: A minimal DSL for doing notifications in RubyMotion (using NSNotificationCenter)
31
+ email:
32
+ - hi@michaelerasm.us
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - README.md
39
+ - Rakefile
40
+ - app/app_delegate.rb
41
+ - example/.gitignore
42
+ - example/Rakefile
43
+ - example/app/app_delegate.rb
44
+ - example/app/commander.rb
45
+ - example/app/soldier.rb
46
+ - example/resources/Default-568h@2x.png
47
+ - example/spec/main_spec.rb
48
+ - lib/motify.rb
49
+ - lib/motify/motify.rb
50
+ - lib/motify/version.rb
51
+ - motify.gemspec
52
+ - spec/motify_spec.rb
53
+ homepage: https://github.com/michael-erasmus/motify
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.24
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: A minimal DSL for doing notifications in RubyMotion (using NSNotificationCenter)
77
+ test_files:
78
+ - spec/motify_spec.rb