set_sync 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/set_sync.rb +109 -0
  2. metadata +45 -0
data/lib/set_sync.rb ADDED
@@ -0,0 +1,109 @@
1
+ require 'set'
2
+ class SetSync
3
+ attr_accessor :local_binding, :remote_binding
4
+
5
+ def initialize(local, remote, options={}, &blk)
6
+ @local = local
7
+ @remote = remote
8
+
9
+ @local_binding = options[:local_binding] || :id
10
+ @remote_binding = options[:remote_binding] || :id
11
+
12
+ sync_block(blk) if blk
13
+ end
14
+
15
+ def local_hash
16
+ @local_hash ||= begin
17
+ h = {}
18
+ @local.each do |l|
19
+ if l.respond_to? local_binding #is an objecty thing
20
+ h[l.send(local_binding)] = l
21
+ elsif l.respond_to? :[] #is a hashy thing
22
+ h[ l[local_binding] ] = l
23
+ end
24
+ end
25
+ h
26
+ end
27
+ end
28
+
29
+ def remote_hash
30
+ @remote_hash ||= begin
31
+ h = {}
32
+ @remote.each do |r|
33
+ if r.respond_to? remote_binding
34
+ h[ r.send(remote_binding) ] = r
35
+ elsif r.respond_to? :[]
36
+ h[ r[remote_binding] ] = r
37
+ end
38
+ end
39
+ h
40
+ end
41
+ end
42
+
43
+ def sync_block(blk)
44
+ blk.call(self)
45
+ do_sync
46
+ end
47
+
48
+ def do_sync
49
+ do_entering
50
+ do_exiting
51
+ do_updating
52
+ end
53
+
54
+ def do_entering
55
+ entering.each do |binding|
56
+ @on_enter.call remote_hash[binding]
57
+ end
58
+ end
59
+
60
+ def do_exiting
61
+ exiting.each do |binding|
62
+ @on_exit.call local_hash[binding]
63
+ end
64
+ end
65
+
66
+ def do_updating
67
+ updating.each do |binding|
68
+ @on_update.call local_hash[binding], remote_hash[binding]
69
+ end
70
+ end
71
+
72
+ def on_enter(&blk)
73
+ @on_enter = blk
74
+ end
75
+ def on_exit(&blk)
76
+ @on_exit = blk
77
+ end
78
+ def on_update(&blk)
79
+ @on_update = blk
80
+ end
81
+
82
+ def select_remotes(ids)
83
+ remote.select { |r| ids.include? r.send(remote_binding) }
84
+ end
85
+ def select_locals(ids)
86
+ local.select { |r| ids.include? r.send(local_binding) }
87
+ end
88
+
89
+ def remote_bindings
90
+ Set.new remote_hash.keys
91
+ end
92
+
93
+ def local_bindings
94
+ Set.new local_hash.keys
95
+ end
96
+
97
+ def entering
98
+ remote_bindings - local_bindings
99
+ end
100
+
101
+ def updating
102
+ remote_bindings & local_bindings
103
+ end
104
+
105
+ def exiting
106
+ local_bindings - remote_bindings
107
+ end
108
+
109
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: set_sync
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Philip Roberts
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-31 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Syncs sets
15
+ email: phil@latentflip.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/set_sync.rb
21
+ homepage: http://github.com/latentflip/set_sync
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.21
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Syncs sets between things
45
+ test_files: []