Class: Configurable::Delegate

Inherits:
Object
  • Object
show all
Defined in:
lib/configurable/delegate.rb

Overview

Delegates are used by DelegateHash to determine how to map read/write operations to a receiver.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader, writer = "#{reader}=", default = nil, attributes = {}) ⇒ Delegate

Initializes a new Delegate with the specified key and default value.



32
33
34
35
36
37
38
# File 'lib/configurable/delegate.rb', line 32

def initialize(reader, writer="#{reader}=", default=nil, attributes={})
  @attributes = attributes
  
  self.default = default
  self.reader = reader
  self.writer = writer
end

Instance Attribute Details

#attributesObject (readonly)

An hash of metadata for self, used to present the delegate in different contexts (ex on the command line, in a web form, or a desktop app). Note that attributes should be set through []= and not through this reader.



29
30
31
# File 'lib/configurable/delegate.rb', line 29

def attributes
  @attributes
end

#readerObject

The reader method, by default key



20
21
22
# File 'lib/configurable/delegate.rb', line 20

def reader
  @reader
end

#writerObject

The writer method, by default key=



23
24
25
# File 'lib/configurable/delegate.rb', line 23

def writer
  @writer
end

Class Method Details

.duplicable_value?(value) ⇒ Boolean

Determines if the value is duplicable. Non-duplicable values include nil, true, false, Symbol, Numeric, and any object that does not respond to dup.

Returns:

  • (Boolean)


11
12
13
14
15
16
# File 'lib/configurable/delegate.rb', line 11

def duplicable_value?(value)
  case value
  when nil, true, false, Symbol, Numeric, Method then false
  else value.respond_to?(:dup)
  end
end

Instance Method Details

#==(another) ⇒ Object

True if another is a kind of Delegate with the same reader, writer, and default value. Attributes are not considered.



86
87
88
89
90
91
# File 'lib/configurable/delegate.rb', line 86

def ==(another)
  another.kind_of?(Delegate) &&
  self.reader == another.reader &&
  self.writer == another.writer &&
  self.default(false) == another.default(false)
end

#[](key, default = nil) ⇒ Object

Returns the value for the specified attribute, or default, if the attribute is unspecified.



48
49
50
# File 'lib/configurable/delegate.rb', line 48

def [](key, default=nil)
  attributes.has_key?(key) ? attributes[key] : default
end

#[]=(key, value) ⇒ Object

Sets the value of an attribute.



41
42
43
44
# File 'lib/configurable/delegate.rb', line 41

def []=(key, value)
  attributes[key] = value
  reset_duplicable if key == :duplicate_default
end

#default(duplicate = true) ⇒ Object

Returns the default value, or a duplicate of the default value if specified. The default value will not be duplicated unless duplicable (see Delegate.duplicable_value?). Duplication can also be turned off by specifying self = false.



62
63
64
# File 'lib/configurable/delegate.rb', line 62

def default(duplicate=true)
  duplicate && @duplicable ? @default.dup : @default
end

#default=(value) ⇒ Object

Sets the default value for self.



53
54
55
56
# File 'lib/configurable/delegate.rb', line 53

def default=(value)
  @default = value
  reset_duplicable
end

#is_nest?Boolean

Returns true if the default value is a kind of DelegateHash.

Returns:

  • (Boolean)


79
80
81
# File 'lib/configurable/delegate.rb', line 79

def is_nest?
  @default.kind_of?(DelegateHash)
end