Module: Card::Query::Attributes

Included in:
Card::Query
Defined in:
lib/card/query/attributes.rb

Instance Method Summary collapse

Instance Method Details

#complete(val) ⇒ Object



50
51
52
53
54
55
# File 'lib/card/query/attributes.rb', line 50

def complete val
  no_plus_card = (val =~ /\+/ ? "" : "and right_id is null")
  # FIXME: -- this should really be more nuanced --
  # it breaks down after one plus
  name_like "#{val}%", no_plus_card
end

#extension_type(_val) ⇒ Object



61
62
63
64
65
# File 'lib/card/query/attributes.rb', line 61

def extension_type _val
  # DEPRECATED LONG AGO!!!
  Rails.logger.info "using DEPRECATED extension_type in WQL"
  interpret right_plus: AccountID
end

#found_by(val) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/card/query/attributes.rb', line 4

def found_by val
  found_by_cards(val).compact.each do |c|
    unless c && c.respond_to?(:wql_hash)
      raise Card::Error::BadQuery,
            '"found_by" value must be valid Search, ' \
            "but #{c.name} is a #{c.type_name}"
    end
    subquery c.wql_hash.merge(unjoined: true, context: c.name)
  end
end

#found_by_cards(val) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/card/query/attributes.rb', line 15

def found_by_cards val
  if val.is_a? Hash
    Query.run val
  else
    Array.wrap(val).map do |v|
      Card.fetch v.to_name.absolute(context), new: {}
    end
  end
end

#junction_complete(val) ⇒ Object



57
58
59
# File 'lib/card/query/attributes.rb', line 57

def junction_complete val
  name_like ["#{val}%", "%+#{val}%"]
end

#match(val) ⇒ Object

Implements the match attribute that matches always against content and name. That's different from the match operator that can be restricted to names or content. Example: { match: "name or content" } vs. { name: ["match", "a name"] }



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/card/query/attributes.rb', line 29

def match val
  cxn, val = match_prep val
  val.gsub!(/[^#{Card::Name::OK4KEY_RE}]+/, " ")
  return nil if val.strip.empty?

  val_list = val.split(/\s+/).map do |v|
    name_or_content = [
      "replace(#{table_alias}.name,'+',' ')",
      "#{table_alias}.db_content"
    ].map do |field|
      %(#{field} #{cxn.match quote("[[:<:]]#{v}[[:>:]]")})
    end
    or_join name_or_content
  end
  add_condition and_join(val_list)
end

#name_match(val) ⇒ Object



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

def name_match val
  name_like "%#{val}%"
end