Class: StravaApi::HashBasedStore

Inherits:
Object
  • Object
show all
Defined in:
lib/strava-api/hash_based_store.rb

Direct Known Subclasses

Bike, Club, Effort, Member, Ride, Segment, Streams

Instance Method Summary collapse

Constructor Details

#initialize(connection, attribute_map, nested_class_map, options) ⇒ HashBasedStore

Returns a new instance of HashBasedStore.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/strava-api/hash_based_store.rb', line 3

def initialize(connection, attribute_map, nested_class_map, options)
  @connection = connection
  @valid_attributes = attribute_map
  @nested_class_map = nested_class_map

  @values = {}
  @valid_attributes.each do |json_key, ruby_key|
    value = options[json_key]
    @values[ruby_key] = value.is_a?(Hash) ? @nested_class_map[ruby_key].new(@connection, value) : value
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/strava-api/hash_based_store.rb', line 44

def method_missing(symbol, *args)
  if @valid_attributes.values.include?(symbol)
    @values[symbol]
  else
    raise InternalError
  end
end

Instance Method Details

#[](key) ⇒ Object



15
16
17
# File 'lib/strava-api/hash_based_store.rb', line 15

def [](key)
  @values[key]
end

#idObject

This needs to be explicit because otherwise you get the Object#id method (which is depreciated) rather than method missing getting called



21
22
23
# File 'lib/strava-api/hash_based_store.rb', line 21

def id
  @values[:id]
end

#merge(other) ⇒ Object



38
39
40
41
42
# File 'lib/strava-api/hash_based_store.rb', line 38

def merge(other)
  @valid_attributes.each do |json_key, ruby_key|
    @values[ruby_key] = other[ruby_key] if other[ruby_key]
  end
end

#to_sObject Also known as: inspect



25
26
27
28
29
30
31
32
# File 'lib/strava-api/hash_based_store.rb', line 25

def to_s
  result = []
  @values.each do |key, value|
    result << ":#{key} => #{value}" if value
  end
  
  "#<#{self.class} [#{result.join(', ')}]>"
end