Module: BBAttributeFilters

Defined in:
lib/bbattribute_filters.rb,
lib/bbattribute_filters/attribute.rb,
lib/bbattribute_filters/conditions/if.rb,
lib/bbattribute_filters/handlers/proc.rb,
lib/bbattribute_filters/handlers/block.rb,
lib/bbattribute_filters/conditions/none.rb,
lib/bbattribute_filters/handlers/symbol.rb,
lib/bbattribute_filters/conditions/unless.rb,
lib/bbattribute_filters/builders/conditions.rb,
lib/bbattribute_filters/handlers/evaluation.rb,
lib/bbattribute_filters/conditions/condition.rb

Overview

The main BBAttributeFilters module. Should be included to

Defined Under Namespace

Modules: Builders, ClassMethods, Conditions, Handlers Classes: Attribute

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
# File 'lib/bbattribute_filters.rb', line 7

def self.included(base)
  base.extend ClassMethods
  base.class_eval do
    # Gets all of the class Attributes. Please note this is the internal attributes
    def filterable_attributes
      self.class._get_fiterable_attributes
    end

    # @abstract Subclass is expected to implement #evaluate_attribute
    # @!method evaluate_attribute(name)
    #   Evaluate the value of the attribute using the symbol provided

    # Gets all of the attributes as long as they pass the include? condition
    # check
    def all_attributes
      filterable_attributes.each_with_object({}) do |(key, attr), to_return|
        to_return[key] = attr.evaluate(self) if attr.include?(self)
      end
    end

    # Gets a single attribute via the name, this will run the include? check on
    # the attribute before evaulating it
    # @param name {Symbol} The name of the attribute to retrieve
    def attribute(name)
      attr = filterable_attributes[name]
      attr.evaluate(self) if attr&.include?(self)
    end

    # Gets a single attribute via the name but bypassed the included? check
    # @param name {Symbol} The name of the attribute to retrieve
    def attribute_forced(name)
      attr = filterable_attributes[name]
      attr ? attr.evaluate(self) : nil
    end

    # Gets a list of specific attributes based on the names give
    def only_attributes(*only)
      only.each_with_object({}) do |name, to_return|
        to_return[name] = attribute(name)
      end
    end
  end

  # Rather than wasting time with an if check each time we access the attributes hash we instead
  # set it up front upon include
  base._set_fiterable_attributes({})
end