y_support 2.5.2 → 2.5.3

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: ee5ad7ff95e53cc6726095f24b26a862b4da6eb6
4
- data.tar.gz: 596a5b7d77c3b777091fd49ca219857ced62a7e3
3
+ metadata.gz: 45dd9eb80e6817e7e37732c09782c38da5de8ab0
4
+ data.tar.gz: 66d6e1d35bc5d809506d39a79513498ffce83e68
5
5
  SHA512:
6
- metadata.gz: 95135218b6dff384f1dce606e26e359e15ab8154051d1384645a49dacf795a728f2731eaac966dee1b174268319dd936df410e2b6734f8163a4f1e94c00f1af0
7
- data.tar.gz: 8dac0826c19777545ba983b317ecaf513b6f018b7665c21d7d46d1f585e17dfcc2f3d4f7b9046b6d8a7a4a6d6ff8a7952f1843f88c3a5f481bbb018801c45045
6
+ metadata.gz: 9226a106d85dfbc113bad61619b2a8dddb9af15957e8e1283cbc74ecb54a5af12d5016c73e434f9b23d32868c2527751fb58f2217af31675fece6f0e0e3d0a48
7
+ data.tar.gz: dd21b15c30466bce72b0a7715194574862b4179fd080fa7085a9ded206ff37c3843d234bfd0dfea82f47bb240b5af8dd35240f98599135ac7bda937073e1f969
@@ -4,49 +4,52 @@ require File.dirname( __FILE__ ) + '/../module'
4
4
  require File.dirname( __FILE__ ) + '/../class'
5
5
 
6
6
  class Object
7
- # Assigns prescribed atrributes to the object and makes them accessible with
8
- # getter (reader) methods. Raises NameError should any of the getters shadow /
9
- # overwrite existing methods.
7
+ # Assigns atrributes to the receiver object and makes them
8
+ # accessible via reader methods (aka. getter or selector
9
+ # methods). Raises `NameError` in case of name collisions with
10
+ # existing methods of the receiver object.
10
11
  #
11
12
  def set_attr_with_readers hash
12
13
  hash.each_pair { |ß, value|
13
- fail NameError, "Method \##{ß} already defined!" if methods.include? ß
14
+ fail NameError, "Method \##{ß} already defined on " \
15
+ "#{self}! Use #set_attr_with_readers! to overload " \
16
+ "preexisting methods." if methods.include? ß
14
17
  set_attr_with_readers! ß => value
15
18
  }
16
19
  end
20
+ alias set_attr_with_selectors set_attr_with_readers
17
21
 
18
- # Like +#set_attr_with_readers+, but it overloads existing methods, if present.
19
- # Whereas +#set_attr_with_readers+ guards against the presence of methods with
20
- # the same name, +#set_attr_with_reader!+ overloads them in the following way:
21
- #
22
- # * Colliding instance method defined in the singleton class of the receiver
23
- # is overwritten (redefined).
24
- # * Colliding instance methods defined on the receiver's class ancestors are
25
- # overloaded (partially shadowed). If the method message is sent without
26
- # arguments or block, the reader activates and returns the corresponding
27
- # instance variable regardless of what the behavior of the shadowed method
28
- # might have been. However, if the method message is sent with arguments
29
- # or block, the original method is invoked (via +super+) and its result
30
- # returned.
22
+ # Like +#set_attr_with_readers+, but overloads existing methods
23
+ # if their names collied with the attributes to be set.
24
+ # Overloading is performed in the following way: If the attribute
25
+ # reader is called without parameters, it acts as an attribute
26
+ # reader (as expected). If the user tries to invoke the attribute
27
+ # method with parameters (or a block), the message falls through
28
+ # to the colliding instance method. In this way, both methods
29
+ # are preserved -- collision only happens if the colliding
30
+ # method took no parameters.
31
31
  #
32
32
  def set_attr_with_readers! hash
33
33
  hash.each_pair { |ß, value|
34
+ # puts "Setting @#{ß} of #{self} to #{value}."
34
35
  instance_variable_set "@#{ß}", value
35
36
  singleton_class.class_exec do
36
37
  define_method ß do |*args, &block|
37
- return instance_variable_get "@#{ß}" if args.empty? && block.nil?
38
+ return instance_variable_get "@#{ß}" if
39
+ args.empty? && block.nil?
38
40
  super *args, &block
39
41
  end
40
42
  end
41
43
  }
42
44
  end
43
45
 
44
- # Constructs heir classes (parametrized subclasses) of the supplied modules
45
- # (classes) and makes them available under specified getters. Expects a hash of
46
- # pairs { symbol: class }, and a hash of parameters with which to parametrize
47
- # the modules (classes). The methods guards against collisions in the subclass
48
- # getter symbols, rasing +NameError+ should these shadow or overwrite existing
49
- # methods.
46
+ # Constructs heir classes (parametrized subclasses) of the
47
+ # supplied modules (classes) and makes them available under
48
+ # specified getters. Expects a hash of pairs { symbol: class },
49
+ # and a hash of parameters with which to parametrize the modules
50
+ # (classes). The methods guards against collisions in the
51
+ # subclass getter symbols, rasing +NameError+ should these shadow
52
+ # or overwrite existing methods.
50
53
  #
51
54
  def param_class( hash, with: {} )
52
55
  hash.each { |ß, m|
@@ -63,9 +66,10 @@ class Object
63
66
  return nil
64
67
  end
65
68
 
66
- # Like +#param_class+, but it shadows or overwrites existing methods colliding
67
- # with the getters of the parametrized classes. See +#set_attr_with_readers!"
68
- # for full explanation of the shadowing / overwriting behavior.
69
+ # Like +#param_class+, but it shadows or overwrites existing
70
+ # methods colliding with the getters of the parametrized
71
+ # classes. See +#set_attr_with_readers!" for full explanation of
72
+ # the shadowing / overwriting behavior.
69
73
  #
70
74
  def param_class!( hash, with: {} )
71
75
  hash.each { |ß, m|
@@ -81,4 +85,26 @@ class Object
81
85
  }
82
86
  return nil
83
87
  end
88
+
89
+ # New syntax for #param_class method for creating parametrized
90
+ # subclasses.
91
+ #
92
+ def owns_subclass( name, of:, parametrized_by: {},
93
+ named: proc do |**params|
94
+ param_str = params.empty? ? "" :
95
+ "[#{params.pretty_print}]"
96
+ "#{name}#{param_str}>"
97
+ end,
98
+ overwrite_existing_methods: true )
99
+ if overwrite_existing_methods then
100
+ param_class!( { name => of }, with: parametrized_by )
101
+ else
102
+ param_class( { name => of }, with: parametrized_by )
103
+ end
104
+ # FIXME: This method cannot stay like this. It is not possible
105
+ # for has_subclass method expecting a single class to subclass
106
+ # to rely on param_class method which is intended for multiple
107
+ # classes. Simplify simplify simplify. And change that SO
108
+ # answer when done.
109
+ end
84
110
  end
@@ -1,4 +1,4 @@
1
1
  module YSupport
2
- VERSION = "2.5.2"
2
+ VERSION = "2.5.3"
3
3
  DEBUG = false
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: y_support
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.5.2
4
+ version: 2.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - boris
@@ -163,4 +163,3 @@ test_files:
163
163
  - test/name_magic_test.rb
164
164
  - test/typing_test.rb
165
165
  - test/unicode_test.rb
166
- has_rdoc: