Sha256: eafd4a757a5700eee2f3948e671fac5c635b54f14a15f7a3d0180a64d9f109de

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

Contents

module ComicVine
  class CVObject
    def initialize(args)
      args.each do |k,v|
        self.class.class_eval { attr_reader k }
        v.collect! { |i| CVObject.new i } if v.kind_of?(Array) && !v.empty? && v.first.key?("api_detail_url")
        v = CVObject.new v if v.kind_of?(Hash) && v.key?("api_detail_url")
        instance_variable_set "@#{k}", v
      end
    end
    
    def fetch
      ComicVine::API.get_details_by_url(@api_detail_url)
    end
      
    def method_missing(method_sym, *arguments, &block)
      if method_sym.to_s =~ /^get_(.*)$/
        key = method_sym.to_s.sub "get_", ""
        if instance_variable_defined?("@#{key}")
          item = instance_variable_get("@#{key}")
          if item.kind_of?(Array) && item.first.kind_of?(CVObject)
            res = []
            item.each do |i|
              res << i.fetch
            end
            return res
          end
          if item.kind_of?(CVObject)
            return item.fetch
          end
        else
          super
        end
      elsif
        super
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
comic_vine-0.1.5 lib/comic_vine/cv_object.rb
comic_vine-0.1.4 lib/comic_vine/cv_object.rb
comic_vine-0.1.3 lib/comic_vine/cv_object.rb
comic_vine-0.1.2 lib/comic_vine/cv_object.rb