Class: Ducktape::BindableAttribute

Inherits:
Object
  • Object
show all
Includes:
Hookable
Defined in:
lib/ducktape/bindable_attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hookable

extended, included

Constructor Details

#initialize(owner, name) ⇒ BindableAttribute

:source_link # Link - link between source and target



18
19
20
21
# File 'lib/ducktape/bindable_attribute.rb', line 18

def initialize(owner, name)
  @owner, @name, @source_link = owner, name.to_s, nil
  reset_value
end

Instance Attribute Details

#nameObject (readonly)

Bindable



13
14
15
# File 'lib/ducktape/bindable_attribute.rb', line 13

def name
  @name
end

#ownerObject (readonly)

Bindable



13
14
15
# File 'lib/ducktape/bindable_attribute.rb', line 13

def owner
  @owner
end

#valueObject (readonly)

Bindable



13
14
15
# File 'lib/ducktape/bindable_attribute.rb', line 13

def value
  @value
end

Instance Method Details

#binding_sourceObject



23
24
25
# File 'lib/ducktape/bindable_attribute.rb', line 23

def binding_source
  @source_link.binding_source if @source_link
end

#has_source?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/ducktape/bindable_attribute.rb', line 27

def has_source?
  !!@source_link
end

#metadataObject



31
32
33
# File 'lib/ducktape/bindable_attribute.rb', line 31

def 
  @owner.send(:metadata, @name)
end

#remove_source(reset = true) ⇒ Object

After unbinding the source the value can be reset, or kept. The default is to reset the target’s (self) value.



37
38
39
40
41
42
43
# File 'lib/ducktape/bindable_attribute.rb', line 37

def remove_source(reset = true)
  return unless @source_link
  src, @source_link = @source_link, nil
  src.unbind
  reset_value if reset
  src.binding_source
end

#reset_valueObject



45
46
47
# File 'lib/ducktape/bindable_attribute.rb', line 45

def reset_value
  set_value .default
end

#set_value(value) {|@value| ... } ⇒ Object

Yields:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ducktape/bindable_attribute.rb', line 49

def set_value(value)
  if value.is_a?(BindingSource) #attach new binding source
    replace_source value
    return @value unless @source_link.forward? #value didn't change
    value = @source_link.source_value
  end

  original_value, value = value, transform_value(value)

  return @value if value.equal?(@value) || value == @value # transformed value is the same?

  #set effective value
  old_value, @original_value, @value = @value, original_value, value
  yield @value if block_given?
  call_hooks :on_changed, owner, attribute: name.dup, value: @value, old_value: old_value

  @source_link.update_source if @source_link && @source_link.reverse?

  @value
end

#to_sObject



70
71
72
# File 'lib/ducktape/bindable_attribute.rb', line 70

def to_s
  "#<#{self.class}:0x#{object_id.to_s(16)} @name=#{name}>"
end