Class: Lastfm::Util
- Inherits:
-
Object
- Object
- Lastfm::Util
- Defined in:
- lib/lastfm/util.rb
Class Method Summary collapse
- .build_options(args, mandatory, optional) ⇒ Object
- .build_options_from_array(values, mandatory, optional) ⇒ Object
- .build_options_from_hash(options, mandatory, optional) ⇒ Object
- .force_array(array_or_something) ⇒ Object
Class Method Details
.build_options(args, mandatory, optional) ⇒ Object
7 8 9 10 11 12 13 |
# File 'lib/lastfm/util.rb', line 7 def self.(args, mandatory, optional) if args.first.is_a?(Hash) (args.first, mandatory || [], optional || []) else (args, mandatory || [], optional || []) end end |
.build_options_from_array(values, mandatory, optional) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lastfm/util.rb', line 33 def self.(values, mandatory, optional) = {} mandatory.each_with_index do |name, index| raise ArgumentError.new('%s is required' % name) unless values[index] [name] = values[index] end optional.each_with_index do |name, index| value = name[1] if value.kind_of?(Proc) value = value.call end [name[0]] = values[index + mandatory.size] || value end end |
.build_options_from_hash(options, mandatory, optional) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/lastfm/util.rb', line 15 def self.(, mandatory, optional) candidates = mandatory.kind_of?(AnyParams) ? mandatory.candidates : [mandatory] candidates.each do |params| missing_param = Array(params).any? { |param| !.key?(param) } if missing_param && params.equal?(candidates.last) raise ArgumentError.new("%s is required" % candidates.map{ |c| Array(c).join(', ')}.join(' or ')) end break unless missing_param end optional.each do |name, value| [name] ||= value.kind_of?(Proc) ? value.call : value end end |
.force_array(array_or_something) ⇒ Object
3 4 5 |
# File 'lib/lastfm/util.rb', line 3 def self.force_array(array_or_something) array_or_something.kind_of?(Array) ? array_or_something : [array_or_something].compact end |