Sha256: 15a6c0d12c8c50facf3bac891f5ea6d563f5d96c3749d895c02d37ada5de7f05

Contents?: true

Size: 1.22 KB

Versions: 14

Compression:

Stored size: 1.22 KB

Contents

class Lastfm
  class Util
    def self.force_array(array_or_something)
      array_or_something.kind_of?(Array) ? array_or_something : [array_or_something]
    end

    def self.build_options(args, mandatory, optional)
      if args.first.is_a?(Hash)
        build_options_from_hash(args.first, mandatory, optional)
      else
        build_options_from_array(args, mandatory, optional)
      end
    end

    def self.build_options_from_hash(options, mandatory, optional)
      mandatory.each do |name|
        raise ArgumentError.new("%s is required" % name) unless options[name]
      end

      optional.each do |name, value|
        options[name] ||= value.kind_of?(Proc) ? value.call : value
      end

      options
    end

    def self.build_options_from_array(values, mandatory, optional)
      options = {}

      mandatory.each_with_index do |name, index|
        raise ArgumentError.new('%s is required' % name) unless values[index]
        options[name] = values[index]
      end

      optional.each_with_index do |name, index|
        value = name[1]
        if value.kind_of?(Proc)
          value = value.call
        end
        options[name[0]] = values[index + mandatory.size] || value
      end

      options
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
lastfm-1.17.0 lib/lastfm/util.rb
lastfm-1.16.0 lib/lastfm/util.rb
lastfm-1.15.1 lib/lastfm/util.rb
lastfm-1.15.0 lib/lastfm/util.rb
lastfm-1.14.1 lib/lastfm/util.rb
lastfm-1.14.0 lib/lastfm/util.rb
lastfm-1.13.1 lib/lastfm/util.rb
lastfm-1.13.0 lib/lastfm/util.rb
lastfm-1.12.0 lib/lastfm/util.rb
lastfm-1.11.0 lib/lastfm/util.rb
lastfm-1.10.0 lib/lastfm/util.rb
lastfm-1.9.0 lib/lastfm/util.rb
lastfm-1.8.0 lib/lastfm/util.rb
lastfm-1.7.0 lib/lastfm/util.rb