Class: FilterFactory::Filter
- Inherits:
-
Object
- Object
- FilterFactory::Filter
- 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
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
Class Method Summary collapse
-
.create(&block) ⇒ Filter
Creates a new filter instance and executes block on it.
Instance Method Summary collapse
-
#attributes ⇒ HashWithIndifferentAccess
Returns list of filter attributes.
-
#attributes=(attributes = {}) ⇒ Object
Assigns values to filter’s attributes.
-
#filled_fields ⇒ Array<Field>
Returns list of filled fields.
-
#get_field(name) ⇒ Field?
Returns field by its name.
-
#initialize ⇒ Filter
constructor
Initializes new instance of Filter class.
-
#persisted? ⇒ Boolean
Required by ActiveModel.
Constructor Details
#initialize ⇒ Filter
Initializes new instance of Filter class.
14 15 16 |
# File 'lib/filter_factory/filter.rb', line 14 def initialize @fields = [] end |
Instance Attribute Details
#fields ⇒ Object (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
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
#attributes ⇒ HashWithIndifferentAccess
Returns list of filter attributes.
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.
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_fields ⇒ Array<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.
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.
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.
58 59 60 |
# File 'lib/filter_factory/filter.rb', line 58 def persisted? false end |