module ComicVine::Mongo

Module to hold our mongoid specific methods and overrides for {ComicVine::Resource} @since 0.1.0

Constants

VERSION

Public Class Methods

new(args) click to toggle source

Replaces the default {ComicVine::Resource::initialize} function, allowing us to create mongo enabled classes @since 0.1.0

Calls superclass method
# File lib/comicvine/mongo.rb, line 18
def initialize(args)

  args.each do |k, v|

    # Convert sub arrays to objects
    v.collect! { |i| ComicVine::Resource::create_resource(i) } if v.kind_of?(Array) && !v.empty? && v.first.key?('api_detail_url')

    # Convert sub hashes to objects
    if v.kind_of?(Hash) && v.key?('api_detail_url')
      v = ComicVine::Resource::create_resource(v)
    end

    # Set the instance variable to value
    args[k] = v
  end
  #puts args.to_json
  super
end

Public Instance Methods

_fetch_by_assoc_and_id(meta, id) click to toggle source

Will parse the relation metadata to identify the class and resource type. A check is performed to see if it is saved in the mongodb, if so it will then load it. It will then query ComicVine::API for the latest information, and return the resource @param meta [Mongoid::Relations::Metadata] @param id [Integer] @macro return.sub_resources @since 0.1.0

# File lib/comicvine/mongo.rb, line 77
def _fetch_by_assoc_and_id(meta, id)
  if Object.class_eval(meta.class_name).where(id: id).exists?
    Object.class_eval(meta.class_name).find(id)
  else
    type = meta.class_name.demodulize.underscore.to_sym

    if ComicVine::API.find_detail(type).nil?
      nil
    else
      resource_name = ComicVine::API.find_detail(type)['detail_resource_name'].to_sym

      begin
        ComicVine::API.get_details(resource_name, id)
      rescue ComicVine::API::ComicVineAPIError
        return nil
      end

    end
  end
end
fetch_and_update!() click to toggle source

Method to fetch info from ComicVine and save @return [ComicVine::Resource] @since 0.1.5

# File lib/comicvine/mongo.rb, line 102
def fetch_and_update!
  fetch!
  save!
  self
end
fetch_and_update_assoc!() click to toggle source

Method to fetch info from ComicVine and save assoc objects @return [ComicVine::Resource] @since 0.1.5

# File lib/comicvine/mongo.rb, line 112
def fetch_and_update_assoc!
  fetch!
  save!
  save_assoc!
  self
end
save_assoc!() click to toggle source

Cycles through associated id's on the object and loads/fetches the child/parent objects and saves them to mongo @since 0.1.0

# File lib/comicvine/mongo.rb, line 40
def save_assoc!
  # Identify methods that end in id or ids
  self.methods.each do |attr|
    # Match attribute ids
    next if attr !~ /^(?!_)([\w\d\_]+)(_ids?)$/

    # endings with ids imply multiple
    if attr =~ /^(?!_)([\w\d\_]+)(_ids)$/
      assoc = $1.to_s.pluralize.to_sym

      next if self.reflect_on_association(assoc).nil?

      meta = self.reflect_on_association(assoc)
      self.method(attr).call.each do |id|
        obj = self._fetch_by_assoc_and_id(meta, id)
        obj.fetch!.save! unless obj.nil?
      end
    elsif attr =~ /^(?!_)([\w\d\_]+)(_id)$/
      assoc = $1.to_s.to_sym

      next if self.reflect_on_association(assoc).nil?

      meta = self.reflect_on_association(assoc)
      obj = self._fetch_by_assoc_and_id(meta, self.method(attr).call)
      obj.fetch!.save! unless obj.nil?
    else
      next
    end
  end
end