Sha256: 50387345ba0f70a63a60c5c967a53e46e55914bf92ab2559ada3b7bd6674ed0f
Contents?: true
Size: 1.63 KB
Versions: 1
Compression:
Stored size: 1.63 KB
Contents
module VkontakteApi # A mixin for classes that will resolve other classes' objects via `#method_missing`. module Resolver # Main methods dispatch. # # If the called method is a namespace, it creates and returns a new `VkontakteApi::Namespace` instance. # Otherwise it creates a `VkontakteApi::Method` instance and invokes it's `#call` method passing it the arguments and a block. def method_missing(method_name, *args, &block) method_name = method_name.to_s if Resolver.namespaces.include?(method_name) # called from Client Namespace.new(method_name, resolver: resolver) else # called from Namespace or one-level method Method.new(method_name, resolver: resolver).call(args.first || {}, &block) end end # A `Hashie::Mash` structure holding the name and token of current instance. # @return [Hashie::Mash] def resolver @resolver ||= Hashie::Mash.new(name: @name, token: token) end class << self # An array of method namespaces. # Lazily loads the list from `namespaces.yml` and caches it. # @return [Array] def namespaces if @namespaces.nil? filename = File.expand_path('../namespaces.yml', __FILE__) @namespaces = YAML.load_file(filename) end @namespaces end # When this module is included, it undefines the `:send` instance method in the `base_class` # so it can be resolved via `method_missing`. def included(base_class) base_class.class_eval do undef_method :send end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
vkontakte_api-1.2 | lib/vkontakte_api/resolver.rb |