Module: Flexirest::Associations::ClassMethods

Includes:
ActiveSupport::Inflector
Defined in:
lib/flexirest/associations.rb

Instance Method Summary collapse

Instance Method Details

#_associationsObject



80
81
82
# File 'lib/flexirest/associations.rb', line 80

def _associations
  @_associations
end

#_date_fieldsObject



76
77
78
# File 'lib/flexirest/associations.rb', line 76

def _date_fields
  @_date_fields.uniq
end

#_include_associationsObject



64
65
66
# File 'lib/flexirest/associations.rb', line 64

def _include_associations
  (Thread.current[:flexirest_include_associations] ||= {})[self] || []
end

#_reset_include_associations!Object



68
69
70
# File 'lib/flexirest/associations.rb', line 68

def _reset_include_associations!
  (Thread.current[:flexirest_include_associations] ||= {}).delete(self)
end

#has_many(key, klass = nil) ⇒ 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
# File 'lib/flexirest/associations.rb', line 6

def has_many(key, klass = nil)
  if klass.nil?
    klass = key.to_s.classify.constantize
  end

  @_associations ||= {}
  @_associations[key] = klass
  define_method(key) do
    unless _attributes[key].is_a?(Array) || _attributes[key].is_a?(Flexirest::ResultIterator)
      return _attributes[key]
    end

    if _attributes[key].size == 0
      return _attributes[key]
    end

    if _attributes[key][0].is_a?(klass)
      return _attributes[key]
    end

    _attributes[key].each_with_index do |v, k|
      _attributes[key][k] = klass.new(v)
    end

    _attributes[key]
  end
end

#has_one(key, klass = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/flexirest/associations.rb', line 34

def has_one(key, klass = nil)
  if klass.nil?
    klass = key.to_s.classify.constantize
  end

  @_associations ||= {}
  @_associations[key] = klass
  define_method(key) do
    return nil if _attributes[key].nil?

    if _attributes[key].is_a?(klass)
      return _attributes[key]
    end

    _attributes[key] = klass.new(_attributes[key])

    _attributes[key]
  end
end

#includes(*keys) ⇒ Object

The keys passed to .includes(...) travel from this class-method call into the Request built by the subsequent finder call. Storing them in a class instance variable meant concurrent threads calling .includes on the same model shared (and corrupted) one slot, so they're held in thread-local storage keyed by the class instead.



59
60
61
62
# File 'lib/flexirest/associations.rb', line 59

def includes(*keys)
  (Thread.current[:flexirest_include_associations] ||= {})[self] = keys
  self
end

#inherited(subclass) ⇒ Object



85
86
87
88
89
# File 'lib/flexirest/associations.rb', line 85

def inherited(subclass)
  subclass.instance_variable_set(:@_date_fields, [])
  subclass.instance_variable_set(:@_associations, {})
  super
end

#parse_date(*keys) ⇒ Object



72
73
74
# File 'lib/flexirest/associations.rb', line 72

def parse_date(*keys)
  keys.each { |key| @_date_fields << key }
end