Class: Sirens::ModuleBrowserModel

Inherits:
Object
  • Object
show all
Defined in:
lib/sirens/models/module_browser_model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModuleBrowserModel

Initializing



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sirens/models/module_browser_model.rb', line 6

def initialize()
    super()

    @namespaces = TreeChoiceModel.new(
        selection: nil,
        roots: get_root_namespaces,
        get_children_block: proc { |namespace| get_modules_in(namespace) }
    )

    @modules = ChoiceModel.new(selection: nil, choices: [])
    @module_ancestors = ChoiceModel.new(selection: nil, choices: [])
    @methods = ChoiceModel.new(selection: nil, choices: [])
    @constants = ChoiceModel.new(selection: nil, choices: [])

    @instance_or_class_methods_chooser = ChoiceModel.new(selection: :instance, choices: [:instance, :class])
    @show_inherit_methods = ValueModel.on(false)

    @show_public_methods = ValueModel.on(true)
    @show_protected_methods = ValueModel.on(false)
    @show_private_methods = ValueModel.on(false)

    @method_source_code = MethodModel.new

    connect_models
end

Instance Attribute Details

#constantsObject (readonly)

Returns the value of attribute constants.



32
33
34
# File 'lib/sirens/models/module_browser_model.rb', line 32

def constants
  @constants
end

#instance_or_class_methods_chooserObject (readonly)

Returns the value of attribute instance_or_class_methods_chooser.



32
33
34
# File 'lib/sirens/models/module_browser_model.rb', line 32

def instance_or_class_methods_chooser
  @instance_or_class_methods_chooser
end

#method_source_codeObject (readonly)

Returns the value of attribute method_source_code.



32
33
34
# File 'lib/sirens/models/module_browser_model.rb', line 32

def method_source_code
  @method_source_code
end

#methodsObject (readonly)

Returns the value of attribute methods.



32
33
34
# File 'lib/sirens/models/module_browser_model.rb', line 32

def methods
  @methods
end

#module_ancestorsObject (readonly)

Returns the value of attribute module_ancestors.



32
33
34
# File 'lib/sirens/models/module_browser_model.rb', line 32

def module_ancestors
  @module_ancestors
end

#modulesObject (readonly)

Returns the value of attribute modules.



32
33
34
# File 'lib/sirens/models/module_browser_model.rb', line 32

def modules
  @modules
end

#namespacesObject (readonly)

Returns the value of attribute namespaces.



32
33
34
# File 'lib/sirens/models/module_browser_model.rb', line 32

def namespaces
  @namespaces
end

#show_inherit_methodsObject (readonly)

Returns the value of attribute show_inherit_methods.



32
33
34
# File 'lib/sirens/models/module_browser_model.rb', line 32

def show_inherit_methods
  @show_inherit_methods
end

#show_private_methodsObject (readonly)

Returns the value of attribute show_private_methods.



32
33
34
# File 'lib/sirens/models/module_browser_model.rb', line 32

def show_private_methods
  @show_private_methods
end

#show_protected_methodsObject (readonly)

Returns the value of attribute show_protected_methods.



32
33
34
# File 'lib/sirens/models/module_browser_model.rb', line 32

def show_protected_methods
  @show_protected_methods
end

#show_public_methodsObject (readonly)

Returns the value of attribute show_public_methods.



32
33
34
# File 'lib/sirens/models/module_browser_model.rb', line 32

def show_public_methods
  @show_public_methods
end

Instance Method Details

#actual_namespace_hierarchy(a_module) ⇒ Object

Actions



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sirens/models/module_browser_model.rb', line 60

def actual_namespace_hierarchy(a_module)
    namespaces = a_module.name.split('::')

    namespaces.pop if a_module.kind_of?(Class)

    hierarchy = [Object]

    namespaces.each { |module_name|
        hierarchy << hierarchy.last.const_get(module_name)
    }

    hierarchy
end

#all_methods_defined_in(mod) ⇒ Object



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
204
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/sirens/models/module_browser_model.rb', line 170

def all_methods_defined_in(mod)
    return [] if mod.nil?

    modules = show_inherit_methods.value === true ?
        mod.ancestors : [mod]

    all_methods = []

    modules.each do |a_module|
        if showing_instance_methods?
            all_methods.concat(
                a_module.private_instance_methods(false).collect { |method_name|
                    Sirens::Method.new(
                        mod: a_module,
                        name: method_name,
                        visibility: :private,
                        instance_method: true
                    )
                }
            )

            all_methods.concat(
                a_module.protected_instance_methods(false).collect { |method_name|
                    Sirens::Method.new(
                        mod: a_module,
                        name: method_name,
                        visibility: :protected,
                        instance_method: true
                    )
                }
            )
            
            all_methods.concat(
                a_module.public_instance_methods(false).collect { |method_name|
                    Sirens::Method.new(
                        mod: a_module,
                        name: method_name,
                        visibility: :public,
                        instance_method: true
                    )
                }
            )
        else
            all_methods.concat(
                a_module.private_methods(false).collect { |method_name|
                    Sirens::Method.new(
                        mod: a_module,
                        name: method_name,
                        visibility: :private,
                        instance_method: false
                    )
                }
            )

            all_methods.concat(
                a_module.protected_methods(false).collect { |method_name|
                    Sirens::Method.new(
                        mod: a_module,
                        name: method_name,
                        visibility: :protected,
                        instance_method: false
                    )
                }
            )
            
            all_methods.concat(
                a_module.public_methods(false).collect { |method_name|
                    Sirens::Method.new(
                        mod: a_module,
                        name: method_name,
                        visibility: :public,
                        instance_method: false
                    )
                }
            )
        end
    end

    all_methods.sort_by { |method| method.name }
end

#connect_modelsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sirens/models/module_browser_model.rb', line 44

def connect_models()
    @namespaces.selection.add_observer(self, :on_namespace_changed)
    @modules.selection.add_observer(self, :on_class_changed)
    @module_ancestors.selection.add_observer(self, :on_module_changed)
    @methods.selection.add_observer(self, :on_method_changed)

    @instance_or_class_methods_chooser.selection.add_observer(self, :on_methods_filter_changed)
    @show_inherit_methods.add_observer(self, :on_methods_filter_changed)

    @show_public_methods.add_observer(self, :on_methods_filter_changed)
    @show_protected_methods.add_observer(self, :on_methods_filter_changed)
    @show_private_methods.add_observer(self, :on_methods_filter_changed)
end

#get_all_ancestors_of(a_class) ⇒ Object

Answers all the modules in the given class



154
155
156
157
158
# File 'lib/sirens/models/module_browser_model.rb', line 154

def get_all_ancestors_of(a_class)
    return [] if a_class.nil?

    a_class.ancestors.reverse
end

#get_all_constants_in(a_module) ⇒ Object

Answers all the constants in the given class



254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/sirens/models/module_browser_model.rb', line 254

def get_all_constants_in(a_module)
    return [] if a_module.nil?

    inherit = show_inherit_methods.value

    a_module.constants(inherit)
        .collect { |constant_name|
            ConstantModel.new(name: constant_name, value: a_module.const_get(constant_name))
        }
        .select { |constant_model| ! constant_model.value.kind_of?(Module) }
        .sort_by { |constant_model| constant_model.name }
end

#get_all_methods_in(a_module) ⇒ Object

Answers all the methods in the given class



163
164
165
166
167
168
# File 'lib/sirens/models/module_browser_model.rb', line 163

def get_all_methods_in(a_module)
    all_methods_defined_in(a_module)
        .reject { |method| method.is_private? if !showing_private_methods? }
        .reject { |method| method.is_protected? if !showing_protected_methods? }
        .reject { |method| method.is_public? if !showing_public_methods? }
end

#get_modules_in(a_module) ⇒ Object

Answers child modules of a namespace



140
141
142
143
144
145
146
147
148
149
# File 'lib/sirens/models/module_browser_model.rb', line 140

def get_modules_in(a_module)
    return [] if a_module.nil?

    modules = a_module.constants(false)
        .collect { |constant| a_module.const_get(constant) }
        .select { |constant| constant.kind_of?(Module) }
        .sort_by { |a_module| a_module.name }

    modules.prepend(a_module)
end

#get_root_namespacesObject

Answers all the namespaces in the running environment



133
134
135
# File 'lib/sirens/models/module_browser_model.rb', line 133

def get_root_namespaces()
    return [Object]
end

#on_class_changed(announcement) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/sirens/models/module_browser_model.rb', line 96

def on_class_changed(announcement)
    a_class = announcement.new_value

    ancestors = get_all_ancestors_of(a_class)

    module_ancestors.set_choices(ancestors)

    module_ancestors.set_selection(ancestors.last) unless ancestors.empty?
end

#on_method_changed(announcement) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/sirens/models/module_browser_model.rb', line 117

def on_method_changed(announcement)
    selected_method = announcement.new_value

    if selected_method.nil?
        method_source_code.set_method(nil)
        return
    end

    method_source_code.set_method(selected_method)
end

#on_methods_filter_changed(announcement = nil) ⇒ Object



110
111
112
113
114
115
# File 'lib/sirens/models/module_browser_model.rb', line 110

def on_methods_filter_changed(announcement = nil)
    a_module = module_ancestors.selection.value

    methods.set_choices(get_all_methods_in(a_module))
    constants.set_choices(get_all_constants_in(a_module))
end

#on_module_changed(announcement) ⇒ Object



106
107
108
# File 'lib/sirens/models/module_browser_model.rb', line 106

def on_module_changed(announcement)
    on_methods_filter_changed()
end

#on_namespace_changed(announcement) ⇒ Object

Events



90
91
92
93
94
# File 'lib/sirens/models/module_browser_model.rb', line 90

def on_namespace_changed(announcement)
    namespace_hierarchy = announcement.new_value

    modules.set_choices(get_modules_in(namespace_hierarchy.last))
end

#select_class(a_class) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/sirens/models/module_browser_model.rb', line 80

def select_class(a_class)
    hierarchy = actual_namespace_hierarchy(a_class)

    @namespaces.set_selection(hierarchy)
    @modules.set_selection(a_class)
    @module_ancestors.set_selection(a_class)
end

#select_namespace(namespace) ⇒ Object



74
75
76
77
78
# File 'lib/sirens/models/module_browser_model.rb', line 74

def select_namespace(namespace)
    hierarchy = actual_namespace_hierarchy(namespace)

    @namespaces.set_selection(hierarchy)
end

#showing_instance_methods?Boolean

Asking

Returns:

  • (Boolean)


269
270
271
# File 'lib/sirens/models/module_browser_model.rb', line 269

def showing_instance_methods?()
    instance_or_class_methods_chooser.selection.value == :instance
end

#showing_private_methods?Boolean

Returns:

  • (Boolean)


273
274
275
# File 'lib/sirens/models/module_browser_model.rb', line 273

def showing_private_methods?()
    show_private_methods.value
end

#showing_protected_methods?Boolean

Returns:

  • (Boolean)


277
278
279
# File 'lib/sirens/models/module_browser_model.rb', line 277

def showing_protected_methods?()
    show_protected_methods.value
end

#showing_public_methods?Boolean

Returns:

  • (Boolean)


281
282
283
# File 'lib/sirens/models/module_browser_model.rb', line 281

def showing_public_methods?()
    show_public_methods.value
end