Class: AttrSearchableGrammar::Attributes::Collection
- Inherits:
-
Object
- Object
- AttrSearchableGrammar::Attributes::Collection
- Defined in:
- lib/attr_searchable_grammar/attributes.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #attribute_for(attribute_definition) ⇒ Object
- #attributes ⇒ Object
- #compatible?(value) ⇒ Boolean
- #eql?(other) ⇒ Boolean
- #fulltext? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(model, key) ⇒ Collection
constructor
A new instance of Collection.
- #matches(value) ⇒ Object
- #options ⇒ Object
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
#key ⇒ Object (readonly)
Returns the value of attribute key.
7 8 9 |
# File 'lib/attr_searchable_grammar/attributes.rb', line 7 def key @key end |
#model ⇒ Object (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, ) end |
#attributes ⇒ Object
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
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
16 17 18 |
# File 'lib/attr_searchable_grammar/attributes.rb', line 16 def eql?(other) self == other end |
#fulltext? ⇒ Boolean
42 43 44 |
# File 'lib/attr_searchable_grammar/attributes.rb', line 42 def fulltext? (model.[key] || {})[:type] == :fulltext end |
#hash ⇒ Object
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 |
#options ⇒ Object
50 51 52 |
# File 'lib/attr_searchable_grammar/attributes.rb', line 50 def model.[key] end |