Module: Ducktape::Bindable

Defined in:
lib/ducktape/bindable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(_) ⇒ Object



61
62
63
# File 'lib/ducktape/bindable.rb', line 61

def self.extended(_)
  raise 'Cannot extend, only include.'
end

.included(base) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/ducktape/bindable.rb', line 51

def self.included(base)
  base.extend(ClassMethods)
  return unless base.is_a?(Module)
  included = base.respond_to?(:included) && base.method(:included)
  base.define_singleton_method(:included, ->(c) do
    included.(c) if included
    c.extend(ClassMethods)
  end)
end

Instance Method Details

#bindable_attr?(attr_name) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/ducktape/bindable.rb', line 65

def bindable_attr?(attr_name)
  !!(attr_name)
end

#binding_source(attr_name) ⇒ Object



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

def binding_source(attr_name)
  return unless bindable_attr?(attr_name)
  get_bindable_attr(attr_name).binding_source
end

#clear_bindings(reset = true) ⇒ Object



74
75
76
77
# File 'lib/ducktape/bindable.rb', line 74

def clear_bindings(reset = true)
  bindable_attrs.each { |_, attr| attr.remove_source(reset) }
  nil
end

#on_changed(attr_name, hook = nil, &block) ⇒ Object



79
80
81
82
83
# File 'lib/ducktape/bindable.rb', line 79

def on_changed(attr_name, hook = nil, &block)
  return nil unless block || hook
  get_bindable_attr(attr_name).on_changed(hook, &block)
  hook || block
end

#unbind_source(attr_name) ⇒ Object



85
86
87
88
# File 'lib/ducktape/bindable.rb', line 85

def unbind_source(attr_name)
  get_bindable_attr(attr_name).remove_source
  nil
end

#unhook_on_changed(attr_name, hook) ⇒ Object



90
91
92
93
94
# File 'lib/ducktape/bindable.rb', line 90

def unhook_on_changed(attr_name, hook)
  return nil unless hook
  get_bindable_attr(attr_name).remove_hook(:on_changed, hook)
  hook
end