linkedobject 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 797c58c26cdc3dd092577e02d6dfac9aa665b66a
4
+ data.tar.gz: 29eb9a6235f20056208ce8d48c378974714041a3
5
+ SHA512:
6
+ metadata.gz: 0d014bbbdbe65b235efe27d52929b1d2669144a7d6b87da962e7a3ed19db29ad0f19955d0da6d0ca9222f868f78d05c39dcd8fad93f818a34481a5ca983a4e0d
7
+ data.tar.gz: d750de8030111d2499aa213b7f4b432366b6020ef698bf04b4fbfc3ccefbbdb1fd791d5d77682de4ef84265d8e4a448f5ad01770cb3c539fabe42615b2c17fda
@@ -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 'linkedobject/linkedobject'
11
+ require 'linkedobject/version'
@@ -0,0 +1,69 @@
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
+ class LinkedObject
12
+ attr_accessor :val
13
+
14
+ def initialize(val = nil, listeners = [])
15
+ @val = val
16
+ @listeners = [self] + listeners
17
+ end
18
+
19
+ def new_listener(*args)
20
+ new_listeners(1, *args).first
21
+ end
22
+
23
+ def new_listeners(n, *args)
24
+ [].tap do |listeners|
25
+ n.times do
26
+ listeners << self.class.new(*args).tap { |l| self.is_listened_by(l) }
27
+ end
28
+ end
29
+ end
30
+
31
+ def with_listeners(n, *args)
32
+ [self] + new_listeners(n, *args)
33
+ end
34
+
35
+ def is_listened_by(o)
36
+ @listeners << o
37
+ end; alias_method :add_listener, :is_listened_by
38
+
39
+ def get_val
40
+ @val
41
+ end
42
+
43
+ def set_val(val)
44
+ @val = val
45
+ end; alias_method :set, :set_val
46
+
47
+ %i(== === < <= <=> >= >).each do |m|
48
+ define_method(m) do |s|
49
+ @val.send(s)
50
+ end
51
+ end
52
+
53
+ def apply(method, *args)
54
+ @val = @val.send(method, *args)
55
+ end
56
+
57
+ def method_missing(method, *args, &block)
58
+ if method =~ /with_\d_listeners?/
59
+ n = method.to_s.scan(/with(_\d+_)?listeners?/).first.first[1..-2].to_i
60
+ [self] + new_listeners(n, *args)
61
+ else
62
+ @listeners.each { |l| l.apply(method, *args) }
63
+ end
64
+ end
65
+
66
+ def inspect
67
+ get_val
68
+ end
69
+ 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
+ class LinkedObject
12
+ VERSION = '0.1'
13
+ end
@@ -0,0 +1,25 @@
1
+ #! /usr/bin/env ruby
2
+ require 'linkedobject'
3
+
4
+ describe LinkedObject do
5
+ it 'is fucking holy' do
6
+ a, b = LinkedObject.new.with_1_listener(-1)
7
+
8
+ expect(a).to be_a(LinkedObject)
9
+ expect(b).to be_a(LinkedObject)
10
+
11
+ expect { a.set 1 }.to change { a.val }.from(nil).to(1)
12
+ expect { b.set 2 }.to change { b.val }.from(-1).to(2)
13
+
14
+ expect { a + 1 }.to change { a.val }.from(1).to(2)
15
+
16
+ a - 1 # TODO "...and change..."
17
+ expect { a + 1 }.to change { b.val }.from(2).to(3)
18
+
19
+ expect { a.set 'lo' }.to_not change { b.val }
20
+
21
+ b.is_listened_by(a)
22
+ expect { b * 2 }.to change { b.val }.to(6)
23
+ expect { b * 2 }.to change { a.val }.to('lolololo') # TODO "...and change..."
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: linkedobject
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-09-22 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: A linked object is a holy object with the power to change itself according
42
+ to the world.
43
+ email: webmaster@giovannicapuano.net
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/linkedobject.rb
49
+ - lib/linkedobject/linkedobject.rb
50
+ - lib/linkedobject/version.rb
51
+ - spec/linkedobject_spec.rb
52
+ homepage: http://www.giovannicapuano.net
53
+ licenses:
54
+ - WTFPL
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 2.2.2
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: You and the world will change togheter.
76
+ test_files:
77
+ - spec/linkedobject_spec.rb