Module: ComicVine::Mongo
- Included in:
- Resource::Character, Resource::Concept, Resource::Episode, Resource::Issue, Resource::Location, Resource::Movie, Resource::Object, Resource::Origin, Resource::Person, Resource::Power, Resource::Promo, Resource::Publisher, Resource::Series, Resource::StoryArc, Resource::Team, Resource::Volume
- Defined in:
- lib/comicvine/mongo.rb,
lib/comicvine/mongo/version.rb
Overview
Module to hold our mongoid specific methods and overrides for Resource
Constant Summary collapse
- VERSION =
'0.1.2'
Instance Method Summary collapse
-
#_fetch_by_assoc_and_id(meta, id) ⇒ Resource::Character, ...
Will parse the relation metadata to identify the class and resource type.
-
#initialize(args) ⇒ Object
Replaces the default Resource::initialize function, allowing us to create mongo enabled classes.
-
#save_assoc! ⇒ Object
Cycles through associated id’s on the object and loads/fetches the child/parent objects and saves them to mongo.
Instance Method Details
#_fetch_by_assoc_and_id(meta, id) ⇒ Resource::Character, ...
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
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/comicvine/mongo.rb', line 78 def _fetch_by_assoc_and_id(, id) if Object.class_eval(.class_name).where(id: id).exists? Object.class_eval(.class_name).find(id) else type = .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 |
#initialize(args) ⇒ Object
Replaces the default Resource::initialize function, allowing us to create mongo enabled classes
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/comicvine/mongo.rb', line 19 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 |
#save_assoc! ⇒ Object
Cycles through associated id’s on the object and loads/fetches the child/parent objects and saves them to mongo
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/comicvine/mongo.rb', line 41 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? = self.reflect_on_association(assoc) self.method(attr).call.each do |id| obj = self._fetch_by_assoc_and_id(, 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? = self.reflect_on_association(assoc) obj = self._fetch_by_assoc_and_id(, self.method(attr).call) obj.fetch!.save! unless obj.nil? else next end end end |