tardvig 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 724c04628100a4d2d3fe3c4f246af597a26807c3
4
- data.tar.gz: db371a8e466d8b3de396119bbeca10581b335f1b
3
+ metadata.gz: 6d19399953954acf68f31e5a9fb17befd2599a4b
4
+ data.tar.gz: adeb97c978d563c469aee6490d317adf174569eb
5
5
  SHA512:
6
- metadata.gz: d9139f6e0c19bad867a5310718aecc2139f20e1009b8a75494eb3232eeca23fe9b2248ac9e2e04443c04308d3ba0cbde4f5b294a40cb8d08b607e46759713d46
7
- data.tar.gz: 982a8884b2c6dfe3bf10172d4d2ada79634b3824a0f8ee60adbc61fe720550b1b12c49d9ed0e1f1cf02ed18b63cb472f768bab22ca7299290081c0a46471e817
6
+ metadata.gz: d219400808b22aad2ec4cb4c7d920af26b8b1227880b9b4f2292fa1c08a8df0434a8cdbf6b5e4a693f60987b12e1762389d74e3229443900a867b03b47bee594
7
+ data.tar.gz: 2cc8af78421f3b9877373b72993b10756703f5200c5f2aacb714e7398290d7502040a44f2a794bed415893baeb116420fed02f02911c5a859eca62f668a6307b
@@ -0,0 +1,39 @@
1
+ module Tardvig
2
+ # Mixin for containers which adds them ability to use "data identifier":
3
+ # universal selector of data which contains in a container. It is useful in
4
+ # situation when you need to access data, but you can not directly get
5
+ # them (e. g. in client-server games).
6
+ module DataIdentifier
7
+ # Gets an element from this container using data identifier.
8
+ # @param id [Symbol,String] key of the element.
9
+ # It supports attributes (id `:key` for `object.key`) and the `[]` method
10
+ # (`:key` for `object[:key]` or `object['key']`).
11
+ # You can also get elements from container which is contained in this
12
+ # container (etc) using the `/` symbol (`foo/bar` for `object.foo.bar`).
13
+ def get_this(id)
14
+ obj = self
15
+ id.to_s.split('/').each do |part|
16
+ obj = get_elem_from_id obj, part
17
+ end
18
+ obj
19
+ end
20
+
21
+ private
22
+
23
+ def get_elem_from_id(container, id)
24
+ if id.respond_to?(:to_sym) && container.respond_to?(id.to_sym)
25
+ container.send id.to_sym
26
+ elsif container.respond_to?(:[])
27
+ if num?(id)
28
+ container[id.to_i]
29
+ elsif !container.class.ancestors.include?(Array)
30
+ container[id] || container[id.to_sym]
31
+ end
32
+ end
33
+ end
34
+
35
+ def num?(str)
36
+ str.to_i.to_s == str
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,56 @@
1
+ module Tardvig
2
+ module Events
3
+ # It allows to listens events of object using another object.
4
+ # It is useful when you do not have a direct access to object which you
5
+ # are want to listen on, for example, when you have access only to GameIO
6
+ # from a display.
7
+ module Proxy
8
+ # Adds objects to tracked list so you can bind proxy listeners to them
9
+ # @param [Hash] name => object pairs.
10
+ def spy_on(objects)
11
+ tracked_list.merge! objects
12
+ end
13
+
14
+ # @return [Hash] objects (with their names as keys) which you can bind
15
+ # proxy listeners to.
16
+ def tracked_list
17
+ @tracked_list ||= ({}.extend DataIdentifier)
18
+ end
19
+
20
+ # It works like the original Events#on, but it also can bind listeners
21
+ # to objects from the {tracked_list}.
22
+ #
23
+ # To do it, you need to set event name using next syntax:
24
+ # `tracked_obj_name:event_name`.
25
+ #
26
+ # `tracked_obj_name` is
27
+ # {DataIdentifier} of an object (from {tracked_list}) which you want to
28
+ # bind listener to.
29
+ #
30
+ # `event_name` is the event of this object. The `listener` will be binded
31
+ # to this event. *Important!* When you will trigger this event, its name
32
+ # must be a Symbol.
33
+ #
34
+ # See spec if you did not understood.
35
+ def on(event, &listener)
36
+ event_id_parts = event.to_s.split ':'
37
+ if event_id_parts.size == 2
38
+ event_target = tracked_list.get_this event_id_parts[0]
39
+ if event_target
40
+ bind_proxy_listener event_target, event, event_id_parts[1]
41
+ end
42
+ end
43
+ super event, &listener
44
+ end
45
+
46
+ private
47
+
48
+
49
+ def bind_proxy_listener(target, full_event_name, event_name)
50
+ target.on event_name.to_sym do |data|
51
+ happen full_event_name, data
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,3 +1,3 @@
1
1
  module Tardvig
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.2.1'.freeze
3
3
  end
data/lib/tardvig.rb CHANGED
@@ -7,6 +7,8 @@ require_relative 'tardvig/saved_hash.rb'
7
7
  require_relative 'tardvig/toolkit.rb'
8
8
  require_relative 'tardvig/gameio.rb'
9
9
  require_relative 'tardvig/act.rb'
10
+ require_relative 'tardvig/data_identifier.rb'
11
+ require_relative 'tardvig/events/proxy.rb'
10
12
 
11
13
  # The main namespace for the gem
12
14
  module Tardvig
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tardvig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Saprykin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-01 00:00:00.000000000 Z
11
+ date: 2016-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -80,7 +80,9 @@ files:
80
80
  - lib/tardvig.rb
81
81
  - lib/tardvig/act.rb
82
82
  - lib/tardvig/command.rb
83
+ - lib/tardvig/data_identifier.rb
83
84
  - lib/tardvig/events.rb
85
+ - lib/tardvig/events/proxy.rb
84
86
  - lib/tardvig/gameio.rb
85
87
  - lib/tardvig/saved_hash.rb
86
88
  - lib/tardvig/toolkit.rb