Module: QML::Reactive::Object::ClassMethods

Included in:
QML::Reactive::Object
Defined in:
lib/qml/reactive/object.rb

Instance Method Summary collapse

Instance Method Details

#alias_property(name, original_name) ⇒ Symbol

Aliases a property.

Parameters:

  • name (#to_sym)
  • original_name (#to_sym)

Returns:

  • (Symbol)

    The new name



215
216
217
218
219
220
221
# File 'lib/qml/reactive/object.rb', line 215

def alias_property(name, original_name)
  name.to_sym.tap do |name|
    original_name = original_name.to_sym
    add_property(instance_property(original_name).alias(name))
    alias_signal("#{name}_changed", "#{original_name}_changed")
  end
end

#alias_signal(name, original_name) ⇒ Symbol

Aliases a signal.

Parameters:

  • name (#to_sym)
  • original_name (#to_sym)

Returns:

  • (Symbol)

    The new name



174
175
176
177
178
179
# File 'lib/qml/reactive/object.rb', line 174

def alias_signal(name, original_name)
  name.to_sym.tap do |name|
    original_name = original_name.to_sym
    add_signal(instance_signal(original_name).alias(name))
  end
end

#instance_properties(include_super = true) ⇒ Array<Symbol>

Returns all property names for the class.

Parameters:

  • include_super (true|false) (defaults to: true)

Returns:

  • (Array<Symbol>)


# File 'lib/qml/reactive/object.rb', line 96

#instance_property(name) ⇒ QML::Reactive::UnboundProperty

Returns an unbound property.

Parameters:

  • name (Symbol)

    The property name

Returns:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/qml/reactive/object.rb', line 105

[%w{signal signals}, %w{property properties}].each do |singular, plural|
  class_eval <<-EOS, __FILE__, __LINE__ + 1
    def instance_#{singular}_hash(include_super = true)
      if include_super && superclass.include?(Object)
        superclass.instance_#{singular}_hash.merge instance_#{singular}_hash(false)
      else
        @instance_#{singular}_hash ||= {}
      end
    end

    def instance_#{plural}(include_super = true)
      instance_#{singular}_hash(include_super).keys
    end

    def instance_#{singular}(name)
      instance_#{singular}_hash[name] or fail ::NameError, "undefined #{singular} '\#{name}' for class '\#{self}'"
    end
  EOS
end

#instance_signal(name) ⇒ QML::Reactive::UnboundSignal

Returns an unbound signal.

Parameters:

  • name (Symbol)

    The signal name

Returns:



# File 'lib/qml/reactive/object.rb', line 91

#instance_signals(include_super = true) ⇒ Array<Symbol>

Returns all signal names for the class.

Parameters:

  • include_super (true|false) (defaults to: true)

Returns:

  • (Array<Symbol>)


# File 'lib/qml/reactive/object.rb', line 86

#on(signal_name) { ... } ⇒ Object

Adds a signal handler.

Parameters:

  • signal_name

    The name of the signal

Yields:

  • The block that is connected to the signal during object initialization



226
227
228
229
230
231
232
# File 'lib/qml/reactive/object.rb', line 226

def on(signal_name, &block)
  # just for check
  instance_signal(signal_name)
  @initial_connections_hash ||= {}
  @initial_connections_hash[signal_name] ||= []
  @initial_connections_hash[signal_name] << block
end

#on_changed(property_name) { ... } ⇒ Object

Adds a handler to a property change signal.

Examples:

class Foo
  property :bar, ''
  on_changed :bar do
    some_action
  end
  def some_action
    ...
  end
end

Parameters:

  • property_name

    The name of the property

Yields:

  • The block that is connected to the property change signal during object initialization

See Also:



248
249
250
# File 'lib/qml/reactive/object.rb', line 248

def on_changed(property_name, &block)
  on(:"#{property_name}_changed", &block)
end

#property(name, init_value = nil, opts = {}) { ... } ⇒ Symbol

Defines a property for the class.

Examples:

class Foo
  include QML::Reactive::Object
  property(:name) { 'hogehoge' }
  ...
end
Foo.new.name #=> 'hogehoge'
Foo.new.name = 'foobar'
Foo.new.name #=> 'foobar'
Foo.new.name_changed.connect do |new_name|
  ...

class Bar < Foo
  property(:name) { 'piyopiyo' }
end
Bar.new.name #=> 'piyopiyo'

Parameters:

  • name (#to_sym)

    The name of the property

  • init_value (defaults to: nil)

    The initial value (optional)

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :property (Proc) — default: nil

Yields:

  • The initial property binding (optional)

Returns:

  • (Symbol)

    The name



204
205
206
207
208
209
# File 'lib/qml/reactive/object.rb', line 204

def property(name, init_value = nil, opts = {}, &init_binding)
  name.to_sym.tap do |name|
    add_property(UnboundProperty.new(name, init_value, init_binding, self, opts[:property]))
    signal("#{name}_changed", [:new_value], signal: -> { property(name).changed })
  end
end

#signal(name, params, opts = {}) ⇒ Symbol

Defines a signal for the class. The signal will be variadic if args == nil.

Examples:

class Button
  include QML::Reactive::Object
  signal :pressed, [:pos]
  def press(pos)
    pressed.emit(pos)
  end
end

button = Button.new
button.pressed.connect { |pos| puts "Pressed at #{pos}" }
button.press([10, 20])

class ColorButton < Button
  signal :pressed, [:pos, :color]
end

color_button = ColorButton.new
color_button.pressed.connect { |pos, color| "#{color} button pressed at #{pos}" }
color_button.press([10, 20], 'red')

Parameters:

  • name (#to_sym)

    The signal name

  • params (Array<#to_sym>, nil)

    The signal parameter names

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :signal (Proc) — default: nil

Returns:

  • (Symbol)

    The signal name



152
153
154
155
156
157
# File 'lib/qml/reactive/object.rb', line 152

def signal(name, params, opts = {})
  name.to_sym.tap do |name|
    params = params.map(&:to_sym)
    add_signal(UnboundSignal.new(name, params, false, self, opts[:signal]))
  end
end

#variadic_signal(name, opts = {}) ⇒ Object

Defines a variadic signal. Variadic signals do not restrict the number of arguments.

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :signal (Proc) — default: nil

See Also:



164
165
166
167
168
# File 'lib/qml/reactive/object.rb', line 164

def variadic_signal(name, opts = {})
  name.to_sym.tap do |name|
    add_signal(UnboundSignal.new(name, nil, true, self, opts[:signal]))
  end
end