Module: Attributes
- Defined in:
- lib/filebase/attributes.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
12
13
14
15
16
17
18
|
# File 'lib/filebase/attributes.rb', line 12
def method_missing(name,*args)
case args.length
when 0 then get( name.to_s )
when 1 then set( name.to_s[0..-2], args[0] )
else raise ArgumentError.new("#{args.length} for 0 or 1")
end
end
|
Instance Method Details
#[](name) ⇒ Object
20
|
# File 'lib/filebase/attributes.rb', line 20
def [](name) ; get(name) ; end
|
#[]=(name, val) ⇒ Object
22
|
# File 'lib/filebase/attributes.rb', line 22
def []=(name,val) ; set(name,val) ; end
|
#attributes ⇒ Object
10
|
# File 'lib/filebase/attributes.rb', line 10
def attributes ; @attrs ||= {} ; end
|
#attributes=(hash) ⇒ Object
5
6
7
8
|
# File 'lib/filebase/attributes.rb', line 5
def attributes=( hash )
@attrs = hash.inject({}) { |h,p| k,v = p; h[k.to_s] = v; h }
end
|
#get(name) ⇒ Object
26
27
28
|
# File 'lib/filebase/attributes.rb', line 26
def get(name)
( ( rval = attributes[name.to_s] ).is_a?( Hash ) and self.new( rval ) ) or rval
end
|
#initialize(hash = {}) ⇒ Object
3
|
# File 'lib/filebase/attributes.rb', line 3
def initialize( hash = {} ) ; self.attributes = hash ; end
|
#set(name, val) ⇒ Object
24
|
# File 'lib/filebase/attributes.rb', line 24
def set(name, val) ; attributes[name.to_s] = val ; end
|
#to_h ⇒ Object
Also known as:
to_hash
30
|
# File 'lib/filebase/attributes.rb', line 30
def to_h ; @attrs ; end
|