hash_all 2.0.0

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.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in hash_all.gemspec
4
+ gemspec
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,49 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "hash_all/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "hash_all"
7
+ s.version = HashAll::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = "Emanuele Tozzato"
10
+ s.email = "etozzato@gmail.com"
11
+ s.homepage = "http://blog.mekdigital.com"
12
+ s.summary = %q{Wrapper for find(:all) that returns Hash}
13
+ s.description = <<-EOF
14
+
15
+ Wrapper/helper for ActiveRecord <tt>find(:all, *args)</tt>. Returns a collection of
16
+ objects injected into a Hash. The attribute used as a key for the Hash should be
17
+ specified as first argument. if :fetch is not specified in the arguments the whole
18
+ object will be assigned as value, otherwise only the attribute spefified.
19
+ You can pass in all the same arguments to this method as you can to <tt>find(:all)</tt>.
20
+
21
+ Example
22
+
23
+ Given the models:
24
+
25
+ Feed(id: integer, state: string, name: string)
26
+ Post(id: integer, feed_name: string, title: string, url: string)
27
+
28
+ count the number of posts of every state. Row count is too high to use SQL JOIN.
29
+
30
+ @post_per_state = {}
31
+ feeds = Feed.hash_all('feed_name', :fetch => 'state')
32
+ posts = Post.all(:select => 'COUNT(id), feed_name',
33
+ :group => 'feed_name')
34
+ posts.each do |p|
35
+ @post_per_state[feeds[p.feed_name]] ||= 0
36
+ @post_per_state[feeds[p.feed_name]] += p.attributes['COUNT(id)'].to_i
37
+ end
38
+
39
+ => {"new york"=>6, "georgia"=>11, "new mexico"=>2, ... }
40
+
41
+ EOF
42
+
43
+ s.rubyforge_project = "hash_all"
44
+
45
+ s.files = `git ls-files`.split("\n")
46
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
47
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
48
+ s.require_paths = ["lib"]
49
+ end
@@ -0,0 +1,40 @@
1
+ module HashAll
2
+
3
+ class ActiveRecord::Base
4
+
5
+ # Wrapper/helper for ActiveRecord <tt>find(:all, *args)</tt>. Returns a collection of
6
+ # objects injected into a Hash. The attribute used as a key for the Hash should be
7
+ # specified as first argument. if :fetch is not specified in the arguments the whole
8
+ # object will be assigned as value, otherwise only the attribute spefified.
9
+ # You can pass in all the same arguments to this method as you can to <tt>find(:all)</tt>.
10
+ #
11
+ # ==== Example
12
+ #
13
+ # Given the models:
14
+ #
15
+ # Feed(id: integer, state: string, name: string)
16
+ # Post(id: integer, feed_name: string, title: string, url: string)
17
+ #
18
+ # count the number of posts of every state. Row count is too high to use SQL JOIN.
19
+ #
20
+ # @post_per_state = {}
21
+ # feeds = Feed.hash_all('feed_name', :fetch => 'state')
22
+ # posts = Post.all(:select => 'COUNT(id), feed_name',
23
+ # :group => 'feed_name')
24
+ # posts.each do |p|
25
+ # @post_per_state[feeds[p.feed_name]] ||= 0
26
+ # @post_per_state[feeds[p.feed_name]] += p.attributes['COUNT(id)'].to_i
27
+ # end
28
+ #
29
+ # => {"new york"=>6, "georgia"=>11, "new mexico"=>2, ... }
30
+
31
+ def self.hash_all(hash_key='id', args={})
32
+ fetch = args.delete(:fetch)
33
+ collection = find(:all, args)
34
+ return {} if collection.empty?
35
+ collection.each.inject({}) { |hash,obj| hash[obj.send(hash_key)] = (fetch.nil? ? obj : obj.send(fetch)); hash}
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,3 @@
1
+ module HashAll
2
+ VERSION = "2.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_all
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 2
7
+ - 0
8
+ - 0
9
+ version: 2.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Emanuele Tozzato
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-03-30 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: " \n Wrapper/helper for ActiveRecord <tt>find(:all, *args)</tt>. Returns a collection of \n objects injected into a Hash. The attribute used as a key for the Hash should be \n specified as first argument. if :fetch is not specified in the arguments the whole \n object will be assigned as value, otherwise only the attribute spefified.\n You can pass in all the same arguments to this method as you can to <tt>find(:all)</tt>.\n \n Example\n \n Given the models:\n \n Feed(id: integer, state: string, name: string)\n Post(id: integer, feed_name: string, title: string, url: string)\n \n count the number of posts of every state. Row count is too high to use SQL JOIN.\n \n @post_per_state = {}\n feeds = Feed.hash_all('feed_name', :fetch => 'state')\n posts = Post.all(:select => 'COUNT(id), feed_name',\n :group => 'feed_name')\n posts.each do |p|\n @post_per_state[feeds[p.feed_name]] ||= 0 \n @post_per_state[feeds[p.feed_name]] += p.attributes['COUNT(id)'].to_i\n end\n \n => {\"new york\"=>6, \"georgia\"=>11, \"new mexico\"=>2, ... }\n \n"
22
+ email: etozzato@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - .gitignore
31
+ - Gemfile
32
+ - Rakefile
33
+ - hash_all.gemspec
34
+ - lib/hash_all.rb
35
+ - lib/hash_all/version.rb
36
+ has_rdoc: true
37
+ homepage: http://blog.mekdigital.com
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project: hash_all
64
+ rubygems_version: 1.3.7
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Wrapper for find(:all) that returns Hash
68
+ test_files: []
69
+