Class: Named
Class Method Summary collapse
- .arguments(names, options, *args) ⇒ Object
- .definition(method) ⇒ Object
- .normalize(*args) ⇒ Object
- .warn(value) ⇒ Object
- .warn? ⇒ Boolean
Instance Method Summary collapse
-
#initialize(name) ⇒ Named
constructor
A new instance of Named.
- #to_sym ⇒ Object
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, , *args) return args if (args.length != 1 || !args.first.is_a?(Hash)) || ([:rest] && !args.last.is_a?(Hash)) parameters = args.pop rest = args args = [] # fix alias parameters parameters.dup.each {|name, value| if [:alias].has_key?(name) parameters[[: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 - [:optional].keys).tap {|required| raise ArgumentError, "the following required parameters are missing: #{required.join(', ')}" unless required.empty? } unless [:optional] == true all_optional_after = names.length - names.reverse.take_while {|name| [: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 [: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 [:optional][name].nil? && index >= all_optional_after break end args << [: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 = [] = { :rest => [], :optional => [] } if method.respond_to? :parameters method.parameters.map {|how, name| if name names << name [:optional] << name if how == :opt [:rest] << name if how == :rest else names << rand.to_s [:rest] << names.last end } else if method.arity > 0 names.push(*(1 .. method.arity)) end end [names, ] 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) = Hash[ :optional => [], :alias => {}, :rest => [] ].merge(args.last.is_a?(Hash) ? args.pop : {}) method = args.shift.to_sym names = args [:optional] = Hash[if [:optional].is_a?(Range) names[[:optional]] elsif [:optional].is_a?(Hash) [[:optional]] else [:optional] end.map {|opt| if opt.is_a?(Hash) opt.to_a else [[opt, nil]] end }.flatten(1)] return method, names, end |
.warn(value) ⇒ Object
16 17 18 |
# File 'lib/call-me/named.rb', line 16 def self.warn (value) @warn = value end |
.warn? ⇒ Boolean
20 21 22 |
# File 'lib/call-me/named.rb', line 20 def self.warn? @warn end |
Instance Method Details
#to_sym ⇒ Object
28 29 30 |
# File 'lib/call-me/named.rb', line 28 def to_sym @name end |