Class: UU::OS::GVC::Container

Inherits:
Object
  • Object
show all
Defined in:
uu_os_gvc-0.28.16/lib/uu/os/gvc/container.rb

Overview

Abstract container for managing components.

Direct Known Subclasses

VUC::View

Instance Method Summary (collapse)

Constructor Details

- (Container) initialize(data = nil)

Creates new instance of container.

Parameters:

  • data (String, Hash) (defaults to: nil)

    Initialization data in form of JSON string or Hash (optional)



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
# File 'uu_os_gvc-0.28.16/lib/uu/os/gvc/container.rb', line 27

def initialize(data = nil)
  @components = {}
  @messages = []
  @focus_on = nil
  if data.nil?
    return
  end
  if data.kind_of?String
    data = JSON.parse(data, :symbolize_names => true)
  end
  if !data.kind_of?Hash
    raise ArgumentError.new("Container data must be JSON String or Hash, but was #{data.class}.")
  end
  if data[:components]
    data[:components].each do |cmp|
      component = create_component(cmp)
      if (@components[component.id])
        raise ArgumentError.new('Invalid container data. Container contains multiple components with same ID.')
      end
      @components[component.id] = component
    end
  end
  if data[:messages]
    data[:messages].each do |message|
      add_message(message)
    end
  end
end

Instance Method Details

- (Object) add_message(message)

Adds global container message.

Parameters:



89
90
91
92
# File 'uu_os_gvc-0.28.16/lib/uu/os/gvc/container.rb', line 89

def add_message(message)
  msg = UU::OS::GVC::Message.new(message)
  @messages << msg
end

- (Array<UU::OS::GVC::Component>) components

Returns list of all components in container.

Returns:



82
83
84
# File 'uu_os_gvc-0.28.16/lib/uu/os/gvc/container.rb', line 82

def components
  @components.values
end

- (FalseClass, TrueClass) focus(component_id)

Sets focus to component with given ID. Actual behavior of focus depends on type of focused component and also on type of container in which is component placed. Only one component can be focused, setting focus on one component disables focus of previously focused component within container.

Parameters:

  • id (String)

    Unique component ID

Returns:

  • (FalseClass, TrueClass)

    True if focus was set, else false



111
112
113
114
115
# File 'uu_os_gvc-0.28.16/lib/uu/os/gvc/container.rb', line 111

def focus(component_id)
  return false if !get_component_by_id(component_id)
  @focus_on = component_id
  return true
end

- (Object) focused_component

Returns component marked as focused. Nil in case no focus is set to any component.

@return Focused component or nil



120
121
122
# File 'uu_os_gvc-0.28.16/lib/uu/os/gvc/container.rb', line 120

def focused_component
  @components[@focus_on]
end

- (UU::OS::GVC::Component) get_component_by_id(id)

Finds single component matching given unique ID.

Parameters:

  • id (String)

    Unique component ID

Returns:



61
62
63
# File 'uu_os_gvc-0.28.16/lib/uu/os/gvc/container.rb', line 61

def get_component_by_id(id)
  @components[id]
end

- (Array<UU::OS::GVC::Component>) get_components_by_code(code)

Finds all components matching given code. If nil is entered as the code, than all components without a defined code are returned.

Parameters:

  • code (String)

    Component code

Returns:



71
72
73
74
75
76
77
# File 'uu_os_gvc-0.28.16/lib/uu/os/gvc/container.rb', line 71

def get_components_by_code(code)
  result = []
  @components.values.each do |component|
    result << component if component.code == code
  end
  result
end

- (Array<UU::OS::GVC::Message>) messages

Returns list of all messages in container (both global and component related).

Returns:



97
98
99
100
# File 'uu_os_gvc-0.28.16/lib/uu/os/gvc/container.rb', line 97

def messages
  # Disallow direct manipulating with messages
  @messages.clone
end