Class: FilterFactory::Filter

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion
Defined in:
lib/filter_factory/filter.rb

Defined Under Namespace

Classes: DuplicateFieldError

Constant Summary collapse

CONDITIONS =
[:eq, :ne, :lt, :lte, :gt, :gte, :all, :in, :nin, :regex, :exists, :presents].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFilter

Initializes new instance of Filter class.



14
15
16
# File 'lib/filter_factory/filter.rb', line 14

def initialize
  @fields = []
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



9
10
11
# File 'lib/filter_factory/filter.rb', line 9

def fields
  @fields
end

Class Method Details

.create(&block) ⇒ Filter

Creates a new filter instance and executes block on it

Parameters:

  • block (Proc)

    block to be executed in context of new filter instance

Returns:



73
74
75
# File 'lib/filter_factory/filter.rb', line 73

def create(&block)
  new.tap { |filter| filter.instance_eval(&block) }
end

Instance Method Details

#attributesHashWithIndifferentAccess

Returns list of filter attributes.

Returns:

  • (HashWithIndifferentAccess)


21
22
23
24
25
26
# File 'lib/filter_factory/filter.rb', line 21

def attributes
  fields.inject(HashWithIndifferentAccess.new) do |acc, field|
    acc[field.alias] = field.value
    acc
  end
end

#attributes=(attributes = {}) ⇒ Object

Assigns values to filter’s attributes.

Parameters:

  • attributes (Hash) (defaults to: {})

    hash of new attributes’ values



31
32
33
34
35
36
# File 'lib/filter_factory/filter.rb', line 31

def attributes=(attributes = {})
  return unless attributes
  attributes.each do |name, value|
    public_send("#{name}=", value)
  end
end

#filled_fieldsArray<Field>

Returns list of filled fields. Field is considered to be filled if it’s value is not nil and is not an empty string.

Returns:



42
43
44
# File 'lib/filter_factory/filter.rb', line 42

def filled_fields
  fields.select { |f| !f.value.nil? && f.value != '' }
end

#get_field(name) ⇒ Field?

Returns field by its name.

Parameters:

  • name (Symbol, String)

    name of the field

Returns:



50
51
52
# File 'lib/filter_factory/filter.rb', line 50

def get_field(name)
  fields.find { |f| f.name == name }
end

#persisted?Boolean

Required by ActiveModel. May be removed in newer Rails versions.

Returns:

  • (Boolean)


58
59
60
# File 'lib/filter_factory/filter.rb', line 58

def persisted?
  false
end