Module: Receiver::ClassMethods
- Defined in:
- lib/gorillib/receiver/validations.rb,
lib/gorillib/receiver.rb,
lib/gorillib/receiver.rb
Overview
methods become class-level
Instance Method Summary collapse
-
#after_receive(&block) ⇒ Object
make a block to run after each time .receive! is invoked.
- #consume_tuple(tuple) ⇒ Object
-
#members ⇒ Object
By default, the hashlike methods iterate over the receiver attributes.
-
#rcvr(name, type, info = {}) ⇒ Object
define a receiver attribute.
-
#rcvr_accessor(name, type, info = {}) ⇒ Object
defines a receiver attribute, an attr_reader and an attr_writer attr_reader is skipped if the getter method is already defined; attr_writer is skipped if the setter method is already defined;.
-
#rcvr_reader(name, type, info = {}) ⇒ Object
defines a receiver attribute and an attr_reader attr_reader is skipped if the getter method is already defined.
-
#rcvr_remaining(name, info = {}) ⇒ Object
Defines a receiver for attributes sent to receive! that are * not defined as receivers * attribute name does not start with ‘_’.
-
#rcvr_writer(name, type, info = {}) ⇒ Object
defines a receiver attribute and an attr_writer attr_writer is skipped if the setter method is already defined.
-
#receive(*args) ⇒ Object
Returns a new instance with the given hash used to set all rcvrs.
-
#receiver_defaults ⇒ Object
a hash from attribute names to their default values if given.
-
#required_rcvrs ⇒ Object
class method gives info for all receiver attributes with required => true.
-
#tuple_keys ⇒ Object
returns an in-order traversal of the.
Instance Method Details
#after_receive(&block) ⇒ Object
make a block to run after each time .receive! is invoked
252 253 254 |
# File 'lib/gorillib/receiver.rb', line 252 def after_receive &block self.after_receivers += [block] end |
#consume_tuple(tuple) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/gorillib/receiver.rb', line 316 def consume_tuple(tuple) obj = self.new receiver_attrs.each do |attr, info| if info[:type].respond_to?(:consume_tuple) val = info[:type].consume_tuple(tuple) else val = tuple.shift end # obj.send("receive_#{attr}", val) obj.send("#{attr}=", val) end obj end |
#members ⇒ Object
By default, the hashlike methods iterate over the receiver attributes. If you want to filter our add to the keys list, override this method
382 383 384 |
# File 'lib/gorillib/receiver.rb', line 382 def members receiver_attr_names end |
#rcvr(name, type, info = {}) ⇒ Object
define a receiver attribute. automatically generates an attr_accessor on the class if none exists
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/gorillib/receiver.rb', line 229 def rcvr name, type, info={} name = name.to_sym type = type_to_klass(type) body = receiver_body_for(type, info) if body.is_a?(String) class_eval(%Q{ def receive_#{name}(v) self.instance_variable_set("@#{name}", (#{body})) end}, __FILE__, __LINE__ + 1) else define_method("receive_#{name}") do |*args| v = body.call(*args) self.instance_variable_set("@#{name}", v) v end end # careful here: don't modify parent's class_attribute in-place self.receiver_attrs = self.receiver_attrs.dup self.receiver_attr_names += [name] unless receiver_attr_names.include?(name) self.receiver_attrs[name] = info.merge({ :name => name, :type => type }) end |
#rcvr_accessor(name, type, info = {}) ⇒ Object
defines a receiver attribute, an attr_reader and an attr_writer attr_reader is skipped if the getter method is already defined; attr_writer is skipped if the setter method is already defined;
259 260 261 262 263 |
# File 'lib/gorillib/receiver.rb', line 259 def rcvr_accessor name, type, info={} attr_reader(name) unless method_defined?(name) attr_writer(name) unless method_defined?("#{name}=") rcvr name, type, info end |
#rcvr_reader(name, type, info = {}) ⇒ Object
defines a receiver attribute and an attr_reader attr_reader is skipped if the getter method is already defined.
266 267 268 269 |
# File 'lib/gorillib/receiver.rb', line 266 def rcvr_reader name, type, info={} attr_reader(name) unless method_defined?(name) rcvr name, type, info end |
#rcvr_remaining(name, info = {}) ⇒ Object
Defines a receiver for attributes sent to receive! that are
-
not defined as receivers
-
attribute name does not start with ‘_’
289 290 291 292 293 294 295 |
# File 'lib/gorillib/receiver.rb', line 289 def rcvr_remaining name, info={} rcvr_reader name, Hash, info after_receive do |hsh| remaining_vals_hsh = hsh.reject{|k,v| (receiver_attrs.include?(k)) || (k.to_s =~ /^_/) } self._receive_attr name, remaining_vals_hsh end end |
#rcvr_writer(name, type, info = {}) ⇒ Object
defines a receiver attribute and an attr_writer attr_writer is skipped if the setter method is already defined.
272 273 274 275 |
# File 'lib/gorillib/receiver.rb', line 272 def rcvr_writer name, type, info={} attr_writer(name) unless method_defined?("#{name}=") rcvr name, type, info end |
#receive(*args) ⇒ Object
Returns a new instance with the given hash used to set all rcvrs.
All args up to the last one are passed to the initializer. The last arg must be a hash – its attributes are set on the newly-created object
214 215 216 217 218 219 |
# File 'lib/gorillib/receiver.rb', line 214 def receive *args hsh = args.pop || {} raise ArgumentError, "Can't receive (it isn't hashlike): {#{hsh.inspect}} -- the hsh should be the *last* arg" unless hsh.respond_to?(:[]) && hsh.respond_to?(:has_key?) obj = self.new(*args) obj.receive!(hsh) end |
#receiver_defaults ⇒ Object
a hash from attribute names to their default values if given
298 299 300 301 302 303 304 |
# File 'lib/gorillib/receiver.rb', line 298 def receiver_defaults defs = {} receiver_attrs.each do |name, info| defs[name] = info[:default] if info.has_key?(:default) end defs end |
#required_rcvrs ⇒ Object
class method gives info for all receiver attributes with required => true
25 26 27 |
# File 'lib/gorillib/receiver/validations.rb', line 25 def required_rcvrs receiver_attrs.select{|name, info| info[:required] } end |
#tuple_keys ⇒ Object
returns an in-order traversal of the
308 309 310 311 312 313 314 |
# File 'lib/gorillib/receiver.rb', line 308 def tuple_keys return @tuple_keys if @tuple_keys @tuple_keys = self @tuple_keys = receiver_attrs.map do |attr, info| info[:type].try(:tuple_keys) || attr end.flatten end |