Module: Utilrb::Models

Included in:
Module
Defined in:
lib/utilrb/models/registration.rb,
lib/utilrb/models/inherited_enumerable.rb

Defined Under Namespace

Modules: Registration

Instance Method Summary collapse

Instance Method Details

#define_inherited_enumerable(name, attribute_name = name, options = Hash.new, &init) ⇒ Object

Helper method for inherited_enumerable

It is called in the context of the singleton class of the module/class on which inherited_enumerable is called



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/utilrb/models/inherited_enumerable.rb', line 12

def define_inherited_enumerable(name, attribute_name = name, options = Hash.new, &init) # :nodoc:
    # Set up the attribute accessor
  attribute(attribute_name, &init)
  class_eval { private "#{attribute_name}=" }

    promote = method_defined?("promote_#{name}")
  options[:enum_with] ||= :each

    class_eval "    def all_\#{name}; each_\#{name}.to_a end\n    def self_\#{name}; @\#{attribute_name} end\n    EOF\n\n    if options[:map]\n        class_eval <<-EOF, __FILE__, __LINE__+1\n        def find_\#{name}(key)\n            raise ArgumentError, \"nil cannot be used as a key in find_\#{name}\" if !key\n            each_\#{name}(key, true) do |value|\n                return value\n            end\n            nil\n        end\n        def has_\#{name}?(key)\n            ancestors = self.ancestors\n            if ancestors.first != self\n                ancestors.unshift self\n            end\n            for klass in ancestors\n                if klass.instance_variable_defined?(:@\#{attribute_name})\n                    return true if klass.\#{attribute_name}.has_key?(key)\n                end\n            end\n            false\n        end\n        EOF\n    end\n\n    class_eval <<-EOF, __FILE__, __LINE__+1\n    def clear_\#{attribute_name}\n        \#{attribute_name}.clear\n        for klass in ancestors\n            if klass.instance_variable_defined?(:@\#{attribute_name})\n                klass.\#{attribute_name}.clear\n            end\n        end\n    end\n    EOF\n\n    if !promote\n        if options[:map]\n            define_inherited_enumerable_map_without_promotion(name, attribute_name, options)\n        else\n            define_inherited_enumerable_nomap_without_promotion(name, attribute_name, options)\n        end\n    else\n        if options[:map]\n            define_inherited_enumerable_map_with_promotion(name, attribute_name, options)\n        else\n            define_inherited_enumerable_nomap_with_promotion(name, attribute_name, options)\n        end\n    end\nend\n", __FILE__, __LINE__+1

#define_inherited_enumerable_map_with_promotion(name, attribute_name, options) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/utilrb/models/inherited_enumerable.rb', line 143

def define_inherited_enumerable_map_with_promotion(name, attribute_name, options)
    class_eval "    def each_\#{name}(key = nil, uniq = true)\n        if !block_given?\n            return enum_for(:each_\#{name}, key, uniq)\n        end\n\n        ancestors = self.ancestors\n        if ancestors.first != self\n            ancestors.unshift self\n        end\n        if key\n            promotions = []\n            for klass in ancestors\n                if klass.instance_variable_defined?(:@\#{attribute_name})\n                    if klass.\#{attribute_name}.has_key?(key)\n                        value = klass.\#{attribute_name}[key]\n                        for p in promotions\n                            value = p.promote_\#{name}(key, value)\n                        end\n                        yield(value)\n                        return self if uniq\n                    end\n                end\n                promotions.unshift(klass) if klass.respond_to?(\"promote_\#{name}\")\n            end\n        elsif !uniq\n            promotions = []\n            for klass in ancestors\n                if klass.instance_variable_defined?(:@\#{attribute_name})\n                    klass.\#{attribute_name}.\#{options[:enum_with]} do |key, value|\n                        for p in promotions\n                            value = p.promote_\#{name}(key, value)\n                        end\n                        yield(key, value)\n                    end\n                end\n                promotions.unshift(klass) if klass.respond_to?(\"promote_\#{name}\")\n            end\n        else\n            seen = Set.new\n            promotions = []\n            for klass in ancestors\n                if klass.instance_variable_defined?(:@\#{attribute_name})\n                    klass.\#{attribute_name}.\#{options[:enum_with]} do |key, value|\n                        unless seen.include?(key)\n                            for p in promotions\n                                value = p.promote_\#{name}(key, value)\n                            end\n                            seen << key\n                            yield(key, value)\n                        end\n                    end\n                end\n                promotions.unshift(klass) if klass.respond_to?(\"promote_\#{name}\")\n            end\n        end\n        self\n    end\n    EOF\nend\n", __FILE__, __LINE__+1

#define_inherited_enumerable_map_without_promotion(name, attribute_name, options) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/utilrb/models/inherited_enumerable.rb', line 75

def define_inherited_enumerable_map_without_promotion(name, attribute_name, options)
    class_eval "    def each_\#{name}(key = nil, uniq = true)\n        if !block_given?\n            return enum_for(:each_\#{name}, key, uniq)\n        end\n\n        ancestors = self.ancestors\n        if ancestors.first != self\n            ancestors.unshift self\n        end\n        if key\n            for klass in ancestors\n                if klass.instance_variable_defined?(:@\#{attribute_name})\n                    if klass.\#{attribute_name}.has_key?(key)\n                        yield(klass.\#{attribute_name}[key])\n                        return self if uniq\n                    end\n                end\n            end\n        elsif !uniq\n            for klass in ancestors\n                if klass.instance_variable_defined?(:@\#{attribute_name})\n                    klass.\#{attribute_name}.\#{options[:enum_with]} do |el|\n                        yield(el)\n                    end\n                end\n            end\n        else\n            seen = Set.new\n            for klass in ancestors\n                if klass.instance_variable_defined?(:@\#{attribute_name})\n                    klass.\#{attribute_name}.\#{options[:enum_with]} do |el| \n                        unless seen.include?(el.first)\n                            seen << el.first\n                            yield(el)\n                        end\n                    end\n                end\n            end\n\n        end\n        self\n    end\n    EOF\nend\n", __FILE__, __LINE__+1

#define_inherited_enumerable_nomap_with_promotion(name, attribute_name, options) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/utilrb/models/inherited_enumerable.rb', line 205

def define_inherited_enumerable_nomap_with_promotion(name, attribute_name, options)
    class_eval "    def each_\#{name}\n        if !block_given?\n            return enum_for(:each_\#{name})\n        end\n\n        ancestors = self.ancestors\n        if ancestors.first != self\n            ancestors.unshift self\n        end\n        promotions = []\n        for klass in ancestors\n            if klass.instance_variable_defined?(:@\#{attribute_name})\n                klass.\#{attribute_name}.\#{options[:enum_with]} do |value|\n                    for p in promotions\n                        value = p.promote_\#{name}(value)\n                    end\n                    yield(value)\n                end\n            end\n            promotions.unshift(klass) if klass.respond_to?(\"promote_\#{name}\")\n        end\n        self\n    end\n    EOF\nend\n", __FILE__, __LINE__+1

#define_inherited_enumerable_nomap_without_promotion(name, attribute_name, options) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/utilrb/models/inherited_enumerable.rb', line 122

def define_inherited_enumerable_nomap_without_promotion(name, attribute_name, options)
    class_eval "    def each_\#{name}\n        if !block_given?\n            return enum_for(:each_\#{name})\n        end\n\n        ancestors = self.ancestors\n        if ancestors.first != self\n            ancestors.unshift self\n        end\n        for klass in ancestors\n            if klass.instance_variable_defined?(:@\#{attribute_name})\n                klass.\#{attribute_name}.\#{options[:enum_with]} { |el| yield(el) }\n            end\n        end\n        self\n    end\n    EOF\nend\n", __FILE__, __LINE__+1

#inherited_enumerable(name, attribute_name = name, options = Hash.new, &init) ⇒ Object

Defines an attribute as being enumerable in the class instance and in the whole class inheritance hierarchy. More specifically, it defines a each_#{name}(&iterator) instance method and a each_#{name}(&iterator) class method which iterates (in order) on

  • the instance #name attribute

  • the singleton class #name attribute

  • the class #name attribute

  • the superclass #name attribute

  • the superclass’ superclass #name attribute

This method can be used on modules, in which case the module is used as if it was part of the inheritance hierarchy.

The name option defines the enumeration method name (value will define a each_value method). attribute_name defines the attribute name. init is a block called to initialize the attribute. Valid options in options are:

map

If true, the attribute should respond to []. In that case, the enumeration method is each_value(key = nil, uniq = false) If key is given, we iterate on the values given by attribute[key]. If uniq is true, the enumeration will yield at most one value for each key found (so, if both key and uniq are given, the enumeration yields at most one value). See the examples below

enum_with

the enumeration method of the enumerable, if it is not each

Example

Let’s define some classes and look at the ancestor chain

class A;  end
module M; end
class B < A; include M end
A.ancestors # => [A, Object, Kernel]
B.ancestors # => [B, M, A, Object, Kernel]

Attributes for which ‘map’ is not set

class A
  inherited_enumerable("value", "values") do
      Array.new
  end
end
module M
  inherited_enumerable("mod") do
      Array.new
  end
end

A.values << 1 # => [1]
B.values << 2 # => [2]
M.mod << 1 # => [1]
b = B.new 
class << b
    self.values << 3 # => [3]
    self.mod << 4 # => [4]
end
M.mod << 2 # => [1, 2]

A.enum_for(:each_value).to_a # => [1]
B.enum_for(:each_value).to_a # => [2, 1]
b.singleton_class.enum_for(:each_value).to_a # => [3, 2, 1]
b.singleton_class.enum_for(:each_mod).to_a # => [4, 1, 2]

Attributes for which ‘map’ is set

class A
  inherited_enumerable("mapped", "map", :map => true) do
      Hash.new { |h, k| h[k] = Array.new }
  end
end

A.map['name'] = 'A' # => "A"
A.map['universe'] = 42
B.map['name'] = 'B' # => "B"
B.map['half_of_it'] = 21

Let’s see what happens if we don’t specify the key option.

A.enum_for(:each_mapped).to_a # => [["name", "A"], ["universe", 42]]

If the uniq option is set (the default), we see only B’s value for ‘name’

B.enum_for(:each_mapped).to_a # => [["half_of_it", 21], ["name", "B"], ["universe", 42]]

If the uniq option is not set, we see both values for ‘name’. Note that since ‘map’ is a Hash, the order of keys in one class is not guaranteed. Nonetheless, we have the guarantee that values from B appear before those from A

B.enum_for(:each_mapped, nil, false).to_a # => [["half_of_it", 21], ["name", "B"], ["name", "A"], ["universe", 42]]

Now, let’s see how ‘key’ behaves

A.enum_for(:each_mapped, 'name').to_a # => ["A"]
B.enum_for(:each_mapped, 'name').to_a # => ["B"]
B.enum_for(:each_mapped, 'name', false).to_a # => ["B", "A"]


326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/utilrb/models/inherited_enumerable.rb', line 326

def inherited_enumerable(name, attribute_name = name, options = Hash.new, &init)
  singleton_class.class_eval { define_inherited_enumerable(name, attribute_name, options, &init) }

  if is_a?(Module) && !is_a?(Class)
 unless const_defined_here?(:ClassExtension)
    const_set(:ClassExtension, Module.new)
 end
 class_extension = const_get(:ClassExtension)
 class_extension.class_eval do
    define_inherited_enumerable(name, attribute_name, options, &init)
 end
  end
end