Class: Ransack::Predicate
- Inherits:
-
Object
- Object
- Ransack::Predicate
- Defined in:
- lib/ransack/predicate.rb
Instance Attribute Summary collapse
-
#arel_predicate ⇒ Object
readonly
Returns the value of attribute arel_predicate.
-
#compound ⇒ Object
readonly
Returns the value of attribute compound.
-
#formatter ⇒ Object
readonly
Returns the value of attribute formatter.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#validator ⇒ Object
readonly
Returns the value of attribute validator.
-
#wants_array ⇒ Object
readonly
Returns the value of attribute wants_array.
Class Method Summary collapse
- .detect_and_strip_from_string!(str) ⇒ Object
- .detect_from_string(str) ⇒ Object
- .named(name) ⇒ Object
- .names ⇒ Object
- .names_by_decreasing_length ⇒ Object
Instance Method Summary collapse
- #eql?(other) ⇒ Boolean (also: #==)
- #format(val) ⇒ Object
- #hash ⇒ Object
-
#initialize(opts = {}) ⇒ Predicate
constructor
A new instance of Predicate.
- #validate(vals, type = @type) ⇒ Object
Constructor Details
permalink #initialize(opts = {}) ⇒ Predicate
Returns a new instance of Predicate.
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ransack/predicate.rb', line 43 def initialize(opts = {}) @name = opts[:name] @arel_predicate = opts[:arel_predicate] @type = opts[:type] @formatter = opts[:formatter] @validator = opts[:validator] || lambda { |v| v.respond_to?(:empty?) ? !v.empty? : !v.nil? } @compound = opts[:compound] @wants_array = opts.fetch(:wants_array, @compound || Constants::IN_NOT_IN.include?(@arel_predicate)) end |
Instance Attribute Details
permalink #arel_predicate ⇒ Object (readonly)
Returns the value of attribute arel_predicate.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def arel_predicate @arel_predicate end |
permalink #compound ⇒ Object (readonly)
Returns the value of attribute compound.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def compound @compound end |
permalink #formatter ⇒ Object (readonly)
Returns the value of attribute formatter.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def formatter @formatter end |
permalink #name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def name @name end |
permalink #type ⇒ Object (readonly)
Returns the value of attribute type.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def type @type end |
permalink #validator ⇒ Object (readonly)
Returns the value of attribute validator.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def validator @validator end |
permalink #wants_array ⇒ Object (readonly)
Returns the value of attribute wants_array.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def wants_array @wants_array end |
Class Method Details
permalink .detect_and_strip_from_string!(str) ⇒ Object
[View source]
20 21 22 23 24 25 |
# File 'lib/ransack/predicate.rb', line 20 def detect_and_strip_from_string!(str) if p = detect_from_string(str) str.sub! /_#{p}$/, Constants::EMPTY p end end |
permalink .detect_from_string(str) ⇒ Object
[View source]
27 28 29 |
# File 'lib/ransack/predicate.rb', line 27 def detect_from_string(str) names_by_decreasing_length.detect { |p| str.end_with?("_#{p}") } end |
permalink .named(name) ⇒ Object
[View source]
16 17 18 |
# File 'lib/ransack/predicate.rb', line 16 def named(name) Ransack.predicates[name.to_s] end |
permalink .names ⇒ Object
[View source]
8 9 10 |
# File 'lib/ransack/predicate.rb', line 8 def names Ransack.predicates.keys end |
permalink .names_by_decreasing_length ⇒ Object
[View source]
12 13 14 |
# File 'lib/ransack/predicate.rb', line 12 def names_by_decreasing_length names.sort { |a,b| b.length <=> a.length } end |
Instance Method Details
permalink #eql?(other) ⇒ Boolean Also known as: ==
55 56 57 58 |
# File 'lib/ransack/predicate.rb', line 55 def eql?(other) self.class == other.class && self.name == other.name end |
permalink #format(val) ⇒ Object
[View source]
65 66 67 68 69 70 71 |
# File 'lib/ransack/predicate.rb', line 65 def format(val) if formatter formatter.call(val) else val end end |
permalink #hash ⇒ Object
[View source]
61 62 63 |
# File 'lib/ransack/predicate.rb', line 61 def hash name.hash end |
permalink #validate(vals, type = @type) ⇒ Object
[View source]
73 74 75 |
# File 'lib/ransack/predicate.rb', line 73 def validate(vals, type = @type) vals.any? { |v| validator.call(type ? v.cast(type) : v.value) } end |