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
- .for_attribute_name(attribute_name) ⇒ Object
- .name_from_attribute_name(attribute_name) ⇒ 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
#initialize(opts = {}) ⇒ Predicate
Returns a new instance of Predicate.
37 38 39 40 41 42 43 44 45 |
# File 'lib/ransack/predicate.rb', line 37 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[:wants_array] == true || @compound || ['in', 'not_in'].include?(@arel_predicate) end |
Instance Attribute Details
#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 |
#compound ⇒ Object (readonly)
Returns the value of attribute compound.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def compound @compound end |
#formatter ⇒ Object (readonly)
Returns the value of attribute formatter.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def formatter @formatter end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def type @type end |
#validator ⇒ Object (readonly)
Returns the value of attribute validator.
3 4 5 |
# File 'lib/ransack/predicate.rb', line 3 def validator @validator end |
#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
.detect_and_strip_from_string!(str) ⇒ Object
19 20 21 |
# File 'lib/ransack/predicate.rb', line 19 def detect_and_strip_from_string!(str) names_by_decreasing_length.detect {|p| str.sub!(/_#{p}$/, '')} end |
.detect_from_string(str) ⇒ Object
23 24 25 |
# File 'lib/ransack/predicate.rb', line 23 def detect_from_string(str) names_by_decreasing_length.detect {|p| str.match(/_#{p}$/)} end |
.for_attribute_name(attribute_name) ⇒ Object
31 32 33 |
# File 'lib/ransack/predicate.rb', line 31 def for_attribute_name(attribute_name) self.named(detect_from_string(attribute_name.to_s)) end |
.name_from_attribute_name(attribute_name) ⇒ Object
27 28 29 |
# File 'lib/ransack/predicate.rb', line 27 def name_from_attribute_name(attribute_name) names_by_decreasing_length.detect {|p| attribute_name.to_s.match(/_#{p}$/)} end |
.named(name) ⇒ Object
15 16 17 |
# File 'lib/ransack/predicate.rb', line 15 def named(name) Ransack.predicates[name.to_s] end |
.names ⇒ Object
7 8 9 |
# File 'lib/ransack/predicate.rb', line 7 def names Ransack.predicates.keys end |
.names_by_decreasing_length ⇒ Object
11 12 13 |
# File 'lib/ransack/predicate.rb', line 11 def names_by_decreasing_length names.sort {|a,b| b.length <=> a.length} end |
Instance Method Details
#eql?(other) ⇒ Boolean Also known as: ==
47 48 49 50 |
# File 'lib/ransack/predicate.rb', line 47 def eql?(other) self.class == other.class && self.name == other.name end |
#format(val) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/ransack/predicate.rb', line 57 def format(val) if formatter formatter.call(val) else val end end |
#hash ⇒ Object
53 54 55 |
# File 'lib/ransack/predicate.rb', line 53 def hash name.hash end |
#validate(vals, type = @type) ⇒ Object
65 66 67 |
# File 'lib/ransack/predicate.rb', line 65 def validate(vals, type = @type) vals.select {|v| validator.call(type ? v.cast(type) : v.value)}.any? end |