Module: KO::Properties

Included in:
Object
Defined in:
lib/ko/properties.rb

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



42
43
44
# File 'lib/ko/properties.rb', line 42

def self.extended(base)
  base.include(InstanceMethods)
end

Instance Method Details

#property(name, type, value: nil, on_change: nil) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity

Raises:

  • (TypeError)


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

def property(name, type, value: nil, on_change: nil) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity
  types = [type].flatten
  value ||= type.new if type.is_a?(Class)

  raise TypeError if types.none? { value.is_a?(_1) }

  signal :"#{name}_changed", type

  define_method(name) do
    return properties[name] if properties.key?(name)

    properties[name] = value
  end

  define_method("#{name}=") do |new_value|
    raise TypeError if types.none? { value.is_a?(_1) }

    return new_value if new_value == properties[name]

    properties[name] = new_value
    send(on_change) if on_change
    signals[:"#{name}_changed"].emit(new_value)
    new_value
  end
end