Module: Medic::Predicate

Constant Summary

Constants included from QueryOptions

QueryOptions::QUERY_OPTIONS

Instance Method Summary collapse

Methods included from QueryOptions

#query_option, #query_options

Instance Method Details

#predicate(args = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/medic/predicate.rb', line 6

def predicate(args={})
  return args if args.is_a? NSPredicate
  if args[:where]
    args[:where]
  elsif args[:predicate]
    args[:predicate]
  elsif args[:sample_predicate]
    args[:sample_predicate]
  elsif args[:uuid] || args[:uuids]
    uuids = Array(args[:uuid] || args[:uuids]).map{ |s| NSUUID.alloc.initWithUUIDString(s.to_s) }
    HKQuery.predicateForObjectsWithUUIDs(uuids)
  elsif args[:source] || args[:sources]
    HKQuery.predicateForObjectsFromSources(Array(args[:source] || args[:sources]))
  elsif args[:meta_data] && (args[:allowed_value] || args[:allowed_values])
    HKQuery.predicateForObjectsWithMetadataKey(args[:meta_data].to_s, allowedValues: Array(args[:allowed_value] || args[:allowed_values]))
  elsif args[:meta_data]
    HKQuery.predicateForObjectsWithMetadataKey(args[:meta_data].to_s)
  elsif args[:no_correlation]
    HKQuery.predicateForObjectsWithNoCorrelation
  elsif args[:start_date] && args[:end_date]
    options = query_options(args[:query_options]) || HKQueryOptionNone
    HKQuery.predicateForSamplesWithStartDate(args[:start_date], endDate: args[:end_date], options: options)
  elsif args[:workout]
    HKQuery.predicateForObjectsFromWorkout(args[:workout])
  elsif args[:workout_activity_type] || args[:activity_type]
    activity_type = args[:workout_activity_type] || args[:activity_type]
    HKQuery.predicateForWorkoutsWithWorkoutActivityType(activity_type)
  else
    nil
  end
end