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
# File 'lib/filebase/attributes.rb', line 16

def method_missing(name,*args)
  name = name.to_s
	if name =~/=$/ and args.length == 1
		set(name[0..-2], args[0] )
	else
		get(name)
	end
end

Instance Method Details

#[](name) ⇒ Object



25
26
27
# File 'lib/filebase/attributes.rb', line 25

def [](name)
	get(name)
end

#[]=(name, val) ⇒ Object



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

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

#attributesObject



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

def attributes
  @attrs ||= {}
end

#attributes=(hash) ⇒ Object



7
8
9
10
# File 'lib/filebase/attributes.rb', line 7

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

#get(name) ⇒ Object



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

def get(name)
	rval = attributes[name.to_s]
	rval.is_a?( Hash ) ? Attributes.new( rval ) : rval
end

#initialize(hash = {}) ⇒ Object



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

def initialize(hash = {} )
  attributes = hash
end

#set(name, val) ⇒ Object



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

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

#to_hObject Also known as: to_hash



42
43
44
# File 'lib/filebase/attributes.rb', line 42

def to_h
	@attrs
end