Class: AttrSearchableGrammar::Attributes::Collection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, key) ⇒ Collection

Returns a new instance of Collection.



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

def initialize(model, key)
  raise(AttrSearchable::UnknownColumn, "Unknown column #{key}") unless model.searchable_attributes[key]

  @model = model
  @key = key
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

Instance Method Details

#==(other) ⇒ Object



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

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

#attribute_for(attribute_definition) ⇒ Object



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

def attribute_for(attribute_definition)
  table, column = attribute_definition.split(".")
  klass = table.classify.constantize

  raise(AttrSearchable::UnknownAttribute, "Unknown attribute #{attribute_definition}") unless klass.columns_hash[column]

  Attributes.const_get(klass.columns_hash[column].type.to_s.classify).new(klass.arel_table.alias(klass.table_name)[column], klass, options)
end

#attributesObject



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

def attributes
  @attributes ||= model.searchable_attributes[key].collect { |attribute_definition| attribute_for attribute_definition }
end

#compatible?(value) ⇒ Boolean

Returns:



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

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

#eql?(other) ⇒ Boolean

Returns:



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

def eql?(other)
  self == other
end

#fulltext?Boolean

Returns:



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

def fulltext?
  (model.searchable_attribute_options[key] || {})[:type] == :fulltext
end

#hashObject



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

def hash
  [model, key].hash
end

#matches(value) ⇒ Object



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

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

#optionsObject



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

def options
  model.searchable_attribute_options[key]
end