Sha256: 3496e4fcc4a1646a596e26ff525b240f9fe44ef4ad77b96d75e0fd18edb4ee5e

Contents?: true

Size: 1.68 KB

Versions: 2

Compression:

Stored size: 1.68 KB

Contents

module Consul
  module Power

    def self.included(base)
      base.extend ActiveSupport::Memoizable
      base.extend ClassMethods
    end

    def include?(name, *args)
      args = args.dup
      record = args.shift
      power_value = send(name)
      if record.nil? || boolean_or_nil?(power_value)
        !!power_value
      else
        power_ids_name = self.class.power_ids_name(name)
        send(power_ids_name, *args).include?(record.id)
      end
    end

    def include!(*args)
      include?(*args) or raise Consul::Powerless.new("No power to #{args.inspect}")
    end

    private

    def boolean_or_nil?(value)
      [TrueClass, FalseClass, NilClass].include?(value.class)
    end

    module ClassMethods

      def power(name, &block)
        define_method(name, &block)
        define_method("#{name.to_s}?") { |*args| include?(name, *args) }
        define_method("#{name.to_s}!") { |*args| include!(name, *args) }
        define_method("#{name.to_s.singularize}?") { |*args| include?(name, *args) }
        define_method("#{name.to_s.singularize}!") { |*args| include!(name, *args) }
        ids_method = power_ids_name(name)
        define_method(ids_method) do |*args|
          scope = send(name, *args)
          scope = scope.scoped(:select => "`#{scope.table_name}`.`id`")
          query = if scope.respond_to?(:to_sql)
            scope.to_sql
          else
            scope.construct_finder_sql({})
          end
          ::ActiveRecord::Base.connection.select_values(query).collect(&:to_i)
        end
        memoize ids_method
        name
      end

      def power_ids_name(name)
        "#{name.to_s.singularize}_ids"
      end

      attr_accessor :current

    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
consul-0.3.0 lib/consul/power.rb
consul-0.2.3 lib/consul/power.rb