Class: SetSync
- Inherits:
-
Object
- Object
- SetSync
- Defined in:
- lib/set_sync.rb
Instance Attribute Summary collapse
-
#local_binding ⇒ Object
Returns the value of attribute local_binding.
-
#remote_binding ⇒ Object
Returns the value of attribute remote_binding.
Class Method Summary collapse
- .local_binding(binding = nil) ⇒ Object
- .on_enter(&blk) ⇒ Object
- .on_exit(&blk) ⇒ Object
- .on_update(&blk) ⇒ Object
- .remote_binding(binding = nil) ⇒ Object
Instance Method Summary collapse
- #do_enter(remote) ⇒ Object
- #do_entering ⇒ Object
- #do_exit(local) ⇒ Object
- #do_exiting ⇒ Object
- #do_sync ⇒ Object
- #do_update(local, remote) ⇒ Object
- #do_updating ⇒ Object
- #entering ⇒ Object
- #exiting ⇒ Object
-
#initialize(local, remote, options = {}, &blk) ⇒ SetSync
constructor
A new instance of SetSync.
- #local_bindings ⇒ Object
- #local_hash ⇒ Object
- #on_enter(&blk) ⇒ Object
- #on_exit(&blk) ⇒ Object
- #on_update(&blk) ⇒ Object
- #remote_bindings ⇒ Object
- #remote_hash ⇒ Object
- #select_locals(ids) ⇒ Object
- #select_remotes(ids) ⇒ Object
- #sync_block(blk) ⇒ Object
- #updating ⇒ Object
Constructor Details
#initialize(local, remote, options = {}, &blk) ⇒ SetSync
Returns a new instance of SetSync.
31 32 33 34 35 36 37 38 39 |
# File 'lib/set_sync.rb', line 31 def initialize(local, remote, ={}, &blk) @local = local @remote = remote @local_binding = [:local_binding] || self.class.local_binding || :id @remote_binding = [:remote_binding] || self.class.remote_binding || :id sync_block(blk) if blk end |
Instance Attribute Details
#local_binding ⇒ Object
Returns the value of attribute local_binding.
30 31 32 |
# File 'lib/set_sync.rb', line 30 def local_binding @local_binding end |
#remote_binding ⇒ Object
Returns the value of attribute remote_binding.
30 31 32 |
# File 'lib/set_sync.rb', line 30 def remote_binding @remote_binding end |
Class Method Details
.local_binding(binding = nil) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/set_sync.rb', line 5 def local_binding(binding=nil) if binding @local_binding = binding else @local_binding end end |
.on_enter(&blk) ⇒ Object
19 20 21 |
# File 'lib/set_sync.rb', line 19 def on_enter(&blk) blk ? @on_enter=blk : @on_enter end |
.on_exit(&blk) ⇒ Object
22 23 24 |
# File 'lib/set_sync.rb', line 22 def on_exit(&blk) blk ? @on_exit=blk : @on_enter end |
.on_update(&blk) ⇒ Object
25 26 27 |
# File 'lib/set_sync.rb', line 25 def on_update(&blk) blk ? @on_update=blk : @on_update end |
.remote_binding(binding = nil) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/set_sync.rb', line 12 def remote_binding(binding=nil) if binding @remote_binding = binding else @remote_binding end end |
Instance Method Details
#do_enter(remote) ⇒ Object
102 103 104 105 |
# File 'lib/set_sync.rb', line 102 def do_enter(remote) blk = @on_enter || self.class.on_enter blk.call remote end |
#do_entering ⇒ Object
84 85 86 87 88 |
# File 'lib/set_sync.rb', line 84 def do_entering entering.each do |binding| do_enter remote_hash[binding] end end |
#do_exit(local) ⇒ Object
106 107 108 109 |
# File 'lib/set_sync.rb', line 106 def do_exit(local) blk = @on_exit || self.class.on_exit blk.call local end |
#do_exiting ⇒ Object
90 91 92 93 94 |
# File 'lib/set_sync.rb', line 90 def do_exiting exiting.each do |binding| do_exit local_hash[binding] end end |
#do_sync ⇒ Object
78 79 80 81 82 |
# File 'lib/set_sync.rb', line 78 def do_sync do_entering do_exiting do_updating end |
#do_update(local, remote) ⇒ Object
110 111 112 113 |
# File 'lib/set_sync.rb', line 110 def do_update(local, remote) blk = @on_update || self.class.on_update blk.call local, remote end |
#do_updating ⇒ Object
96 97 98 99 100 |
# File 'lib/set_sync.rb', line 96 def do_updating updating.each do |binding| do_update local_hash[binding], remote_hash[binding] end end |
#entering ⇒ Object
140 141 142 |
# File 'lib/set_sync.rb', line 140 def entering remote_bindings - local_bindings end |
#exiting ⇒ Object
148 149 150 |
# File 'lib/set_sync.rb', line 148 def exiting local_bindings - remote_bindings end |
#local_bindings ⇒ Object
136 137 138 |
# File 'lib/set_sync.rb', line 136 def local_bindings Set.new local_hash.keys end |
#local_hash ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/set_sync.rb', line 41 def local_hash @local_hash ||= begin h = {} @local.each do |l| if local_binding.respond_to? :call h[ local_binding.call(l) ] = l elsif l.respond_to? local_binding #is an objecty thing h[l.send(local_binding)] = l elsif l.respond_to? :[] #is a hashy thing h[ l[local_binding] ] = l end end h end end |
#on_enter(&blk) ⇒ Object
115 116 117 |
# File 'lib/set_sync.rb', line 115 def on_enter(&blk) @on_enter = blk end |
#on_exit(&blk) ⇒ Object
118 119 120 |
# File 'lib/set_sync.rb', line 118 def on_exit(&blk) @on_exit = blk end |
#on_update(&blk) ⇒ Object
121 122 123 |
# File 'lib/set_sync.rb', line 121 def on_update(&blk) @on_update = blk end |
#remote_bindings ⇒ Object
132 133 134 |
# File 'lib/set_sync.rb', line 132 def remote_bindings Set.new remote_hash.keys end |
#remote_hash ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/set_sync.rb', line 57 def remote_hash @remote_hash ||= begin h = {} @remote.each do |r| if remote_binding.respond_to? :call h[ remote_binding.call(r) ] = r elsif r.respond_to? remote_binding h[ r.send(remote_binding) ] = r elsif r.respond_to? :[] h[ r[remote_binding] ] = r end end h end end |
#select_locals(ids) ⇒ Object
128 129 130 |
# File 'lib/set_sync.rb', line 128 def select_locals(ids) local.select { |r| ids.include? r.send(local_binding) } end |
#select_remotes(ids) ⇒ Object
125 126 127 |
# File 'lib/set_sync.rb', line 125 def select_remotes(ids) remote.select { |r| ids.include? r.send(remote_binding) } end |
#sync_block(blk) ⇒ Object
73 74 75 76 |
# File 'lib/set_sync.rb', line 73 def sync_block(blk) blk.call(self) do_sync end |
#updating ⇒ Object
144 145 146 |
# File 'lib/set_sync.rb', line 144 def updating remote_bindings & local_bindings end |