Class: Pebbles::Uid::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/pebbles-uid/query.rb

Constant Summary collapse

NO_MARKER =
Class.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(term, options = {}) ⇒ Query

Returns a new instance of Query.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pebbles-uid/query.rb', line 11

def initialize(term, options = {})
  @term = term
  @species_name = options.fetch(:species) { 'species' }
  @path_name = options.fetch(:path) { 'path' }
  @suffix = options[:suffix]
  @stop = options.fetch(:stop) { NO_MARKER }

  if multi_oid_query?
    @terms = expand_uid(term)
  elsif wildcard_query?
    @terms = [term]
  else
    @terms = extract_terms
  end

  if list?
    @species, @path, _ = Pebbles::Uid.parse(terms.first)
  else
    @species, @path, @oid = Pebbles::Uid.parse(term)
  end
end

Instance Attribute Details

#oidObject (readonly)

Returns the value of attribute oid.



9
10
11
# File 'lib/pebbles-uid/query.rb', line 9

def oid
  @oid
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/pebbles-uid/query.rb', line 9

def path
  @path
end

#path_nameObject (readonly)

Returns the value of attribute path_name.



9
10
11
# File 'lib/pebbles-uid/query.rb', line 9

def path_name
  @path_name
end

#speciesObject (readonly)

Returns the value of attribute species.



9
10
11
# File 'lib/pebbles-uid/query.rb', line 9

def species
  @species
end

#species_nameObject (readonly)

Returns the value of attribute species_name.



9
10
11
# File 'lib/pebbles-uid/query.rb', line 9

def species_name
  @species_name
end

#stopObject (readonly)

Returns the value of attribute stop.



9
10
11
# File 'lib/pebbles-uid/query.rb', line 9

def stop
  @stop
end

#suffixObject (readonly)

Returns the value of attribute suffix.



9
10
11
# File 'lib/pebbles-uid/query.rb', line 9

def suffix
  @suffix
end

#termObject (readonly)

Returns the value of attribute term.



9
10
11
# File 'lib/pebbles-uid/query.rb', line 9

def term
  @term
end

#termsObject (readonly)

Returns the value of attribute terms.



9
10
11
# File 'lib/pebbles-uid/query.rb', line 9

def terms
  @terms
end

Instance Method Details

#cache_keysObject



76
77
78
# File 'lib/pebbles-uid/query.rb', line 76

def cache_keys
  terms.map { |t| Pebbles::Uid.cache_key(t) }
end

#collection?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/pebbles-uid/query.rb', line 48

def collection?
  !for_one? && !list?
end

#epitethObject



68
69
70
# File 'lib/pebbles-uid/query.rb', line 68

def epiteth
  species_wrapper.tail.join('.')
end

#epiteth?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/pebbles-uid/query.rb', line 60

def epiteth?
  !species_wrapper.tail.empty?
end

#for_one?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/pebbles-uid/query.rb', line 33

def for_one?
  oid?
end

#genusObject



64
65
66
# File 'lib/pebbles-uid/query.rb', line 64

def genus
  species.split('.').first
end

#listObject



41
42
43
44
45
46
# File 'lib/pebbles-uid/query.rb', line 41

def list
  if !list?
    raise "Cannot expand non-list query"
  end
  terms
end

#list?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/pebbles-uid/query.rb', line 37

def list?
  terms.size != 1 or %w(, |).include?(term.strip[-1..-1])
end

#next_path_labelObject



93
94
95
# File 'lib/pebbles-uid/query.rb', line 93

def next_path_label
  path_wrapper.next_label
end

#oid?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/pebbles-uid/query.rb', line 72

def oid?
  !!@oid && @oid != '*'
end

#path?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/pebbles-uid/query.rb', line 52

def path?
  @path && @path.split('.').reject {|s| s == '*'}.length > 0
end

#species?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/pebbles-uid/query.rb', line 56

def species?
  @species && @species != '*'
end

#to_hashObject



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pebbles-uid/query.rb', line 80

def to_hash
  if list?
    raise RuntimeError.new('Cannot compute a conditions hash for a list of uids')
  end

  hash = species_wrapper.to_hash.merge(path_wrapper.to_hash)
  if oid?
    oid_key = ['oid', suffix].compact.join('_').to_sym
    hash = hash.merge(oid_key => oid)
  end
  hash
end