Class: Sirens::ObjectBrowserModel

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

Defined Under Namespace

Classes: InstanceVariable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObjectBrowserModel

Initializing



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sirens/models/object_browser_model.rb', line 6

def initialize()
    super()

    @object = ValueModel.on(nil)

    @object_instance_variables = TreeChoiceModel.new(
            selection: object,
            roots: [InstanceVariable.new(key: nil, value: object)],
            get_children_block: proc { |instance_variable|
                get_child_instance_variables_of(instance_variable.value)
            }
        )

    @text = ValueModel.on('')

    connect_models
end

Instance Attribute Details

#objectObject (readonly)

Accessors



31
32
33
# File 'lib/sirens/models/object_browser_model.rb', line 31

def object
  @object
end

#object_instance_variablesObject (readonly)

Accessors



31
32
33
# File 'lib/sirens/models/object_browser_model.rb', line 31

def object_instance_variables
  @object_instance_variables
end

#textObject (readonly)

Accessors



31
32
33
# File 'lib/sirens/models/object_browser_model.rb', line 31

def text
  @text
end

Instance Method Details

#connect_modelsObject



24
25
26
27
# File 'lib/sirens/models/object_browser_model.rb', line 24

def connect_models()
    @object.add_observer(self, :on_object_changed)
    @object_instance_variables.selection.add_observer(self, :on_instance_variable_changed)
end

#get_child_instance_variables_of(object) ⇒ Object

Get the child instance variables of an object.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/sirens/models/object_browser_model.rb', line 62

def get_child_instance_variables_of(object)
    if object.kind_of?(Array)
        i = -1
        object.collect{ |value|
            i += 1
            InstanceVariable.new(
                key: i,
                value: value
            )
        }
    elsif object.kind_of?(Hash)
      object.collect{ |key, value|
          InstanceVariable.new(
              key: "[#{key.inspect}]",
              value: value
          )
      }
    else
        object.instance_variables.collect{ |inst_var_name|
            InstanceVariable.new(
                key: inst_var_name,
                value: object.instance_variable_get(inst_var_name)
            )
        }
    end
end

#on_instance_variable_changed(announcement) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/sirens/models/object_browser_model.rb', line 51

def on_instance_variable_changed(announcement)
    instance_variable = announcement.new_value.last

    value = instance_variable.nil? ? '' : instance_variable.value.inspect

    @text.set_value(value)
end

#on_object_changed(announcement) ⇒ Object

Events



43
44
45
46
47
48
49
# File 'lib/sirens/models/object_browser_model.rb', line 43

def on_object_changed(announcement)
    new_object = announcement.new_value

    @object_instance_variables.set_tree_roots(
        [InstanceVariable.new(key: nil, value: new_object)]
    )
end

#selected_inst_var_valueObject



35
36
37
38
39
# File 'lib/sirens/models/object_browser_model.rb', line 35

def selected_inst_var_value()
    tree_selection = object_instance_variables.selection.value.last

    tree_selection.nil? ? nil : tree_selection.value
end