Module: Ducktape::Bindable::ClassMethods

Defined in:
lib/ducktape/bindable.rb

Instance Method Summary collapse

Instance Method Details

#bindable(name, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ducktape/bindable.rb', line 18

def bindable(name, options = {})
  name = name.to_s
  options[:access] ||= :both
  m = BindableAttributeMetadata.new((name) || name, options)
  [name] = m
  raise InconsistentAccessorError.new(true, name)  if options[:access] == :writeonly && options[:getter]
  raise InconsistentAccessorError.new(false, name) if options[:access] == :readonly && options[:setter]

  define_method name, options[:getter] || ->() { get_value(name) } unless options[:access] == :writeonly

  unless options[:access] == :readonly
    define_method "#{name}=", options[:setter] || ->(value) { set_value(name, value) }
  end
  nil
end

#metadata(name) ⇒ Object

TODO improve metadata search



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

def (name)
  name = name.to_s
  m = [name]
  return m if m
  a = ancestors.find { |a| a != self && a.respond_to?(:metadata) }
  return nil unless a && (m = a.(name))
  m = m.dup
  [name] = m
end