lib/configurable/delegate.rb in configurable-0.1.0 vs lib/configurable/delegate.rb in configurable-0.3.0
- old
+ new
@@ -20,13 +20,13 @@
attr_reader :reader
# The writer method, by default key=
attr_reader :writer
- # An array of metadata for self, used to present the
+ # An hash of metadata for self, used to present the
# delegate in different contexts (ex on the command
- # line or web).
+ # line, in a web form, or a desktop app).
attr_reader :attributes
# Initializes a new Delegate with the specified key
# and default value.
def initialize(reader, writer="#{reader}=", default=nil, attributes={})
@@ -34,10 +34,21 @@
self.reader = reader
self.writer = writer
@attributes = attributes
end
+
+ # Sets the value of an attribute.
+ def []=(key, value)
+ attributes[key] = value
+ end
+
+ # Returns the value for the specified attribute, or
+ # default, if the attribute is unspecified.
+ def [](key, default=nil)
+ attributes.has_key?(key) ? attributes[key] : default
+ end
# Sets the default value for self.
def default=(value)
@duplicable = Delegate.duplicable_value?(value)
@default = value.freeze
@@ -58,9 +69,14 @@
# Sets the writer for self. The writer is symbolized,
# but may also be set to nil.
def writer=(value)
@writer = value == nil ? value : value.to_sym
+ end
+
+ # Returns true if the default value is a kind of DelegateHash.
+ def is_nest?
+ @default.kind_of?(DelegateHash)
end
# True if another is a kind of Delegate with the same
# reader, writer, and default value. Attributes are
# not considered.
\ No newline at end of file