kvo 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +14 -0
- data/kvo.gemspec +25 -0
- data/lib/kvo.rb +25 -0
- data/lib/kvo/version.rb +3 -0
- data/spec/helper.rb +5 -0
- data/spec/kvo_spec.rb +40 -0
- metadata +111 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
desc "Run all rspecs"
|
7
|
+
RSpec::Core::RakeTask.new(:spec) do |t|
|
8
|
+
t.pattern = 'spec/**/*_spec.rb'
|
9
|
+
end
|
10
|
+
task :default => :spec
|
11
|
+
rescue LoadError
|
12
|
+
puts "please install rspec to run tests"
|
13
|
+
puts "install with gem install rspec"
|
14
|
+
end
|
data/kvo.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "kvo/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "kvo"
|
7
|
+
s.version = Kvo::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Shawn Anderson"]
|
10
|
+
s.email = ["shawn42@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{KVO in ruby}
|
13
|
+
s.description = %q{KVO in ruby.}
|
14
|
+
|
15
|
+
s.rubyforge_project = "kvo"
|
16
|
+
|
17
|
+
s.add_dependency 'publisher'
|
18
|
+
s.add_development_dependency 'rspec'
|
19
|
+
s.add_development_dependency 'mocha'
|
20
|
+
|
21
|
+
s.files = `git ls-files`.split("\n")
|
22
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
23
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
24
|
+
s.require_paths = ["lib"]
|
25
|
+
end
|
data/lib/kvo.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'publisher'
|
2
|
+
module Kvo
|
3
|
+
module ClassMethods
|
4
|
+
def kvo_attr_accessor(*kvo_symbols)
|
5
|
+
kvo_symbols.each do |kvo|
|
6
|
+
class_eval <<-METHODS
|
7
|
+
can_fire :#{kvo}_changed
|
8
|
+
def #{kvo}
|
9
|
+
@kvo_#{kvo}
|
10
|
+
end
|
11
|
+
def #{kvo}=(newVal)
|
12
|
+
old = @kvo_#{kvo}
|
13
|
+
@kvo_#{kvo}=newVal
|
14
|
+
fire :#{kvo}_changed, old, newVal
|
15
|
+
end
|
16
|
+
METHODS
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.included(receiver)
|
22
|
+
receiver.extend Publisher
|
23
|
+
receiver.extend ClassMethods
|
24
|
+
end
|
25
|
+
end
|
data/lib/kvo/version.rb
ADDED
data/spec/helper.rb
ADDED
data/spec/kvo_spec.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__),'helper')
|
2
|
+
|
3
|
+
describe 'kvo' do
|
4
|
+
before do
|
5
|
+
@target = Foo.new
|
6
|
+
@target.bar = :old
|
7
|
+
|
8
|
+
@fired = nil
|
9
|
+
@target.when :bar_changed do |old, new|
|
10
|
+
@fired = {:old => old, :new => new}
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'notifies on setting of same value' do
|
15
|
+
old_val = @target.bar
|
16
|
+
@target.bar = old_val
|
17
|
+
@fired.should_not be_nil
|
18
|
+
@fired[:old].should == old_val
|
19
|
+
@fired[:new].should == old_val
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'notifies on setting of different value' do
|
23
|
+
old_val = @target.bar
|
24
|
+
@target.bar = :yar
|
25
|
+
@fired.should_not be_nil
|
26
|
+
@fired[:old].should == old_val
|
27
|
+
@fired[:new].should == :yar
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'is not available for direct setting' do
|
31
|
+
@target.bar = :yar
|
32
|
+
@target.instance_variable_get("@bar").should be_nil
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
class Foo
|
38
|
+
include Kvo
|
39
|
+
kvo_attr_accessor :bar, :baz
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kvo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Shawn Anderson
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-01-13 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: publisher
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :runtime
|
32
|
+
version_requirements: *id001
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rspec
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
version: "0"
|
44
|
+
type: :development
|
45
|
+
version_requirements: *id002
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: mocha
|
48
|
+
prerelease: false
|
49
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
type: :development
|
58
|
+
version_requirements: *id003
|
59
|
+
description: KVO in ruby.
|
60
|
+
email:
|
61
|
+
- shawn42@gmail.com
|
62
|
+
executables: []
|
63
|
+
|
64
|
+
extensions: []
|
65
|
+
|
66
|
+
extra_rdoc_files: []
|
67
|
+
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- Rakefile
|
72
|
+
- kvo.gemspec
|
73
|
+
- lib/kvo.rb
|
74
|
+
- lib/kvo/version.rb
|
75
|
+
- spec/helper.rb
|
76
|
+
- spec/kvo_spec.rb
|
77
|
+
has_rdoc: true
|
78
|
+
homepage: ""
|
79
|
+
licenses: []
|
80
|
+
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
version: "0"
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
version: "0"
|
102
|
+
requirements: []
|
103
|
+
|
104
|
+
rubyforge_project: kvo
|
105
|
+
rubygems_version: 1.3.7
|
106
|
+
signing_key:
|
107
|
+
specification_version: 3
|
108
|
+
summary: KVO in ruby
|
109
|
+
test_files:
|
110
|
+
- spec/helper.rb
|
111
|
+
- spec/kvo_spec.rb
|