Sha256: d233f257db4fc9af42a8ecd5251a783b8db0171bb858d26d8a17c53e996bbb02

Contents?: true

Size: 1.64 KB

Versions: 5

Compression:

Stored size: 1.64 KB

Contents

module SharingTags
  class Configuration

    NETWORKS = %i{ google facebook twitter }

    attr_accessor :running_context

    def initialize
      clear!
    end

    def context(name, &block)
      raise "please define context block params" unless block_given?
      (@contexts[name] ||= Context.new(name, self)).instance_exec(&block)
    end

    def switch_context(name = nil, *args)
      clean_params!
      @current_context_params = args
      @prev_context = current_context

      @current_context =
        if name
           @contexts[name]
        else
          default_context
        end
    end
    alias switch_context_to switch_context

    def clear!
      @contexts = {}
      @default_context = Context.new(:default, self)
      @current_context = nil
      @running_context = nil
      clean_params!
    end

    def params
      # @params ||= fetch_params
      @params = fetch_params
    end

    def within_context_params(running_context_instance)
      @running_context = running_context_instance
      params
    end

    def current_context
      @current_context || default_context
    end

    def clean_params!
      @params = nil
    end

    def clear_context!
      @current_context = nil
    end

    private

    def fetch_params
      default_context_params = default_context.params(@current_context_params)
      return default_context_params unless @current_context
      @current_context.params(@current_context_params, default_context_params)
    end

    def method_missing(method_name, *arguments, &block)
      current_context.send(method_name, *arguments, &block)
    end

    def default_context
      @default_context
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sharing_tags-0.0.9 lib/sharing_tags/configuration.rb
sharing_tags-0.0.8 lib/sharing_tags/configuration.rb
sharing_tags-0.0.7 lib/sharing_tags/configuration.rb
sharing_tags-0.0.6 lib/sharing_tags/configuration.rb
sharing_tags-0.0.5 lib/sharing_tags/configuration.rb