Class: SearchCopGrammar::Attributes::Collection
- Inherits:
-
Object
- Object
- SearchCopGrammar::Attributes::Collection
- Defined in:
- lib/search_cop_grammar/attributes.rb
Constant Summary collapse
- INCLUDED_OPERATORS =
[:matches, :eq, :not_eq, :gt, :gteq, :lt, :lteq].freeze
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#query_info ⇒ Object
readonly
Returns the value of attribute query_info.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #alias_for(name) ⇒ Object
- #attribute_for(attribute_definition) ⇒ Object
- #attributes ⇒ Object
- #compatible?(value) ⇒ Boolean
- #eql?(other) ⇒ Boolean
- #fulltext? ⇒ Boolean
- #generator(generator, value) ⇒ Object
- #generator_for(name) ⇒ Object
- #generators ⇒ Object
- #hash ⇒ Object
-
#initialize(query_info, key) ⇒ Collection
constructor
A new instance of Collection.
- #klass_for(name) ⇒ Object
- #klass_for_association(name) ⇒ Object
- #matches(value) ⇒ Object
- #options ⇒ Object
- #valid_operator?(operator) ⇒ Boolean
Constructor Details
#initialize(query_info, key) ⇒ Collection
Returns a new instance of Collection.
11 12 13 14 15 16 |
# File 'lib/search_cop_grammar/attributes.rb', line 11 def initialize(query_info, key) raise(SearchCop::UnknownColumn, "Unknown column #{key}") unless query_info.scope.reflection.attributes[key] @query_info = query_info @key = key end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/search_cop_grammar/attributes.rb', line 7 def key @key end |
#query_info ⇒ Object (readonly)
Returns the value of attribute query_info.
7 8 9 |
# File 'lib/search_cop_grammar/attributes.rb', line 7 def query_info @query_info end |
Instance Method Details
#==(other) ⇒ Object
22 23 24 |
# File 'lib/search_cop_grammar/attributes.rb', line 22 def ==(other) other.is_a?(self.class) && [query_info.model, key] == [query_info.model, other.key] end |
#alias_for(name) ⇒ Object
85 86 87 |
# File 'lib/search_cop_grammar/attributes.rb', line 85 def alias_for(name) (query_info.scope.reflection.aliases[name] && name) || klass_for(name).table_name end |
#attribute_for(attribute_definition) ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/search_cop_grammar/attributes.rb', line 89 def attribute_for(attribute_definition) query_info.references.push attribute_definition table, column = attribute_definition.split(".") klass = klass_for(table) raise(SearchCop::UnknownAttribute, "Unknown attribute #{attribute_definition}") unless klass.columns_hash[column] Attributes.const_get(klass.columns_hash[column].type.to_s.classify).new(klass, alias_for(table), column, ) end |
#attributes ⇒ Object
62 63 64 |
# File 'lib/search_cop_grammar/attributes.rb', line 62 def attributes @attributes ||= query_info.scope.reflection.attributes[key].collect { |attribute_definition| attribute_for attribute_definition } end |
#compatible?(value) ⇒ Boolean
54 55 56 |
# File 'lib/search_cop_grammar/attributes.rb', line 54 def compatible?(value) attributes.all? { |attribute| attribute.compatible? value } end |
#eql?(other) ⇒ Boolean
18 19 20 |
# File 'lib/search_cop_grammar/attributes.rb', line 18 def eql?(other) self == other end |
#fulltext? ⇒ Boolean
50 51 52 |
# File 'lib/search_cop_grammar/attributes.rb', line 50 def fulltext? (query_info.scope.reflection.[key] || {})[:type] == :fulltext end |
#generator(generator, value) ⇒ Object
36 37 38 39 40 |
# File 'lib/search_cop_grammar/attributes.rb', line 36 def generator(generator, value) attributes.collect! do |attribute| SearchCopGrammar::Nodes::Generator.new(attribute, generator: generator, value: value) end.inject(:or) end |
#generator_for(name) ⇒ Object
100 101 102 |
# File 'lib/search_cop_grammar/attributes.rb', line 100 def generator_for(name) generators[name] end |
#generators ⇒ Object
108 109 110 |
# File 'lib/search_cop_grammar/attributes.rb', line 108 def generators query_info.scope.reflection.generators end |
#hash ⇒ Object
26 27 28 |
# File 'lib/search_cop_grammar/attributes.rb', line 26 def hash [query_info.model, key].hash end |
#klass_for(name) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/search_cop_grammar/attributes.rb', line 75 def klass_for(name) alias_value = query_info.scope.reflection.aliases[name] return alias_value if alias_value.is_a?(Class) value = alias_value || name klass_for_association(value) || value.classify.constantize end |
#klass_for_association(name) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/search_cop_grammar/attributes.rb', line 66 def klass_for_association(name) reflections = query_info.model.reflections return reflections[name].klass if reflections[name] return reflections[name.to_sym].klass if reflections[name.to_sym] nil end |
#matches(value) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/search_cop_grammar/attributes.rb', line 42 def matches(value) if fulltext? SearchCopGrammar::Nodes::MatchesFulltext.new self, value.to_s else attributes.collect! { |attribute| attribute.matches value }.inject(:or) end end |
#options ⇒ Object
58 59 60 |
# File 'lib/search_cop_grammar/attributes.rb', line 58 def query_info.scope.reflection.[key] end |
#valid_operator?(operator) ⇒ Boolean
104 105 106 |
# File 'lib/search_cop_grammar/attributes.rb', line 104 def valid_operator?(operator) (INCLUDED_OPERATORS + generators.keys).include?(operator) end |