Class: Helium::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/helium/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Collection

Returns a new instance of Collection



7
8
9
10
11
12
13
# File 'lib/helium/collection.rb', line 7

def initialize(opts)
  @client     = opts.fetch(:client)
  @klass      = opts.fetch(:klass)
  @belongs_to = opts.fetch(:belongs_to, nil)

  @filter_criteria = {}
end

Instance Attribute Details

#filter_criteriaObject (readonly)

Returns the value of attribute filter_criteria



5
6
7
# File 'lib/helium/collection.rb', line 5

def filter_criteria
  @filter_criteria
end

Instance Method Details

#+(other) ⇒ Object

TODO: could support something like label.sensors << new_sensor see Label#add_sensors for where it would be applied



60
61
62
# File 'lib/helium/collection.rb', line 60

def +(other)
  collection + other
end

#-(other) ⇒ Object



64
65
66
# File 'lib/helium/collection.rb', line 64

def -(other)
  collection - other
end

#==(other) ⇒ Object

Collections are considered equal if they contain the same resources as determined by the resources' ids



74
75
76
# File 'lib/helium/collection.rb', line 74

def ==(other)
  self.map(&:id).sort == other.map(&:id).sort
end

#[](index) ⇒ Object



68
69
70
# File 'lib/helium/collection.rb', line 68

def [](index)
  collection[index]
end

#allHelium::Collection

Returns all resources

Parameters:

  • opts (Hash)

    a customizable set of options

Returns:



18
19
20
21
# File 'lib/helium/collection.rb', line 18

def all
  @filter_criteria = {}
  self
end

#collectionResource

Returns an array of the Resources belonging to the Collection

Returns:



42
43
44
# File 'lib/helium/collection.rb', line 42

def collection
  fetch_collection
end

#eachObject



46
47
48
# File 'lib/helium/collection.rb', line 46

def each
  collection.each{ |element| yield element }
end

#inspectObject



50
51
52
# File 'lib/helium/collection.rb', line 50

def inspect
  collection
end

#lastObject

NOTE: if we implement pagination, we'll need to rethink this



79
80
81
# File 'lib/helium/collection.rb', line 79

def last
  collection.last
end

#to_json(*options) ⇒ Object



54
55
56
# File 'lib/helium/collection.rb', line 54

def to_json(*options)
  collection.to_json(*options)
end

#where(criteria) ⇒ Collection

Uses metadata filtering (https://docs.helium.com/api/v1/metadata/index.html#filtering) to search for a collection of resources matching the search parameters

Examples:

Search for sensors by location

client.sensors.where(location: 'Building B') #=> [Sensor, Sensor]

Search for multiple matching search parameters

client.sensors.where(departments: ['it', 'engineering']) #=> [Sensor, Sensor]

Parameters:

  • criteria (Hash)

    a set of search criteria

Returns:

  • (Collection)

    a collection of resources matching the provided search criteria



35
36
37
38
# File 'lib/helium/collection.rb', line 35

def where(criteria)
  add_filter_criteria(criteria)
  self
end