alakazam 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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1652b38f86938a1de1195361d799813aaed014bb
4
+ data.tar.gz: a7c3aa7a6c456820fa68a613b04cb3b0dc236c8e
5
+ SHA512:
6
+ metadata.gz: 82ec4cd082b655986ee20e685f55c6e41e36b84bf1659084a113798b279dba0c6efc1d21333fa66b2d5ae97a878f735e6572dc1a5dcf938ea1ac44e80338c45c
7
+ data.tar.gz: 17c4f35ea5299dddb41755828349eee3efca11356f08002cf72342be7ebd008893fb40bd287d492b0d84cc82cf1739b16be38a5b2f35490f84c9e650dc9389d1
@@ -0,0 +1,11 @@
1
+ #--
2
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3
+ # Version 2, December 2004
4
+ #
5
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
6
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
7
+ #
8
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
9
+ #++
10
+ require 'alakazam/alakazam'
11
+ require 'alakazam/version'
@@ -0,0 +1,98 @@
1
+ #--
2
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3
+ # Version 2, December 2004
4
+ #
5
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
6
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
7
+ #
8
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
9
+ #++
10
+
11
+ module Alakazam
12
+ def add_observer(observer, attributes: {}, when_change: true, methods: [] )
13
+ attributes = [attributes] unless attributes.is_a? Array
14
+ methods = [methods ] unless methods.is_a? Array
15
+
16
+ attributes.each { |attribute|
17
+ next if not attribute[:var]
18
+
19
+ self.class.send(:define_method, :"#{attribute[:var]}=") do |val|
20
+ fire!
21
+ notify_observers attribute[:notify]
22
+ instance_eval "@#{attribute[:var]} = #{val}"
23
+ end
24
+ }
25
+
26
+ if not has_observer? observer
27
+ @observers ||= {}
28
+ @observers[observer] = { when_change: when_change, methods: methods }
29
+ end
30
+ end
31
+ alias_method :is_observed_by, :add_observer
32
+ alias_method :attach, :add_observer
33
+ alias_method :observe, :add_observer
34
+
35
+
36
+ def delete_observer(observer)
37
+ (@observers || {}).delete observer
38
+ end
39
+ alias_method :remove_observer, :delete_observer
40
+ alias_method :detach, :delete_observer
41
+ alias_method :disconnect, :delete_observer
42
+
43
+ def has_observer?(observer)
44
+ (@observers || {}).include? observer
45
+ end
46
+ alias_method :is_observed_by?, :has_observer?
47
+
48
+ def count_observers
49
+ (@observers || {}).length
50
+ end
51
+ alias_method :how_many_observers?, :count_observers
52
+
53
+ def changed
54
+ @changed = !changed?
55
+ end
56
+ alias_method :change!, :changed
57
+ alias_method :fire!, :changed
58
+
59
+ def changed=(state)
60
+ @changed = state
61
+ end
62
+ alias_method :fired=, :changed=
63
+
64
+ def changed?
65
+ !!@changed
66
+ end
67
+ alias_method :fired?, :changed?
68
+
69
+ def notify_observers(*things)
70
+ (@observers || {}).each { |observer, options|
71
+ if !options[:when_change] || changed?
72
+ if options[:methods].any?
73
+ options[:methods].each { |method|
74
+ if observer.respond_to? method
75
+ observer.send method, things
76
+ elsif observer.singleton_class.respond_to? method
77
+ observer.singleton_class.send method, things
78
+ elsif observer.respond_to? :"#{method}="
79
+ observer.send method, things
80
+ end
81
+ }
82
+ elsif observer.respond_to? :update
83
+ observer.update things
84
+ elsif observer.is_a? Proc
85
+ observer.call things
86
+ else
87
+ options[:methods].each { |method|
88
+ observer.send(method, things) if observer.respond_to? method
89
+ }
90
+ end
91
+ end
92
+ }
93
+
94
+ @changed = false
95
+ end
96
+ alias_method :notify, :notify_observers
97
+ alias_method :fire, :notify_observers
98
+ end
@@ -0,0 +1,13 @@
1
+ #--
2
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
3
+ # Version 2, December 2004
4
+ #
5
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
6
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
7
+ #
8
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
9
+ #++
10
+
11
+ module Alakazam
12
+ VERSION = '0.1'
13
+ end
@@ -0,0 +1,117 @@
1
+ #! /usr/bin/env ruby
2
+ #--
3
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
4
+ # Version 2, December 2004
5
+ #
6
+ # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
7
+ # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
8
+ #
9
+ # 0. You just DO WHAT THE FUCK YOU WANT TO.
10
+ #++
11
+ require 'alakazam'
12
+
13
+ class Shiftry
14
+ include Alakazam
15
+ extend Alakazam # required only to observe class methods
16
+ attr_accessor :lal
17
+
18
+ def lol
19
+ fire!
20
+ notify 'fired'
21
+ end
22
+
23
+ def lel
24
+ notify 'fired'
25
+ end
26
+
27
+ def self.lul
28
+ fire!
29
+ notify 'fired'
30
+ end
31
+ end
32
+
33
+ class Logger
34
+ def update(*things)
35
+ $i += 1
36
+ end
37
+
38
+ def self.on_fire(*things)
39
+ $i += 1
40
+ end
41
+ end
42
+
43
+ describe Alakazam do
44
+ before do
45
+ $i = 0
46
+ end
47
+
48
+ let(:counter) do
49
+ counter = $i
50
+ end
51
+
52
+ let(:logger) do
53
+ logger = ->(*things) { $i += 1 }
54
+ end
55
+
56
+ let(:shiftry) do
57
+ shiftry = Shiftry.new
58
+ end
59
+
60
+ after do
61
+ $i = 0
62
+ end
63
+
64
+ it 'invokes a Proc when notified by observed class' do
65
+ shiftry.is_observed_by logger
66
+
67
+ shiftry.lol
68
+ shiftry.lol
69
+
70
+ shiftry.lel
71
+ shiftry.lel
72
+
73
+ counter.should be 2
74
+ end
75
+
76
+ it 'invokes a Proc without explicit notify when notified by observed class' do
77
+ shiftry.is_observed_by logger, when_change: false
78
+
79
+ shiftry.lol
80
+ shiftry.lol
81
+
82
+ shiftry.lel
83
+ shiftry.lel
84
+
85
+ counter.should be 4
86
+ end
87
+
88
+ it 'invokes a Proc when notified by a class method of the observed class' do
89
+ Shiftry.is_observed_by logger
90
+
91
+ Shiftry.lul
92
+ Shiftry.lul
93
+
94
+ counter.should be 2
95
+ end
96
+
97
+ it 'invokes the logger\'s custom and default method when notified by observed class' do
98
+ shiftry.is_observed_by Logger.new, methods: [ :on_fire, :non_existing_method ]
99
+ shiftry.is_observed_by Logger.new
100
+
101
+ shiftry.lol
102
+
103
+ counter.should be 2
104
+ end
105
+
106
+ it 'invokes the logger\'s default method when a variable changes in the observed class' do
107
+ shiftry.is_observed_by Logger.new, attributes: { var: :lal, notify: 'fired' }
108
+
109
+ shiftry.lal = 3
110
+ shiftry.lal.should be 3
111
+
112
+ shiftry.lal = 4
113
+ shiftry.lal.should be 4
114
+
115
+ counter.should be 2
116
+ end
117
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alakazam
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Giovanni Capuano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Alakazam provides methods to observe all your things.
42
+ email: webmaster@giovannicapuano.net
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/alakazam/alakazam.rb
48
+ - lib/alakazam/version.rb
49
+ - lib/alakazam.rb
50
+ - tests/alakazam_spec.rb
51
+ homepage: https://github.com/RoxasShadow
52
+ licenses:
53
+ - WTFPL
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 2.0.3
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: An observer with everything you need
75
+ test_files:
76
+ - tests/alakazam_spec.rb