module Flexirest::Associations::ClassMethods

Public Instance Methods

_associations() click to toggle source
# File lib/flexirest/associations.rb, line 75
def _associations
  @_associations
end
_date_fields() click to toggle source
# File lib/flexirest/associations.rb, line 71
def _date_fields
  @_date_fields.uniq
end
_include_associations() click to toggle source
# File lib/flexirest/associations.rb, line 59
def _include_associations
  @_include_associations
end
_reset_include_associations!() click to toggle source
# File lib/flexirest/associations.rb, line 63
def _reset_include_associations!
  @_include_associations = []
end
has_many(key, klass = nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/flexirest/associations.rb, line 54
def includes(*keys)
  @_include_associations = keys
  self
end
inherited(subclass) click to toggle source
Calls superclass method
# File lib/flexirest/associations.rb, line 80
def inherited(subclass)
  subclass.instance_variable_set(:@_date_fields, [])
  subclass.instance_variable_set(:@_associations, {})
  subclass.instance_variable_set(:@_include_associations, [])
  super
end
parse_date(*keys) click to toggle source
# File lib/flexirest/associations.rb, line 67
def parse_date(*keys)
  keys.each { |key| @_date_fields << key }
end