Module: QuickAttr

Defined in:
lib/quick_attr.rb

Instance Method Summary collapse

Instance Method Details

#quick_array(*args) ⇒ Object



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

def quick_array (*args)
	quick_klass(Array,*args)
end

#quick_attr(*args) ⇒ Object

QUICK_ATTR_NIL = “QUICK_ATTR_NIL”.freeze



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/quick_attr.rb', line 3

def quick_attr (*args)
	args.each {|var|
		ivar = "@#{var}"
#	send :define_method, var, proc {|arg|
#		return send (:instance_variable_get, ivar) if arg.nil? 
#		send (:instance_variable_set, ivar, arg)
#		self
#		}
	class_eval "def #{var} (*arg,&block)
		raise \"cannot call #{var} with block and (#{args.join(',')}\" if arg.length > 0 and block
		arg[0] = block if block
		return #{ivar} if arg.length == 0
		#{ivar} = arg[0] if arg.length == 1
		#{ivar} = arg if arg.length > 1
		self
	end"

	#class_eval "def #{var} (arg=(QUICK_ATTR_NIL = \"\"))
	#	return #{ivar} if arg.object_id == QUICK_ATTR_NIL.object_id
	#	#{ivar} = arg
	#	self
	#end"
	}
end

#quick_hash(*args) ⇒ Object



51
52
53
# File 'lib/quick_attr.rb', line 51

def quick_hash (*args)
	quick_klass(Hash,*args)
end

#quick_klass(*args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/quick_attr.rb', line 28

def quick_klass (*args)
	unless args.first.is_a? Class then
	 raise "quick_klass's first argument must be a class, was #{args.first}, try Hash, or Array for example"
	end
	 klass = args.shift 
	args.each {|var|
		ivar = "@#{var}"
	class_eval "def #{var} (*arg)
	 	if arg.length == 0 then
			return #{ivar} = #{ivar} || #{klass.name}.new
		else
			#{ivar} = arg.is_a?(#{klass}) ? arg : #{klass}.new(arg)
			self
		end
	end"
	
	}

end