Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: carlinisaacson/medic
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: ryanlntn/medic
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 18 commits
  • 41 files changed
  • 2 contributors

Commits on Jan 26, 2015

  1. Copy the full SHA
    c5ae04e View commit details

Commits on Jan 27, 2015

  1. Add Units module

    ryanlntn committed Jan 27, 2015
    Copy the full SHA
    9e2c1bb View commit details
  2. Copy the full SHA
    ff8cd42 View commit details
  3. Fix broken spec

    ryanlntn committed Jan 27, 2015
    Copy the full SHA
    826ce2e View commit details
  4. Remove NSLog statements

    ryanlntn committed Jan 27, 2015
    Copy the full SHA
    ca4e8c9 View commit details
  5. Fix array wrapping

    ryanlntn committed Jan 27, 2015
    Copy the full SHA
    5064f09 View commit details
  6. Use correlation example in save controller to demonstrate saving both…

    … correlations and quantities
    ryanlntn committed Jan 27, 2015
    Copy the full SHA
    2d9b858 View commit details
  7. Always save with metadata

    ryanlntn committed Jan 27, 2015
    Copy the full SHA
    393c4f1 View commit details
  8. Copy the full SHA
    662d6a0 View commit details

Commits on Mar 7, 2015

  1. Merge pull request ryanlntn#3 from ryanlntn/feature/units

    Sample sharing support
    ryanlntn committed Mar 7, 2015
    Copy the full SHA
    39b5c90 View commit details

Commits on Mar 18, 2015

  1. Copy the full SHA
    e6ce6a8 View commit details
  2. Fix specs

    ryanlntn committed Mar 18, 2015
    Copy the full SHA
    c7171b4 View commit details
  3. Copy the full SHA
    9c8a750 View commit details
  4. Merge pull request ryanlntn#5 from ryanlntn/fix/query-class-issues

    Fix query subclassing issues
    ryanlntn committed Mar 18, 2015
    Copy the full SHA
    01875ed View commit details
  5. Update version/README

    ryanlntn committed Mar 18, 2015
    Copy the full SHA
    6392669 View commit details

Commits on Apr 25, 2016

  1. Copy the full SHA
    00be8d1 View commit details
  2. Support Fixnum anchors

    ryanlntn committed Apr 25, 2016
    Copy the full SHA
    34bc3d8 View commit details
  3. Bump version

    ryanlntn committed Apr 25, 2016
    Copy the full SHA
    5d0f044 View commit details
Showing with 300 additions and 197 deletions.
  1. +1 −1 .travis.yml
  2. +3 −3 README.md
  3. +1 −1 Rakefile
  4. +1 −0 app/controllers/menu_controller.rb
  5. +29 −0 app/controllers/save_controller.rb
  6. +1 −0 lib/medic/anchor.rb
  7. +5 −2 lib/medic/{anchored_object_query.rb → anchored_object_query_builder.rb}
  8. +5 −2 lib/medic/{correlation_query.rb → correlation_query_builder.rb}
  9. +20 −19 lib/medic/finders.rb
  10. +1 −0 lib/medic/interval.rb
  11. +5 −2 lib/medic/{observer_query.rb → observer_query_builder.rb}
  12. +6 −2 lib/medic/{sample_query.rb → sample_query_builder.rb}
  13. +5 −2 lib/medic/{source_query.rb → source_query_builder.rb}
  14. +0 −44 lib/medic/statistics_collection_query.rb
  15. +45 −0 lib/medic/statistics_collection_query_builder.rb
  16. +5 −2 lib/medic/{statistics_query.rb → statistics_query_builder.rb}
  17. +52 −18 lib/medic/store.rb
  18. +15 −0 lib/medic/units.rb
  19. +1 −1 lib/medic/version.rb
  20. +1 −1 spec/medic/anchor.rb
  21. +12 −0 spec/medic/anchored_object_query_builder_spec.rb
  22. +0 −12 spec/medic/anchored_object_query_spec.rb
  23. +4 −4 spec/medic/{correlation_query_spec.rb → correlation_query_builder_spec.rb}
  24. +1 −1 spec/medic/hk_constants_spec.rb
  25. +1 −1 spec/medic/interval_spec.rb
  26. +1 −1 spec/medic/medic_spec.rb
  27. +12 −0 spec/medic/observer_query_builder_spec.rb
  28. +0 −12 spec/medic/observer_query_spec.rb
  29. +1 −1 spec/medic/predicate_spec.rb
  30. +1 −1 spec/medic/query_options_spec.rb
  31. +12 −0 spec/medic/sample_query_builder_spec.rb
  32. +0 −12 spec/medic/sample_query_spec.rb
  33. +1 −1 spec/medic/sort_spec.rb
  34. +12 −0 spec/medic/source_query_builder_spec.rb
  35. +0 −12 spec/medic/source_query_spec.rb
  36. +10 −10 spec/medic/{statistics_collection_query_spec.rb → statistics_collection_query_builder_spec.rb}
  37. +1 −1 spec/medic/statistics_options_spec.rb
  38. +12 −0 spec/medic/statistics_query_builder_spec.rb
  39. +0 −12 spec/medic/statistics_query_spec.rb
  40. +16 −15 spec/medic/store_spec.rb
  41. +1 −1 spec/medic/types_spec.rb
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ before_install:
- (ruby --version)
- sudo chown -R travis ~/Library/RubyMotion
- mkdir -p ~/Library/RubyMotion/build
- sudo motion update --cache-version=3.1
- sudo motion update
gemfile:
- Gemfile
script:
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -202,20 +202,20 @@ Medic.blood_type # => :o_negative

#### Queries

If for some reason you need to access the `HKSample` objects directly you can use Medic's Query objects:
If for some reason you need to access the `HKSample` objects directly you can use Medic's QueryBuilder objects:

```ruby
query_params = { type: :dietary_protein, sort_desc: :start_date, limit: 7 }

query = Medic::SampleQuery.new query_params do |query, results, error|
query = Medic::SampleQueryBuilder.new query_params do |query, results, error|
if results
results.each do |sample|
NSLog "#{sample.startDate} - #{sample.quantity.doubleValueForUnit(HKUnit.gramUnit)}"
end
else
NSLog "no results"
end
end
end.query

Medic.execute(query)
```
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
$:.unshift('/Library/RubyMotion3.1/lib')
$:.unshift('/Library/RubyMotion/lib')
require 'motion/project/template/ios'
require './lib/medic'
require 'motion-stump'
1 change: 1 addition & 0 deletions app/controllers/menu_controller.rb
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ def viewDidLoad
@data = [
'Authorize',
'Characteristics',
'Save',
'ObserverQuery',
'SourceQuery',
'SampleQuery',
29 changes: 29 additions & 0 deletions app/controllers/save_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class SaveController < BaseController

def viewDidLoad
super

self.view.backgroundColor = UIColor.purpleColor
self.title = "Save"
end

def viewDidAppear(animated)
blood_pressure = {
correlation_type: :blood_pressure,
objects: [
{
quantity_type: :blood_pressure_systolic,
quantity: 120
},
{
quantity_type: :blood_pressure_diastolic,
quantity: 80
}
]
}
Medic.save(blood_pressure) do |success, error|
NSLog "Saved sample" unless error
end
end

end
1 change: 1 addition & 0 deletions lib/medic/anchor.rb
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ module Anchor
def anchor_for_symbol(sym)
return unless sym
return sym if sym.to_s == '0'
return sym if sym.is_a? Fixnum
return sym if sym.is_a? NSDate
parts = sym.to_s.gsub('_', ' ').split.reject{ |part| part == 'ago' }
component = parts.pop.chomp('s')
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
module Medic
class AnchoredObjectQuery < HKAnchoredObjectQuery
class AnchoredObjectQueryBuilder
attr_reader :params
attr_reader :query

include Medic::Types
include Medic::Predicate
include Medic::Anchor

def initialize(args={}, block=Proc.new)
self.initWithType(object_type(args[:type]),
@params = args
@query = HKAnchoredObjectQuery.alloc.initWithType(object_type(args[:type]),
predicate: predicate(args),
anchor: anchor_for_symbol(args[:anchor_date] || args[:anchor] || args[:date] || HKAnchoredObjectQueryNoAnchor),
limit: args[:limit] || HKObjectQueryNoLimit,
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
module Medic
class CorrelationQuery < HKCorrelationQuery
class CorrelationQueryBuilder
attr_reader :params
attr_reader :query

include Medic::Types
include Medic::Predicate

def initialize(args={}, block=Proc.new)
self.initWithType(object_type(args[:type]),
@params = args
@query = HKCorrelationQuery.alloc.initWithType(object_type(args[:type]),
predicate: predicate(args),
samplePredicates: sample_predicates(args[:sample_predicates]),
completion: block
39 changes: 20 additions & 19 deletions lib/medic/finders.rb
Original file line number Diff line number Diff line change
@@ -3,56 +3,56 @@ module Finders

def observe(type, options={}, block=Proc.new)
query_params = options.merge(type: type)
query = Medic::ObserverQuery.new query_params do |query, completion, error|
query = Medic::ObserverQueryBuilder.new query_params do |query, completion, error|
block.call(completion, error)
end
end.query
Medic.execute(query)
end

def find_sources(type, options={}, block=Proc.new)
query_params = options.merge(type: type)
query = Medic::SourceQuery.new query_params do |query, results, error|
query = Medic::SourceQueryBuilder.new query_params do |query, results, error|
sources = results ? results.allObjects.map{ |source| source.name.to_s } : []
block.call(sources)
end
end.query
Medic.execute(query)
end

def find_samples(type, options={}, block=Proc.new)
query_params = options.merge(type: type)
query = Medic::SampleQuery.new query_params do |query, results, error|
query = Medic::SampleQueryBuilder.new query_params do |query, results, error|
block.call(samples_to_hashes(Array(results)))
end
end.query
Medic.execute(query)
end

def find_correlations(type, options={}, block=Proc.new)
query_params = options.merge(type: type)
query = Medic::CorrelationQuery.new query_params do |query, correlations, error|
query = Medic::CorrelationQueryBuilder.new query_params do |query, correlations, error|
block.call(samples_to_hashes(Array(correlations)))
end
end.query
Medic.execute(query)
end

def find_anchored(type, options={}, block=Proc.new)
query_params = options.merge(type: type)
query = Medic::AnchoredObjectQuery.new query_params do |query, results, new_anchor, error|
query = Medic::AnchoredObjectQueryBuilder.new query_params do |query, results, new_anchor, error|
block.call(samples_to_hashes(Array(results)), new_anchor)
end
end.query
Medic.execute(query)
end

def find_statistics(type, options={}, block=Proc.new)
query_params = options.merge(type: type)
query = Medic::StatisticsQuery.new query_params do |query, statistics, error|
query = Medic::StatisticsQueryBuilder.new query_params do |query, statistics, error|
block.call(statistics_to_hash(statistics)) if statistics
end
end.query
Medic.execute(query)
end

def find_statistics_collection(type, options={}, block=Proc.new)
query_params = options.merge(type: type)
query = Medic::StatisticsCollectionQuery.new query_params do |query, collection, error|
query = Medic::StatisticsCollectionQueryBuilder.new query_params do |query, collection, error|
from_date = options[:from_date] || options[:from] || options[:start_date] || collection.anchorDate
to_date = options[:to_date] || options[:to] || options[:end_date] || NSDate.date
formatted_stats = []
@@ -62,7 +62,8 @@ def find_statistics_collection(type, options={}, block=Proc.new)
})
end
block.call(formatted_stats)
end
end.query

Medic.execute(query)
end

@@ -76,21 +77,21 @@ def samples_to_hashes(samples)
h[:source] = sample.source.name
h[:start_date] = sample.startDate
h[:end_date] = sample.endDate
h[:sample_type] = Medic::Types::TYPE_IDENTIFIERS.index(sample.sampleType.identifier)
h[:sample_type] = Medic::Types::TYPE_IDENTIFIERS.key(sample.sampleType.identifier)

if sample.respond_to?(:categoryType) && sample.respond_to?(:value)
h[:category_type] = Medic::Types::TYPE_IDENTIFIERS.index(sample.categoryType.identifier)
h[:category_type] = Medic::Types::TYPE_IDENTIFIERS.key(sample.categoryType.identifier)
h[:value] = [:in_bed, :asleep][sample.value] # SleepAnalysis is the only category type at the moment
end

if sample.respond_to?(:correlationType) && sample.respond_to?(:objects)
h[:correlation_type] = Medic::Types::TYPE_IDENTIFIERS.index(sample.correlationType.identifier)
h[:correlation_type] = Medic::Types::TYPE_IDENTIFIERS.key(sample.correlationType.identifier)
h[:objects] = samples_to_hashes(Array(sample.objects.allObjects))
end

if sample.respond_to?(:quantity) && sample.respond_to?(:quantityType)
h[:quantity] = sample.quantity.doubleValueForUnit(sample.quantityType.canonicalUnit)
h[:quantity_type] = Medic::Types::TYPE_IDENTIFIERS.index(sample.quantityType.identifier)
h[:quantity_type] = Medic::Types::TYPE_IDENTIFIERS.key(sample.quantityType.identifier)
h[:canonical_unit] = sample.quantityType.canonicalUnit.unitString
end

@@ -108,7 +109,7 @@ def statistics_to_hash(stats)
h[:start_date] = stats.startDate
h[:end_date] = stats.endDate
h[:sources] = stats.sources.map(&:name) if stats.sources
h[:quantity_type] = Medic::Types::TYPE_IDENTIFIERS.index(stats.quantityType.identifier)
h[:quantity_type] = Medic::Types::TYPE_IDENTIFIERS.key(stats.quantityType.identifier)
h[:canonical_unit] = stats.quantityType.canonicalUnit.unitString
h[:data_count] = stats.dataCount
h[:average] = stats.averageQuantity.doubleValueForUnit(stats.quantityType.canonicalUnit) if stats.averageQuantity
1 change: 1 addition & 0 deletions lib/medic/interval.rb
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ def interval(sym)
end
end
n ||= 1

NSDateComponents.new.send("#{component}=", n)
end

Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
module Medic
class ObserverQuery < HKObserverQuery
class ObserverQueryBuilder
attr_reader :params
attr_reader :query

include Medic::Types
include Medic::Predicate

def initialize(args={}, block=Proc.new)
self.initWithSampleType(object_type(args[:type]),
@params = args
@query = HKObserverQuery.alloc.initWithSampleType(object_type(args[:type]),
predicate: predicate(args),
updateHandler: block
)
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
module Medic
class SampleQuery < HKSampleQuery
class SampleQueryBuilder
attr_reader :params
attr_reader :query

include Medic::Types
include Medic::Predicate
include Medic::Sort

def initialize(args={}, block=Proc.new)
@params = args

sort = args[:sort_descriptors] || args[:sort_by] || args[:sort_asc] || args[:sort]
sort = sort_descriptors(sort) if sort
sort ||= sort_descriptors(args[:sort_desc], false) if args[:sort_desc]

self.initWithSampleType(object_type(args[:type]),
@query = HKSampleQuery.alloc.initWithSampleType(object_type(args[:type]),
predicate: predicate(args),
limit: args[:limit] || HKObjectQueryNoLimit,
sortDescriptors: sort,
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
module Medic
class SourceQuery < HKSourceQuery
class SourceQueryBuilder
attr_reader :params
attr_reader :query

include Medic::Types
include Medic::Predicate

def initialize(args={}, block=Proc.new)
self.initWithSampleType(object_type(args[:type]),
@params = args
@query = HKSourceQuery.alloc.initWithSampleType(object_type(args[:type]),
samplePredicate: predicate(args),
completionHandler: block
)
44 changes: 0 additions & 44 deletions lib/medic/statistics_collection_query.rb

This file was deleted.

45 changes: 45 additions & 0 deletions lib/medic/statistics_collection_query_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Medic
class StatisticsCollectionQueryBuilder
attr_reader :params
attr_reader :query

include Medic::Types
include Medic::Predicate
include Medic::StatisticsOptions
include Medic::Anchor
include Medic::Interval

def initialize(args={})
@params = args
@query = HKStatisticsCollectionQuery.alloc.initWithQuantityType(object_type(args[:type]),
quantitySamplePredicate: predicate(args),
options: options_for_stat_query(args[:options]),
anchorDate: anchor_for_symbol(args[:anchor_date] || args[:anchor] || args[:date] || NSDate.date),
intervalComponents: interval(args[:interval_components] || args[:interval])
)
if block_given?
handler = Proc.new
@query.initialResultsHandler = handler
@query.statisticsUpdateHandler = Proc.new{|q,_,r,e| handler.call(q,r,e)} if args[:update] == true
end
@query.statisticsUpdateHandler = args[:update] if args[:update].is_a?(Proc)
end

def initial_results_handler
@query.initialResultsHandler
end

def initial_results_handler=(callback=Proc.new)
@query.initialResultsHandler = callback
end

def statistics_update_handler
@query.statisticsUpdateHandler
end

def statistics_update_handler=(callback=Proc.new)
@query.statisticsUpdateHandler = callback
end

end
end
Loading