Class: Whiteprint::AttributeScope

Inherits:
Object
  • Object
show all
Defined in:
lib/whiteprint/attributes.rb

Instance Method Summary collapse

Constructor Details

#initialize(scope, model: nil) ⇒ AttributeScope

Returns a new instance of AttributeScope.



75
76
77
78
79
80
# File 'lib/whiteprint/attributes.rb', line 75

def initialize(scope, model: nil)
  @scope   = scope
  @model   = model
  @selects = []
  @rejects = []
end

Instance Method Details

#filterObject



96
97
98
99
100
101
102
# File 'lib/whiteprint/attributes.rb', line 96

def filter
  select = proc { |scope, condition| scope.select(&condition) }
  reject = proc { |scope, condition| scope.reject(&condition) }

  scope = @selects.inject(@scope, &select)
  @rejects.inject(scope, &reject)
end

#not(*keys, **conditions) ⇒ Object



89
90
91
92
93
94
# File 'lib/whiteprint/attributes.rb', line 89

def not(*keys, **conditions)
  @rejects << proc do |_, attribute|
    attribute.has?(*keys, **conditions)
  end
  Attributes.new(filter, model: @model)
end

#where(*keys, **conditions) ⇒ Object



82
83
84
85
86
87
# File 'lib/whiteprint/attributes.rb', line 82

def where(*keys, **conditions)
  @selects << proc do |_, attribute|
    attribute.has?(*keys, **conditions)
  end
  Attributes.new(filter, model: @model)
end