Sha256: ef855f647fdb3af861c4287857b6102705c07bc7d996d15e2443dac2eed11d14

Contents?: true

Size: 1.64 KB

Versions: 27

Compression:

Stored size: 1.64 KB

Contents

module FbGraph
  module Connections
    module Settings
      AVAILABLE_SETTINGS = [
        :users_can_post,
        :users_can_post_photos,
        :users_can_tag_photos,
        :users_can_post_videos
      ]

      def self.included(klass)
        AVAILABLE_SETTINGS.each do |setting|
          klass.class_eval <<-SETTING
            def #{setting}?(options = {})
              settings(options).include? :#{setting}
            end

            def #{setting}!(options = {})
              enable! :#{setting}, options
            end

            def #{setting.to_s.sub('can', 'cannot')}!(options = {})
              disable! :#{setting}, options
            end
          SETTING
        end
      end

      def settings(options = {})
        @settings = nil if options[:no_cache]
        @settings ||= self.connection(:settings, options).inject([]) do |_settings_, _setting_|
          _settings_ << _setting_[:setting].downcase.to_sym if _setting_[:value]
          _settings_
        end
      end

      def enable!(setting, options = {})
        __update_setting__ setting, true, options
      end

      def disable!(setting, options = {})
        __update_setting__ setting, false, options
      end

      private

      def __update_setting__(setting, value, options = {})
        succeeded = post options.merge(
          :setting => setting.to_s.upcase,
          :value => value,
          :connection => :settings
        )
        if succeeded
          if value
            @settings << setting.to_sym
          else
            @settings.delete_if { |key| key == setting.to_sym }
          end
        end
        succeeded
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
fb_graph-2.2.0 lib/fb_graph/connections/settings.rb
fb_graph-2.2.0.beta lib/fb_graph/connections/settings.rb
fb_graph-2.2.0.alpha2 lib/fb_graph/connections/settings.rb
fb_graph-2.2.0.alpha lib/fb_graph/connections/settings.rb
fb_graph-2.1.13 lib/fb_graph/connections/settings.rb
fb_graph-2.1.12 lib/fb_graph/connections/settings.rb
fb_graph-2.1.11 lib/fb_graph/connections/settings.rb
fb_graph-2.1.10 lib/fb_graph/connections/settings.rb
fb_graph-2.1.9 lib/fb_graph/connections/settings.rb
fb_graph-2.1.8 lib/fb_graph/connections/settings.rb
fb_graph-2.1.7 lib/fb_graph/connections/settings.rb
fb_graph-2.1.6 lib/fb_graph/connections/settings.rb
fb_graph-2.1.5 lib/fb_graph/connections/settings.rb
fb_graph-2.1.4 lib/fb_graph/connections/settings.rb
fb_graph-2.1.3 lib/fb_graph/connections/settings.rb
fb_graph-2.1.2 lib/fb_graph/connections/settings.rb
fb_graph-2.1.1 lib/fb_graph/connections/settings.rb
fb_graph-2.1.0 lib/fb_graph/connections/settings.rb
fb_graph-2.1.0.alpha lib/fb_graph/connections/settings.rb
fb_graph-2.0.2 lib/fb_graph/connections/settings.rb