Class: SearchCopGrammar::Attributes::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/search_cop_grammar/attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query_info, key) ⇒ Collection

Returns a new instance of Collection.



9
10
11
12
13
14
# File 'lib/search_cop_grammar/attributes.rb', line 9

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

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/search_cop_grammar/attributes.rb', line 7

def key
  @key
end

#query_infoObject (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



20
21
22
# File 'lib/search_cop_grammar/attributes.rb', line 20

def ==(other)
  other.is_a?(self.class) && [query_info.model, key] == [query_info.model, other.key]
end

#alias_for(name) ⇒ Object



77
78
79
# File 'lib/search_cop_grammar/attributes.rb', line 77

def alias_for(name)
  (query_info.scope.reflection.aliases[name] && name) || klass_for(name).table_name
end

#attribute_for(attribute_definition) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/search_cop_grammar/attributes.rb', line 81

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, options)
end

#attributesObject



54
55
56
# File 'lib/search_cop_grammar/attributes.rb', line 54

def attributes
  @attributes ||= query_info.scope.reflection.attributes[key].collect { |attribute_definition| attribute_for attribute_definition }
end

#compatible?(value) ⇒ Boolean

Returns:



46
47
48
# File 'lib/search_cop_grammar/attributes.rb', line 46

def compatible?(value)
  attributes.all? { |attribute| attribute.compatible? value }
end

#eql?(other) ⇒ Boolean

Returns:



16
17
18
# File 'lib/search_cop_grammar/attributes.rb', line 16

def eql?(other)
  self == other
end

#fulltext?Boolean

Returns:



42
43
44
# File 'lib/search_cop_grammar/attributes.rb', line 42

def fulltext?
  (query_info.scope.reflection.options[key] || {})[:type] == :fulltext
end

#hashObject



24
25
26
# File 'lib/search_cop_grammar/attributes.rb', line 24

def hash
  [query_info.model, key].hash
end

#klass_for(name) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/search_cop_grammar/attributes.rb', line 67

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



58
59
60
61
62
63
64
65
# File 'lib/search_cop_grammar/attributes.rb', line 58

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



34
35
36
37
38
39
40
# File 'lib/search_cop_grammar/attributes.rb', line 34

def matches(value)
  if fulltext?
    SearchCopGrammar::Nodes::MatchesFulltext.new self, value.to_s
  else
    attributes.collect! { |attribute| attribute.matches value }.inject(:or)
  end
end

#optionsObject



50
51
52
# File 'lib/search_cop_grammar/attributes.rb', line 50

def options
  query_info.scope.reflection.options[key]
end