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



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/filebase/attributes.rb', line 16

def method_missing(name,*args)
  name = name.to_s
  case args.length
   when 0 then get( name )
   when 1
     if name[-1,1] == '=' 
       set( name[0..-2], args[0] )
     else
       super
     end
   else super
  end
end

Instance Method Details

#[](name) ⇒ Object



30
# File 'lib/filebase/attributes.rb', line 30

def [](name) ; get(name) ; end

#[]=(name, val) ⇒ Object



32
# File 'lib/filebase/attributes.rb', line 32

def []=(name,val) ; set(name,val) ; end

#attributesObject



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 )
  # copy the hash, converting all keys to strings
  @attrs = hash.inject({}) { |h,p| k,v = p; h[k.to_s] = v; h  }
end

#delete(key) ⇒ Object



14
# File 'lib/filebase/attributes.rb', line 14

def delete( key ) ; attributes.delete( key.to_s ) ; end

#get(name) ⇒ Object



36
37
38
# File 'lib/filebase/attributes.rb', line 36

def get(name)
  ( ( rval = attributes[name.to_s] ).is_a?( Hash ) and attributes[name.to_s] = self.class.new( rval ) ) or rval
end

#has_key?(key) ⇒ Boolean



12
# File 'lib/filebase/attributes.rb', line 12

def has_key?( key ) ; attributes.has_key?( key.to_s ) ; end

#initialize(hash = {}) ⇒ Object



3
# File 'lib/filebase/attributes.rb', line 3

def initialize( hash = {} ) ; self.attributes = hash ; end

#set(name, val) ⇒ Object



34
# File 'lib/filebase/attributes.rb', line 34

def set(name, val) ; attributes[name.to_s] = val ; end

#to_hObject Also known as: to_hash



40
# File 'lib/filebase/attributes.rb', line 40

def to_h ; @attrs ; end