Class: Filterameter::FilterDeclaration
- Inherits:
-
Object
- Object
- Filterameter::FilterDeclaration
- Defined in:
- lib/filterameter/filter_declaration.rb
Overview
# Filter Declaration
Class FilterDeclaration captures the filter declaration within the controller.
When the min_only or max_only range option is specified, in addition to the attribute filter which carries that option, the registry builds a duplicate declaration that also carries the range_type flag (as either :minimum or :maximum).
The predicate methods ‘min_only?` and `max_only?` answer what was declared; the predicate methods `minimum_range?` and `maximum_range?` answer what type of filter should be built.
Constant Summary collapse
- VALID_RANGE_OPTIONS =
[true, :min_only, :max_only].freeze
Instance Attribute Summary collapse
-
#association ⇒ Object
readonly
Returns the value of attribute association.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parameter_name ⇒ Object
readonly
Returns the value of attribute parameter_name.
-
#validations ⇒ Object
readonly
Returns the value of attribute validations.
Instance Method Summary collapse
-
#initialize(parameter_name, options, range_type: nil) ⇒ FilterDeclaration
constructor
A new instance of FilterDeclaration.
- #max_only? ⇒ Boolean
- #maximum_range? ⇒ Boolean
- #min_only? ⇒ Boolean
- #minimum_range? ⇒ Boolean
- #nested? ⇒ Boolean
- #partial_options ⇒ Object
- #partial_search? ⇒ Boolean
- #range? ⇒ Boolean
- #range_enabled? ⇒ Boolean
- #sortable? ⇒ Boolean
- #to_s ⇒ Object
- #validations? ⇒ Boolean
Constructor Details
#initialize(parameter_name, options, range_type: nil) ⇒ FilterDeclaration
Returns a new instance of FilterDeclaration.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/filterameter/filter_declaration.rb', line 21 def initialize(parameter_name, , range_type: nil) @parameter_name = parameter_name.to_s () @name = .fetch(:name, parameter_name).to_s @association = Array.wrap([:association]).presence @validations = Array.wrap([:validates]) @raw_partial_options = .fetch(:partial, false) @raw_range = [:range] @range_type = range_type @sortable = .fetch(:sortable, true) end |
Instance Attribute Details
#association ⇒ Object (readonly)
Returns the value of attribute association.
19 20 21 |
# File 'lib/filterameter/filter_declaration.rb', line 19 def association @association end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
19 20 21 |
# File 'lib/filterameter/filter_declaration.rb', line 19 def name @name end |
#parameter_name ⇒ Object (readonly)
Returns the value of attribute parameter_name.
19 20 21 |
# File 'lib/filterameter/filter_declaration.rb', line 19 def parameter_name @parameter_name end |
#validations ⇒ Object (readonly)
Returns the value of attribute validations.
19 20 21 |
# File 'lib/filterameter/filter_declaration.rb', line 19 def validations @validations end |
Instance Method Details
#max_only? ⇒ Boolean
62 63 64 |
# File 'lib/filterameter/filter_declaration.rb', line 62 def max_only? @raw_range == :max_only end |
#maximum_range? ⇒ Boolean
70 71 72 |
# File 'lib/filterameter/filter_declaration.rb', line 70 def maximum_range? @range_type == :maximum end |
#min_only? ⇒ Boolean
58 59 60 |
# File 'lib/filterameter/filter_declaration.rb', line 58 def min_only? @raw_range == :min_only end |
#minimum_range? ⇒ Boolean
66 67 68 |
# File 'lib/filterameter/filter_declaration.rb', line 66 def minimum_range? @range_type == :minimum end |
#nested? ⇒ Boolean
34 35 36 |
# File 'lib/filterameter/filter_declaration.rb', line 34 def nested? !@association.nil? end |
#partial_options ⇒ Object
46 47 48 |
# File 'lib/filterameter/filter_declaration.rb', line 46 def @partial_options ||= @raw_partial_options ? Options::PartialOptions.new(@raw_partial_options) : nil end |
#partial_search? ⇒ Boolean
42 43 44 |
# File 'lib/filterameter/filter_declaration.rb', line 42 def partial_search? .present? end |
#range? ⇒ Boolean
54 55 56 |
# File 'lib/filterameter/filter_declaration.rb', line 54 def range? @raw_range == true end |
#range_enabled? ⇒ Boolean
50 51 52 |
# File 'lib/filterameter/filter_declaration.rb', line 50 def range_enabled? @raw_range.present? end |
#sortable? ⇒ Boolean
74 75 76 |
# File 'lib/filterameter/filter_declaration.rb', line 74 def sortable? @sortable end |
#to_s ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/filterameter/filter_declaration.rb', line 78 def to_s = {} [:name] = ":#{@name}" if @parameter_name != @name [:association] = @association if nested? [:partial] = if (["filter :#{@parameter_name}"] + .map { |k, v| "#{k}: #{v}" }) .join(', ') end |
#validations? ⇒ Boolean
38 39 40 |
# File 'lib/filterameter/filter_declaration.rb', line 38 def validations? !@validations.empty? end |