woyo-world 0.0.6 → 0.0.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d6b8db18d440d61ddaf291c7db149e5fa35f8a98
4
- data.tar.gz: 393a37a546f63d71e2132374c927f0571d475c4b
3
+ metadata.gz: 2c1c941040eef171a088f6ee54a6416f55eb46ba
4
+ data.tar.gz: 881d8203e4a35f0965a191cf47f6706cc18ee8ba
5
5
  SHA512:
6
- metadata.gz: 465dab73ed8d398391a9e7ca63bac830b205a7622e895a5b1fc736ad36d7c2f36179a5713f7bece47aaf53ab6ecdb438937f9c8b026f730f5d3b9eff1e8f2f08
7
- data.tar.gz: 033f7d8ddf1839495ead16bb0d65ae6e4e7df2469bb31ab7e5e7993483a41c7d8e4d1485d43eccd3844f2fd1ce0f6813fe02497ccd8bc9ca4e36bffe836dc076
6
+ metadata.gz: eb02560ec28f3cc4c370406424518b5c9246a69c34225903e5a17abde911be52231a9cc1670ffc37036c1da788492b39d78b141a00aa17d1f1776996b748bf84
7
+ data.tar.gz: 958c5a2eacdf5be0ef4e3e3a0fd0ba420040bc8842efebdf18d03a5e427cfbf5bfa05eac7eac2fb4b12e84bb653157cc11f8aaee8998a4d6c946c714bea429cd
@@ -0,0 +1,24 @@
1
+ require_relative 'attributes'
2
+
3
+ module Woyo
4
+
5
+ module Actions
6
+
7
+ include Attributes
8
+
9
+ def action *acts, &block
10
+ send :_attributes, acts, ivn: '@actions', &block
11
+ end
12
+
13
+ def actions *acts, &block
14
+ send :_attributes, acts, ivn: '@actions', &block
15
+ end
16
+
17
+ def do act
18
+ send act
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+
@@ -8,195 +8,146 @@ module Woyo
8
8
 
9
9
  module Attributes
10
10
 
11
- def self.included(base)
12
- base.extend(ClassMethods)
11
+ def attribute *attrs, &block
12
+ send :_attributes, attrs, ivn: '@attributes', &block
13
13
  end
14
14
 
15
- def self.prepended(base)
16
- base.singleton_class.prepend(ClassMethods) # base.extend(ClassMethods) also seems to work, what's the diff ?
15
+ def attributes *attrs, &block
16
+ send :_attributes, attrs, ivn: '@attributes', &block
17
17
  end
18
-
19
- def initialize *args
20
- initialize_attributes
21
- initialize_groups
22
- initialize_boolean_groups
23
- initialize_is_overrides
24
- #super # we'll need this if we prepend Attributes again
25
- end
26
-
27
- module ClassMethods
28
18
 
29
- def define_attr_methods attr, default = nil
30
- define_attr_default attr, default
31
- define_attr_equals attr
32
- define_attr attr
33
- if default == true || default == false # boolean convenience methods
34
- define_attr? attr
35
- define_attr! attr
36
- end
19
+ def _attributes attrs, ivn:, &block
20
+ if instance_variable_defined? ivn
21
+ ivar = instance_variable_get ivn
22
+ else
23
+ ivar = instance_variable_set ivn, Woyo::Attributes::AttributesHash.new
37
24
  end
38
-
39
- def define_attr_default attr, default
40
- define_method "#{attr}_default" do
41
- if default.respond_to? :call
42
- return default.arity == 0 ? default.call : default.call(self)
25
+ return ivar if attrs.empty?
26
+ attrs.each do |attr|
27
+ case
28
+ when attr.kind_of?( Hash )
29
+ attr.each do |attr_sym,default|
30
+ define_attr_methods attr_sym, default, ivn: ivn
31
+ ivar[attr_sym] = send "#{attr_sym}_default"
43
32
  end
44
- default
45
- end
46
- end
47
-
48
- def define_attr_equals attr
49
- define_method "#{attr}=" do |arg|
50
- attributes[attr] = arg
51
- end
52
- end
53
-
54
- def define_attr attr
55
- define_method attr do |arg = nil|
56
- return attributes[attr] = arg unless arg.nil?
57
- return attributes[attr] unless attributes[attr].kind_of? Hash
58
- true_attribute_match = attributes[attr].detect { |name,value| attributes[name] == true }
59
- return true_attribute_match[1] if true_attribute_match
60
- attributes[attr]
61
- end
62
- end
63
-
64
- def define_attr? attr
65
- define_method "#{attr}?" do
66
- ( send attr ) ? true : false
67
- end
68
- end
69
-
70
- def define_attr! attr
71
- define_method "#{attr}!" do
72
- send "#{attr}=", true
73
- end
74
- end
75
-
76
- def attributes *attrs
77
- @attributes ||= []
78
- return @attributes if attrs.empty?
79
- attrs.each do |attr|
80
- if attr.kind_of? Hash
81
- attr.each do |attr_sym,default_value|
82
- @attributes << attr_sym unless @attributes.include? attr_sym
83
- define_attr_methods attr_sym, default_value
84
- end
85
- else
86
- unless @attributes.include? attr
87
- @attributes << attr
88
- define_attr_methods attr
89
- end
33
+ when block
34
+ define_attr_methods attr, block, ivn: ivn
35
+ ivar[attr] = send "#{attr}_default"
36
+ else
37
+ unless ivar.include? attr
38
+ define_attr_methods attr, ivn: ivn
39
+ ivar[attr] = nil
90
40
  end
91
41
  end
92
42
  end
43
+ end
93
44
 
94
- def attribute *attrs
95
- self.attributes *attrs
96
- end
97
-
98
- def groups
99
- @groups ||= {}
45
+ def define_attr_methods( attr, default = nil, ivn: )
46
+ define_attr_default attr, default
47
+ define_attr_equals attr, ivn: ivn
48
+ define_attr attr, ivn: ivn
49
+ if default == true || default == false # boolean convenience methods
50
+ define_attr? attr
51
+ define_attr! attr
100
52
  end
53
+ end
101
54
 
102
- def group sym, *attrs
103
- @groups ||= {}
104
- group = @groups[sym] ? @groups[sym] : ( @groups[sym] = [] )
105
- self.attributes *attrs
106
- attrs.each do |attr|
107
- if attr.kind_of? Hash
108
- attr.each do |attr_sym,default_value|
109
- group << attr_sym
110
- end
111
- else
112
- group << attr
113
- end
114
- end
115
- define_method sym do
116
- groups[sym]
117
- end
118
- group
55
+ def define_attr_default attr, default
56
+ define_singleton_method "#{attr}_default" do
57
+ default
119
58
  end
59
+ end
120
60
 
121
- def boolean_groups
122
- @boolean_groups ||={}
123
- end
124
-
125
- def group! sym, *attrs
126
- @boolean_groups ||= {}
127
- group = @boolean_groups[sym] ? @boolean_groups[sym] : ( @boolean_groups[sym] = [] )
128
- self.attributes *attrs
129
- attrs.each do |attr|
130
- define_attr? attr
131
- define_attr! attr
132
- if attr.kind_of? Hash
133
- attr.each do |attr_sym,default_value|
134
- group << attr_sym
135
- end
136
- else
137
- group << attr
138
- end
139
- end
140
- define_method sym do
141
- boolean_groups[sym]
142
- end
143
- group
61
+ def define_attr_equals( attr, ivn: )
62
+ define_singleton_method "#{attr}=" do |arg|
63
+ ivar = instance_variable_get ivn
64
+ ivar[attr] = arg
144
65
  end
66
+ end
145
67
 
146
- def is *attrs
147
- @is_overrides ||= []
148
- attrs.each do |attr|
149
- @is_overrides << attr unless @is_overrides.include? attr
150
- self.attribute( { attr => true } )
68
+ def define_attr( attr, ivn: )
69
+ define_singleton_method attr do |arg = nil|
70
+ ivar = instance_variable_get ivn
71
+ return ivar[attr] = arg unless arg.nil?
72
+ case
73
+ when ivar[attr].kind_of?( Hash )
74
+ true_attribute_match = ivar[attr].detect { |name,value| ivar[name] == true }
75
+ return true_attribute_match[1] if true_attribute_match
76
+ ivar[attr]
77
+ when ivar[attr].respond_to?( :call )
78
+ return ivar[attr].arity == 0 ? ivar[attr].call : ivar[attr].call(self)
79
+ else
80
+ ivar[attr]
151
81
  end
152
82
  end
83
+ end
153
84
 
154
- def is_overrides
155
- @is_overrides ||= []
156
- end
157
-
158
- end # module ClassMethods
159
-
160
- def initialize_attributes
161
- @attributes = self.class.attributes.each_with_object(Woyo::Attributes::AttributesHash.new) do |attr,hash|
162
- hash[attr] = send "#{attr}_default"
85
+ def define_attr? attr
86
+ define_singleton_method "#{attr}?" do
87
+ ( send attr ) ? true : false
163
88
  end
164
89
  end
165
90
 
166
- def initialize_groups
167
- @groups = {}
168
- self.class.groups.each { |sym,members| @groups[sym] = Woyo::Attributes::Group.new @attributes, *members }
169
- @groups
91
+ def define_attr! attr
92
+ define_singleton_method "#{attr}!" do
93
+ send "#{attr}=", true
94
+ end
170
95
  end
171
96
 
172
- def initialize_boolean_groups
173
- @boolean_groups = {}
174
- self.class.boolean_groups.each { |sym,members| @boolean_groups[sym] = Woyo::Attributes::BooleanGroup.new @attributes, *members }
175
- @boolean_groups
97
+ def is? attr
98
+ send "#{attr}?"
176
99
  end
177
100
 
178
- def initialize_is_overrides
179
- self.class.is_overrides.each { |attr| send "#{attr}!" }
101
+ def is attr
102
+ send "#{attr}=", true
180
103
  end
181
104
 
182
- def attributes
183
- @attributes
184
- end
185
-
186
105
  def groups
187
106
  @groups
188
107
  end
189
108
 
190
- def boolean_groups
191
- @boolean_groups
109
+ def group sym, *attrs
110
+ @groups ||= {}
111
+ grp = @groups[sym] ? @groups[sym] : ( @groups[sym] = Woyo::Attributes::Group.new attributes )
112
+ attributes *attrs
113
+ attrs.each do |attr|
114
+ if attr.kind_of? Hash
115
+ attr.each do |attr_sym,default_value|
116
+ grp << attr_sym
117
+ end
118
+ else
119
+ grp << attr
120
+ end
121
+ end
122
+ define_singleton_method sym do
123
+ @groups[sym]
124
+ end
125
+ grp
192
126
  end
193
127
 
194
- def is? attr
195
- send "#{attr}?"
128
+ def exclusions
129
+ @exclusions
196
130
  end
197
-
198
- def is attr
199
- send "#{attr}=", true
131
+
132
+ def exclusion sym, *attrs
133
+ @exclusions ||= {}
134
+ exc = @exclusions[sym] ? @exclusions[sym] : ( @exclusions[sym] = Woyo::Attributes::Exclusion.new attributes )
135
+ attributes *attrs
136
+ attrs.each do |attr|
137
+ define_attr? attr
138
+ define_attr! attr
139
+ if attr.kind_of? Hash
140
+ attr.each do |attr_sym,default_value|
141
+ exc << attr_sym
142
+ end
143
+ else
144
+ exc << attr
145
+ end
146
+ end
147
+ define_singleton_method sym do
148
+ @exclusions[sym]
149
+ end
150
+ exc
200
151
  end
201
152
 
202
153
  end
@@ -4,7 +4,10 @@ module Woyo
4
4
 
5
5
  class Character < WorldObject
6
6
 
7
- attributes :description, name: lambda { |this| this.id.to_s.capitalize }
7
+ def initialize_object
8
+ super
9
+ attributes :description, name: lambda { |this| this.id.to_s.capitalize }
10
+ end
8
11
 
9
12
  def world
10
13
  @world ||= context if context.is_a? World
@@ -1,7 +1,7 @@
1
1
 
2
2
  module Woyo
3
3
 
4
- module DSL
4
+ module Evaluate
5
5
 
6
6
  def evaluate &block
7
7
  (block.arity < 1 ? (instance_eval &block) : block.call(self)) if block_given?
@@ -35,7 +35,6 @@ module DSL
35
35
  when !#{child} && !known && block_given? then #{child} = self.#{child}s[id] = #{child.capitalize}.new id, context: self, &block
36
36
  when !#{child} && !known && !block_given? then #{child} = self.#{child}s[id] = #{child.capitalize}.new id, context: self
37
37
  end
38
- # maybe: god-like lists of everything at world level... would need unique ids... self.world.#{child}s[id] = #{child} if !known && self.world
39
38
  #{child}
40
39
  end
41
40
 
@@ -33,7 +33,7 @@ module Attributes
33
33
 
34
34
  extend Forwardable
35
35
 
36
- def_delegators :@members, :count
36
+ def_delegators :@members, :count, :<<
37
37
  def_delegators :@attributes, :[], :[]=
38
38
 
39
39
  attr_reader :members, :attributes
@@ -53,19 +53,34 @@ module Attributes
53
53
 
54
54
  end
55
55
 
56
- class BooleanGroup < Group
56
+ class Exclusion < Group
57
57
 
58
58
  attr_reader :default
59
59
 
60
60
  def initialize attributes, *members
61
61
  super
62
- @default = @members.first
63
- self[@default] = true
64
- @members.each { |member| @attributes.add_attribute_listener member, self }
62
+ if @members && ! @members.empty?
63
+ @default = @members.first
64
+ self[@default] = true
65
+ @members.each { |member| @attributes.add_attribute_listener member, self }
66
+ end
67
+ end
68
+
69
+ def << new_member
70
+ raise "#{new_member} is not an attribute" unless @attributes.names.include? new_member
71
+ super
72
+ if @members.count == 1
73
+ @default = new_member
74
+ @attributes.set new_member, true
75
+ else
76
+ @attributes.set new_member, false
77
+ end
78
+ @attributes.add_attribute_listener new_member, self
79
+ self
65
80
  end
66
81
 
67
82
  def []= this_member, value
68
- raise '#{this_member} is not a member of this group' unless @members.include? this_member
83
+ raise "#{this_member} is not a member of this group" unless @members.include? this_member
69
84
  super
70
85
  if value #true
71
86
  # sync group members via AttributesHash#set to prevent triggering notify
@@ -0,0 +1,26 @@
1
+ require_relative 'world_object'
2
+
3
+ module Woyo
4
+
5
+ class Item < WorldObject
6
+
7
+ def initialize_object
8
+ super
9
+ end
10
+
11
+ def location
12
+ self.context.instance_of?( Location ) ? self.context : nil
13
+ end
14
+
15
+ def world
16
+ case
17
+ when self.context.instance_of?( World ) then self.context
18
+ when self.context.instance_of?( Location) then self.context.world
19
+ else nil
20
+ end
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+
@@ -4,12 +4,14 @@ module Woyo
4
4
 
5
5
  class Location < WorldObject
6
6
 
7
- attributes :description, name: lambda { |this| this.id.to_s.capitalize }
7
+ children :way, :item
8
8
 
9
- children :way, :character
9
+ def initialize_object
10
+ super
11
+ end
10
12
 
11
13
  def world
12
- self.context
14
+ context
13
15
  end
14
16
 
15
17
  def here