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



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

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

.included(base) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/ducktape/bindable.rb', line 64

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

#bind(attr_name, *args) ⇒ Object



78
79
80
# File 'lib/ducktape/bindable.rb', line 78

def bind(attr_name, *args)
  BindingSource.new(*args).tap { |source| send "#{attr_name}=", source }
end

#bindable_attr?(attr_name) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/ducktape/bindable.rb', line 82

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

#binding_source(attr_name) ⇒ Object



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

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

#clear_bindings(reset = true) ⇒ Object



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

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

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



96
97
98
99
100
# File 'lib/ducktape/bindable.rb', line 96

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



102
103
104
105
# File 'lib/ducktape/bindable.rb', line 102

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

#unhook_on_changed(attr_name, hook) ⇒ Object



107
108
109
110
111
# File 'lib/ducktape/bindable.rb', line 107

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