kvo-rb 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.
Files changed (4) hide show
  1. data/LICENSE +22 -0
  2. data/README.md +55 -0
  3. data/lib/kvo.rb +40 -0
  4. metadata +72 -0
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Neil Cowburn
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.
@@ -0,0 +1,55 @@
1
+ # KVO - Key-Value Observing for Ruby
2
+
3
+ KVO provides a mechanism that allows objects to be notified of changes to attributes of other objects. If you have used Key-Value Observing in a Mac OS X or iOS app, KVO will feel very familiar.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'kvo'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install kvo
18
+
19
+ ## Example
20
+
21
+ class Product
22
+ attr_accessible :name, :price
23
+
24
+ def initialize(name)
25
+ self.name = name
26
+ end
27
+ end
28
+
29
+ class StockController
30
+ attr_accessible :products
31
+
32
+ def initialize
33
+ @products = []
34
+ end
35
+
36
+ def add_product(product)
37
+ product.addObserver(self, :price)
38
+ @products << product
39
+ end
40
+
41
+ def observeValueForKeyPath(keyPath, object)
42
+ puts "New price for #{object.name}: #{object.send(keyPath)}"
43
+ end
44
+ end
45
+
46
+ fruit = Product.new('Banana')
47
+ stockManager = StockController.new
48
+ stockManager.add_product(fruit)
49
+ fruit.price = "0.30"
50
+ => "New price for Banana: 0.30"
51
+
52
+
53
+ ## License
54
+
55
+ [MIT license](http://neil.mit-license.org)
@@ -0,0 +1,40 @@
1
+ module KVO
2
+ def addObserver(target, keyPath)
3
+ @observers ||= {}
4
+ @observers[keyPath.to_sym] ||= []
5
+ @observers[keyPath.to_sym] << target
6
+ end
7
+
8
+ def removeObserver(target, keyPath)
9
+ @observers[keyPath].delete(target)
10
+ @observers.delete(keyPath) if @observers[keyPath.to_sym].empty?
11
+ end
12
+
13
+ def removeAllObserversForKeyPath(keyPath)
14
+ @observers.delete(keyPath.to_sym) if @observers.keys.include?(keyPath.to_sym)
15
+ end
16
+
17
+ class << Object
18
+ alias_method :_kvo_attr_accessor, :attr_accessor
19
+ private :_kvo_attr_accessor
20
+
21
+ def attr_accessor(*attributes)
22
+ _kvo_attr_accessor *attributes
23
+ attributes.each do |attribute|
24
+ alias_method "_kvo_#{attribute}=".to_sym, "#{attribute}=".to_sym
25
+ private "_kvo_#{attribute}=".to_sym
26
+
27
+ define_method "#{attribute}=" do |val|
28
+ self.send("_kvo_#{attribute}=".to_sym, val)
29
+ @observers[attribute].each do |target|
30
+ target.send(:observeValueForKeyPath, attribute, self) if target.respond_to?(:observeValueForKeyPath, true)
31
+ end if !@observers.nil? && @observers.keys.include?(attribute)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ class Object
39
+ include KVO
40
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kvo-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Neil Cowburn
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-12 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: KVO - a Key-Value Observing gem for Ruby models
31
+ email: neilco@gmail.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files:
35
+ - LICENSE
36
+ - README.md
37
+ files:
38
+ - lib/kvo.rb
39
+ - LICENSE
40
+ - README.md
41
+ homepage: http://github.com/neilco/kvo
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --charset=UTF-8
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ segments:
55
+ - 0
56
+ hash: -2645081363472524107
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ segments:
64
+ - 0
65
+ hash: -2645081363472524107
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.24
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: A simple way to monitor classes for changes to attributes
72
+ test_files: []