Class: Named

Inherits:
Object show all
Defined in:
lib/call-me/named.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Named

Returns a new instance of Named.



24
25
26
# File 'lib/call-me/named.rb', line 24

def initialize (name)
	@name = name.to_sym
end

Class Method Details

.arguments(names, options, *args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/call-me/named.rb', line 59

def self.arguments (names, options, *args)
	return args if (args.length != 1 || !args.first.is_a?(Hash)) || (options[:rest] && !args.last.is_a?(Hash))

	parameters = args.pop
	rest       = args
	args       = []

	# fix alias parameters
	parameters.dup.each {|name, value|
		if options[:alias].has_key?(name)
			parameters[options[:alias][name]] = value
			parameters.delete(name)
		elsif name.is_a?(Integer) && !parameters[names[name - 1]].is_a?(Integer)
			parameters[names[name - 1]] = value
			parameters.delete(name)
		end
	}

	# check if there are unknown parameters
	parameters.keys.each {|parameter|
		raise ArgumentError, "#{parameter} is an unknown parameter" unless names.member?(parameter)
	}

	# check for missing required parameters
	(names - parameters.keys - options[:optional].keys).tap {|required|
		raise ArgumentError, "the following required parameters are missing: #{required.join(', ')}" unless required.empty?
	} unless options[:optional] == true

	all_optional_after = names.length - names.reverse.take_while {|name|
		options[:optional].has_key?(name) && !parameters.has_key?(name)
	}.length

	# fill the arguments array
	# TODO: try to not add nil for the last optional parameters
	names.each_with_index {|name, index|
		if parameters.has_key?(name)
			if options[:rest].member?(name)
				args.push(*parameters[name])
			else
				args << parameters[name]
			end
		else
			if index < all_optional_after
				warn 'keep in mind that optionals between two arguments will have nil as value' if Named.warn?
			end

			if options[:optional][name].nil? && index >= all_optional_after
				break
			end

			args << options[:optional][name]
		end
	}

	args
end

.definition(method) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/call-me/named.rb', line 116

def self.definition (method)
	names   = []
	options = { :rest => [], :optional => [] }

	if method.respond_to? :parameters
		method.parameters.map {|how, name|
			if name
				names << name
				
				options[:optional] << name if how == :opt
				options[:rest]     << name if how == :rest
			else
				names          << rand.to_s
				options[:rest] << names.last
			end
		}
	else
		if method.arity > 0
			names.push(*(1 .. method.arity))
		end
	end

	[names, options]
end

.normalize(*args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/call-me/named.rb', line 32

def self.normalize (*args)
	options = Hash[
		:optional => [],
		:alias    => {},
		:rest     => []
	].merge(args.last.is_a?(Hash) ? args.pop : {})

	method = args.shift.to_sym
	names  = args

	options[:optional] = Hash[if options[:optional].is_a?(Range)
		names[options[:optional]]
	elsif options[:optional].is_a?(Hash)
		[options[:optional]]
	else
		options[:optional]
	end.map {|opt|
		if opt.is_a?(Hash)
			opt.to_a
		else
			[[opt, nil]]
		end
	}.flatten(1)]

	return method, names, options
end

.warn(value) ⇒ Object



16
17
18
# File 'lib/call-me/named.rb', line 16

def self.warn (value)
	@warn = value
end

.warn?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/call-me/named.rb', line 20

def self.warn?
	@warn
end

Instance Method Details

#to_symObject



28
29
30
# File 'lib/call-me/named.rb', line 28

def to_sym
	@name
end