dm-unlazy 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/dm-unlazy.rb +11 -0
 - data/lib/dm-unlazy/collection.rb +74 -0
 - data/lib/dm-unlazy/model.rb +34 -0
 - metadata +82 -0
 
    
        data/lib/dm-unlazy.rb
    ADDED
    
    | 
         @@ -0,0 +1,11 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            #--
         
     | 
| 
      
 2 
     | 
    
         
            +
            #            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
         
     | 
| 
      
 3 
     | 
    
         
            +
            #                    Version 2, December 2004
         
     | 
| 
      
 4 
     | 
    
         
            +
            #
         
     | 
| 
      
 5 
     | 
    
         
            +
            #            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
         
     | 
| 
      
 6 
     | 
    
         
            +
            #   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
         
     | 
| 
      
 7 
     | 
    
         
            +
            #
         
     | 
| 
      
 8 
     | 
    
         
            +
            #  0. You just DO WHAT THE FUCK YOU WANT TO.
         
     | 
| 
      
 9 
     | 
    
         
            +
            #++
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
            require 'dm-unlazy/collection'
         
     | 
| 
         @@ -0,0 +1,74 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            #--
         
     | 
| 
      
 2 
     | 
    
         
            +
            #            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
         
     | 
| 
      
 3 
     | 
    
         
            +
            #                    Version 2, December 2004
         
     | 
| 
      
 4 
     | 
    
         
            +
            #
         
     | 
| 
      
 5 
     | 
    
         
            +
            #            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
         
     | 
| 
      
 6 
     | 
    
         
            +
            #   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
         
     | 
| 
      
 7 
     | 
    
         
            +
            #
         
     | 
| 
      
 8 
     | 
    
         
            +
            #  0. You just DO WHAT THE FUCK YOU WANT TO.
         
     | 
| 
      
 9 
     | 
    
         
            +
            #++
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
            require 'dm-unlazy/model'
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            module DataMapper
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
            class Collection < LazyArray
         
     | 
| 
      
 16 
     | 
    
         
            +
            	def unlazy
         
     | 
| 
      
 17 
     | 
    
         
            +
            		Unlazy::Collection.new self
         
     | 
| 
      
 18 
     | 
    
         
            +
            	end
         
     | 
| 
      
 19 
     | 
    
         
            +
            end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
            module Unlazy
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
            class Collection
         
     | 
| 
      
 24 
     | 
    
         
            +
            	def initialize (collection)
         
     | 
| 
      
 25 
     | 
    
         
            +
            		unless collection.is_a?(DataMapper::Collection)
         
     | 
| 
      
 26 
     | 
    
         
            +
            			raise ArgumentError, 'you have to pass a Collection'
         
     | 
| 
      
 27 
     | 
    
         
            +
            		end
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
            		@collection = collection
         
     | 
| 
      
 30 
     | 
    
         
            +
            	end
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
      
 32 
     | 
    
         
            +
            	def method_missing (id, *args, &block)
         
     | 
| 
      
 33 
     | 
    
         
            +
            		if (Array.instance_method(id) rescue false)
         
     | 
| 
      
 34 
     | 
    
         
            +
            			reload unless @resources
         
     | 
| 
      
 35 
     | 
    
         
            +
             
     | 
| 
      
 36 
     | 
    
         
            +
            			return @resources.__send__ id, *args, &block
         
     | 
| 
      
 37 
     | 
    
         
            +
            		end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
            		result = @collection.__send__ id, *args, &block
         
     | 
| 
      
 40 
     | 
    
         
            +
             
     | 
| 
      
 41 
     | 
    
         
            +
            		if result.is_a?(DataMapper::Collection)
         
     | 
| 
      
 42 
     | 
    
         
            +
            			Collection.new(result)
         
     | 
| 
      
 43 
     | 
    
         
            +
            		else
         
     | 
| 
      
 44 
     | 
    
         
            +
            			result
         
     | 
| 
      
 45 
     | 
    
         
            +
            		end
         
     | 
| 
      
 46 
     | 
    
         
            +
            	end
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
            	def fields (*fields)
         
     | 
| 
      
 49 
     | 
    
         
            +
            		@fields = fields.flatten.map(&:to_sym)
         
     | 
| 
      
 50 
     | 
    
         
            +
            		@fields.push *model.key.entries.map(&:name)
         
     | 
| 
      
 51 
     | 
    
         
            +
            		@fields.uniq!
         
     | 
| 
      
 52 
     | 
    
         
            +
             
     | 
| 
      
 53 
     | 
    
         
            +
            		self
         
     | 
| 
      
 54 
     | 
    
         
            +
            	end
         
     | 
| 
      
 55 
     | 
    
         
            +
             
     | 
| 
      
 56 
     | 
    
         
            +
            	def reload
         
     | 
| 
      
 57 
     | 
    
         
            +
            		adapter = repository.adapter
         
     | 
| 
      
 58 
     | 
    
         
            +
            		query   = @collection.all(:fields => @fields || model.properties.map(&:field)).query
         
     | 
| 
      
 59 
     | 
    
         
            +
             
     | 
| 
      
 60 
     | 
    
         
            +
            		@resources = adapter.select(*adapter.send(:select_statement, query).flatten).map {|data|
         
     | 
| 
      
 61 
     | 
    
         
            +
            			if data.is_a?(Struct)
         
     | 
| 
      
 62 
     | 
    
         
            +
            				Unlazy::Model.new(model, data)
         
     | 
| 
      
 63 
     | 
    
         
            +
            			else
         
     | 
| 
      
 64 
     | 
    
         
            +
            				Unlazy::Model.new(model, Struct.new(@fields.first).new(data))
         
     | 
| 
      
 65 
     | 
    
         
            +
            			end
         
     | 
| 
      
 66 
     | 
    
         
            +
            		}
         
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
      
 68 
     | 
    
         
            +
            		self
         
     | 
| 
      
 69 
     | 
    
         
            +
            	end
         
     | 
| 
      
 70 
     | 
    
         
            +
            end
         
     | 
| 
      
 71 
     | 
    
         
            +
             
     | 
| 
      
 72 
     | 
    
         
            +
            end
         
     | 
| 
      
 73 
     | 
    
         
            +
             
     | 
| 
      
 74 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,34 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            #--
         
     | 
| 
      
 2 
     | 
    
         
            +
            #            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
         
     | 
| 
      
 3 
     | 
    
         
            +
            #                    Version 2, December 2004
         
     | 
| 
      
 4 
     | 
    
         
            +
            #
         
     | 
| 
      
 5 
     | 
    
         
            +
            #            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
         
     | 
| 
      
 6 
     | 
    
         
            +
            #   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
         
     | 
| 
      
 7 
     | 
    
         
            +
            #
         
     | 
| 
      
 8 
     | 
    
         
            +
            #  0. You just DO WHAT THE FUCK YOU WANT TO.
         
     | 
| 
      
 9 
     | 
    
         
            +
            #++
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
            module DataMapper; module Unlazy
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
            class Model
         
     | 
| 
      
 14 
     | 
    
         
            +
            	def initialize (model, data = nil)
         
     | 
| 
      
 15 
     | 
    
         
            +
            		@model = model.new
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
            		if data
         
     | 
| 
      
 18 
     | 
    
         
            +
            			@model.attributes = Hash[data.each_pair.to_a]
         
     | 
| 
      
 19 
     | 
    
         
            +
            		end
         
     | 
| 
      
 20 
     | 
    
         
            +
            	end
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
            	def method_missing (id, *args, &block)
         
     | 
| 
      
 23 
     | 
    
         
            +
            		result = @model.__send__ id, *args, &block
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
            		if [Associations::OneToMany::Collection, Associations::ManyToMany::Collection].any? { |k| k === result }
         
     | 
| 
      
 26 
     | 
    
         
            +
            			result.source.define_singleton_method(:saved?) { true }
         
     | 
| 
      
 27 
     | 
    
         
            +
            			result.reload
         
     | 
| 
      
 28 
     | 
    
         
            +
            		end
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
            		result
         
     | 
| 
      
 31 
     | 
    
         
            +
            	end
         
     | 
| 
      
 32 
     | 
    
         
            +
            end
         
     | 
| 
      
 33 
     | 
    
         
            +
             
     | 
| 
      
 34 
     | 
    
         
            +
            end; end
         
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,82 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: dm-unlazy
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version
         
     | 
| 
      
 4 
     | 
    
         
            +
              version: 0.0.1
         
     | 
| 
      
 5 
     | 
    
         
            +
              prerelease: 
         
     | 
| 
      
 6 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 7 
     | 
    
         
            +
            authors:
         
     | 
| 
      
 8 
     | 
    
         
            +
            - meh.
         
     | 
| 
      
 9 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 10 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 11 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 12 
     | 
    
         
            +
            date: 2012-02-08 00:00:00.000000000 Z
         
     | 
| 
      
 13 
     | 
    
         
            +
            dependencies:
         
     | 
| 
      
 14 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 15 
     | 
    
         
            +
              name: dm-core
         
     | 
| 
      
 16 
     | 
    
         
            +
              requirement: &14330280 !ruby/object:Gem::Requirement
         
     | 
| 
      
 17 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 18 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 19 
     | 
    
         
            +
                - - ! '>='
         
     | 
| 
      
 20 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 21 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 22 
     | 
    
         
            +
              type: :runtime
         
     | 
| 
      
 23 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 24 
     | 
    
         
            +
              version_requirements: *14330280
         
     | 
| 
      
 25 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 26 
     | 
    
         
            +
              name: rake
         
     | 
| 
      
 27 
     | 
    
         
            +
              requirement: &14329060 !ruby/object:Gem::Requirement
         
     | 
| 
      
 28 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 29 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 30 
     | 
    
         
            +
                - - ! '>='
         
     | 
| 
      
 31 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 32 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 33 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 34 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 35 
     | 
    
         
            +
              version_requirements: *14329060
         
     | 
| 
      
 36 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency
         
     | 
| 
      
 37 
     | 
    
         
            +
              name: rspec
         
     | 
| 
      
 38 
     | 
    
         
            +
              requirement: &14327500 !ruby/object:Gem::Requirement
         
     | 
| 
      
 39 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 40 
     | 
    
         
            +
                requirements:
         
     | 
| 
      
 41 
     | 
    
         
            +
                - - ! '>='
         
     | 
| 
      
 42 
     | 
    
         
            +
                  - !ruby/object:Gem::Version
         
     | 
| 
      
 43 
     | 
    
         
            +
                    version: '0'
         
     | 
| 
      
 44 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 45 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 46 
     | 
    
         
            +
              version_requirements: *14327500
         
     | 
| 
      
 47 
     | 
    
         
            +
            description: 
         
     | 
| 
      
 48 
     | 
    
         
            +
            email: meh@paranoici.org
         
     | 
| 
      
 49 
     | 
    
         
            +
            executables: []
         
     | 
| 
      
 50 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 51 
     | 
    
         
            +
            extra_rdoc_files: []
         
     | 
| 
      
 52 
     | 
    
         
            +
            files:
         
     | 
| 
      
 53 
     | 
    
         
            +
            - lib/dm-unlazy/model.rb
         
     | 
| 
      
 54 
     | 
    
         
            +
            - lib/dm-unlazy/collection.rb
         
     | 
| 
      
 55 
     | 
    
         
            +
            - lib/dm-unlazy.rb
         
     | 
| 
      
 56 
     | 
    
         
            +
            homepage: http://github.com/meh/dm-unlazy
         
     | 
| 
      
 57 
     | 
    
         
            +
            licenses: []
         
     | 
| 
      
 58 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 59 
     | 
    
         
            +
            rdoc_options: []
         
     | 
| 
      
 60 
     | 
    
         
            +
            require_paths:
         
     | 
| 
      
 61 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 62 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 63 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 64 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 65 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 66 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 67 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 68 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         
     | 
| 
      
 69 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 70 
     | 
    
         
            +
              requirements:
         
     | 
| 
      
 71 
     | 
    
         
            +
              - - ! '>='
         
     | 
| 
      
 72 
     | 
    
         
            +
                - !ruby/object:Gem::Version
         
     | 
| 
      
 73 
     | 
    
         
            +
                  version: '0'
         
     | 
| 
      
 74 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 75 
     | 
    
         
            +
            rubyforge_project: 
         
     | 
| 
      
 76 
     | 
    
         
            +
            rubygems_version: 1.8.15
         
     | 
| 
      
 77 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 78 
     | 
    
         
            +
            specification_version: 3
         
     | 
| 
      
 79 
     | 
    
         
            +
            summary: Get unlazy collections, for when you know you are going to use all that data
         
     | 
| 
      
 80 
     | 
    
         
            +
              anyway.
         
     | 
| 
      
 81 
     | 
    
         
            +
            test_files: []
         
     | 
| 
      
 82 
     | 
    
         
            +
            has_rdoc: 
         
     |