Class: Scopable

Inherits:
Object
  • Object
show all
Defined in:
lib/scopable.rb,
lib/scopable/version.rb

Constant Summary collapse

VERSION =
'2.1.1'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model = nil, scopes = nil) ⇒ Scopable

Returns a new instance of Scopable.



7
8
9
10
# File 'lib/scopable.rb', line 7

def initialize(model = nil, scopes = nil)
  @model = model || self.class.model
  @scopes = scopes || self.class.scopes
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/scopable.rb', line 5

def model
  @model
end

#scopesObject (readonly)

Returns the value of attribute scopes.



5
6
7
# File 'lib/scopable.rb', line 5

def scopes
  @scopes
end

Class Method Details

.model(model = nil) ⇒ Object



87
88
89
# File 'lib/scopable.rb', line 87

def self.model(model = nil)
  @model ||= model
end

.resolve(params = {}) ⇒ Object



83
84
85
# File 'lib/scopable.rb', line 83

def self.resolve(params = {})
  new.resolve(params)
end

.scope(name, options = {}, &block) ⇒ Object



95
96
97
# File 'lib/scopable.rb', line 95

def self.scope(name, options = {}, &block)
  scopes.store name, options.merge(block: block)
end

.scopesObject



91
92
93
# File 'lib/scopable.rb', line 91

def self.scopes
  @scopes ||= {}
end

Instance Method Details

#delegator(relation, value, params) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/scopable.rb', line 12

def delegator(relation, value, params)
  SimpleDelegator.new(relation).tap do |delegator|
    delegator.define_singleton_method(:value) do
      value
    end
    delegator.define_singleton_method(:params) do
      params
    end
  end
end

#resolve(params = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/scopable.rb', line 23

def resolve(params = {})
  params = params.with_indifferent_access

  scopes.reduce(model.all) do |relation, scope|
    name, options = *scope

    # Resolve param name.
    param = options[:param] || name

    # Resolve a value for the scope.
    value = options[:value] || params[param] || options[:default]

    # When value is empty treat it as nil.
    value = nil if value.respond_to?(:empty?) && value.empty?

    # When a nil value was given either skip the scope or bail with #none (if the required options was used).
    if value.nil?
      if options[:required]
        break relation.none
      else
        next relation
      end
    end

    # Cast boolean-like strings.
    case value.to_s
    when /\A(false|no|off)\z/
      value = false
    when /\A(true|yes|on)\z/
      value = true
    end

    # Enforce 'if' option.
    if options[:if]
      unless delegator(relation, value, params).instance_exec(&options[:if])
        next relation
      end
    end

    # Enforce 'unless' option.
    if options[:unless]
      if delegator(relation, value, params).instance_exec(&options[:unless])
        next relation
      end
    end

    # Bail if the value is false or nil.
    next relation unless value

    # When a block is present, use that, otherwise call the scope method.
    if options[:block].present?
      delegator(relation, value, params).instance_exec(&options[:block])
    elsif value == true
      relation.send(name)
    else
      relation.send(name, value)
    end
  end
end