Sha256: cbe64a05f9a255bab404d7dfa677bfa566370207f13debad9019712043a5f21e

Contents?: true

Size: 1.01 KB

Versions: 3

Compression:

Stored size: 1.01 KB

Contents

require 'active_support/concern'
require 'active_support/core_ext/module/aliasing'

module StatelyScopes
  extend ActiveSupport::Concern

  included do
    class_eval do
      class << self
        alias_method_chain :scope, :state if StatelyScopes.configuration.alias_scope_method
      end
    end
  end

  def has_scoped_state?(name)
    self.class.send(name.to_sym).exists?(self.id)
  end

  module ClassMethods
    def scope_with_state(name, body, &block)
      if StatelyScopes.configuration.alias_scope_method
        scope_without_state name, body, &block
      else
        scope name, body, &block
      end
      class_eval "def #{name}?() self.has_scoped_state?(:#{name}) end"
    end
  end

  class << self
    def configure(&block)
      yield(StatelyScopes::Configuration.configuration)
    end

    def configuration
      StatelyScopes::Configuration.configuration
    end
  end

  class Configuration
    def self.configuration
      @@configuration ||= OpenStruct.new(:alias_scope_method => true)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
stately_scopes-0.0.4 lib/stately_scopes.rb
stately_scopes-0.0.3 lib/stately_scopes.rb
stately_scopes-0.0.2 lib/stately_scopes.rb