itrigga-core_ext 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "bundler", "~> 1.0.0"
10
+ gem "jeweler", "~> 1.6.4"
11
+ gem "rcov", ">= 0"
12
+ gem "rspec", ">= 0"
13
+ gem "fastercsv", ">= 1.5.4"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ fastercsv (1.5.4)
6
+ git (1.2.5)
7
+ jeweler (1.6.4)
8
+ bundler (~> 1.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ rake (0.8.7)
12
+ rcov (0.9.9)
13
+ rspec (2.5.0)
14
+ rspec-core (~> 2.5.0)
15
+ rspec-expectations (~> 2.5.0)
16
+ rspec-mocks (~> 2.5.0)
17
+ rspec-core (2.5.1)
18
+ rspec-expectations (2.5.0)
19
+ diff-lcs (~> 1.1.2)
20
+ rspec-mocks (2.5.0)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ bundler (~> 1.0.0)
27
+ fastercsv (>= 1.5.4)
28
+ jeweler (~> 1.6.4)
29
+ rcov
30
+ rspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 iTrigga Media Ltd
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = core_ext
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to core_ext
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Al Davidson. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,62 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "itrigga-core_ext"
18
+ gem.homepage = "http://gems.itrigga.com/trigga-core_ext"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{extensions for core classes Hash, Array}
21
+ gem.description = %Q{Adds methods for
22
+ Hash:
23
+ - symbolize_keys
24
+ - recursive_symbolize_keys
25
+ Array:
26
+ - to_csv (requires FasterCSV gem)
27
+ - select_with_index
28
+ - to_auto_complete_list
29
+ - active_record_type_condition_rendering
30
+ }
31
+ gem.email = "aldavidson@itrigga.com"
32
+ gem.authors = ["Al Davidson", "Anson Kelly"]
33
+ # dependencies defined in Gemfile
34
+ end
35
+ Jeweler::RubygemsDotOrgTasks.new
36
+
37
+ require 'rake/testtask'
38
+ Rake::TestTask.new(:test) do |test|
39
+ test.libs << 'lib' << 'test'
40
+ test.pattern = 'test/**/test_*.rb'
41
+ test.verbose = true
42
+ end
43
+
44
+ require 'rcov/rcovtask'
45
+ Rcov::RcovTask.new do |test|
46
+ test.libs << 'test'
47
+ test.pattern = 'test/**/test_*.rb'
48
+ test.verbose = true
49
+ test.rcov_opts << '--exclude "gems/*"'
50
+ end
51
+
52
+ task :default => :test
53
+
54
+ require 'rake/rdoctask'
55
+ Rake::RDocTask.new do |rdoc|
56
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
57
+
58
+ rdoc.rdoc_dir = 'rdoc'
59
+ rdoc.title = "itrigga-core_ext #{version}"
60
+ rdoc.rdoc_files.include('README*')
61
+ rdoc.rdoc_files.include('lib/**/*.rb')
62
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,3 @@
1
+ require 'itrigga/core_ext/array'
2
+ require 'itrigga/core_ext/hash'
3
+ require 'itrigga/core_ext/string'
@@ -0,0 +1,70 @@
1
+ require 'fastercsv'
2
+
3
+ class Array
4
+
5
+ # transforms the collection into a csv formatted string
6
+ #
7
+ # each column is described by a label/property pair
8
+ # eg [["Published","created_at"]] generates a column with label "Published" and the data value being the created_at field
9
+ #
10
+ def to_csv(columns)
11
+ FasterCSV.generate do |csv|
12
+ csv << columns.collect{|c| c.to_a.first }
13
+ self.each do |result|
14
+ csv << columns.collect{|c| c.to_a.last }.collect{|c| result.send(c) }
15
+ end
16
+ end
17
+
18
+ end
19
+
20
+
21
+ def select_with_index(&block)
22
+ selected = []
23
+ self.each_index do |i|
24
+ selected << self[i] if block.call(self[i],i) == true
25
+ end
26
+ selected
27
+ end
28
+
29
+
30
+ #
31
+ # to_auto_complete_list(:label => ["? (?)",:auditable_type,:auditable_id], :value => ["?-?",:auditable_type,:auditable_id])
32
+ #
33
+ def to_auto_complete_list(opts = {})
34
+
35
+ if opts.blank? || opts[:label].blank? || opts[:value].blank? || (opts[:label].kind_of?(Array) && opts[:label].size < 2)
36
+ return []
37
+ end
38
+
39
+ list = collect do |obj|
40
+ [active_record_type_condition_rendering(opts[:label],obj), active_record_type_condition_rendering(opts[:value],obj)]
41
+ end
42
+
43
+ # a :include_blank key
44
+ # if it == true then just include a blank array (ie all elements)
45
+ # if it == Array then render it using the AR rendering eg ["?",property]
46
+ if opts[:include_blank] == true
47
+ list.unshift(["All",""])
48
+ elsif opts[:include_blank]
49
+ list = collect {|obj| [active_record_type_condition_rendering(opts[:include_blank],obj), active_record_type_condition_rendering(opts[:include_blank],obj)] }.uniq + list
50
+ end
51
+
52
+ list.compact.uniq
53
+
54
+ end
55
+
56
+ private
57
+
58
+ def active_record_type_condition_rendering(format, obj)
59
+ if format.kind_of?(Array)
60
+ label = format.clone
61
+ label_format = label.shift
62
+ label_split = label_format.split('?')
63
+ label_values = label.collect{|p| obj.respond_to?(p) ? obj.send(p) : "" }
64
+ label_split.zip(label_values).join
65
+ else
66
+ obj.respond_to?(format) ? obj.send(format) : ""
67
+ end
68
+ end
69
+
70
+ end
@@ -0,0 +1,20 @@
1
+ class Hash
2
+
3
+ def symbolize_keys!
4
+ inject({}) do |options, (key, value)|
5
+ options[(key.to_sym rescue key) || key] = value
6
+ options
7
+ end
8
+ end
9
+
10
+ def recursive_symbolize_keys!
11
+
12
+ inject({}) do |options, (key, value)|
13
+
14
+ options[(key.to_sym rescue key) || key] = value.is_a?( Hash ) ? value.recursive_symbolize_keys! : value
15
+ options
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,15 @@
1
+ class String
2
+
3
+ def remove_whitespace
4
+ self.gsub(" ","")
5
+ end
6
+
7
+ def is_i?
8
+ !!(self =~ /^[-+]?[0-9]([0-9]*)?$/)
9
+ end
10
+
11
+ def to_utf16le
12
+ Iconv.conv('utf-16le', 'UTF-8', self)
13
+ end
14
+
15
+ end
Binary file
Binary file
Binary file
data/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'core_ext'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestCoreExt < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: itrigga-core_ext
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Al Davidson
14
+ - Anson Kelly
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-07-20 00:00:00 +01:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ type: :development
24
+ prerelease: false
25
+ name: bundler
26
+ version_requirements: &id001 !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ~>
30
+ - !ruby/object:Gem::Version
31
+ hash: 23
32
+ segments:
33
+ - 1
34
+ - 0
35
+ - 0
36
+ version: 1.0.0
37
+ requirement: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ type: :development
40
+ prerelease: false
41
+ name: jeweler
42
+ version_requirements: &id002 !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ hash: 7
48
+ segments:
49
+ - 1
50
+ - 6
51
+ - 4
52
+ version: 1.6.4
53
+ requirement: *id002
54
+ - !ruby/object:Gem::Dependency
55
+ type: :development
56
+ prerelease: false
57
+ name: rcov
58
+ version_requirements: &id003 !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ requirement: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ type: :development
70
+ prerelease: false
71
+ name: rspec
72
+ version_requirements: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirement: *id004
82
+ - !ruby/object:Gem::Dependency
83
+ type: :development
84
+ prerelease: false
85
+ name: fastercsv
86
+ version_requirements: &id005 !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ hash: 11
92
+ segments:
93
+ - 1
94
+ - 5
95
+ - 4
96
+ version: 1.5.4
97
+ requirement: *id005
98
+ description: "Adds methods for \n Hash: \n - symbolize_keys\n - recursive_symbolize_keys\n Array:\n - to_csv (requires FasterCSV gem)\n - select_with_index\n - to_auto_complete_list\n - active_record_type_condition_rendering\n "
99
+ email: aldavidson@itrigga.com
100
+ executables: []
101
+
102
+ extensions: []
103
+
104
+ extra_rdoc_files:
105
+ - LICENSE.txt
106
+ - README.rdoc
107
+ files:
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - LICENSE.txt
111
+ - README.rdoc
112
+ - Rakefile
113
+ - VERSION
114
+ - lib/itrigga/core_ext.rb
115
+ - lib/itrigga/core_ext/array.rb
116
+ - lib/itrigga/core_ext/hash.rb
117
+ - lib/itrigga/core_ext/string.rb
118
+ - pkg/core_ext-0.1.0.gem
119
+ - pkg/itrigga-core_ext-0.1.0.gem
120
+ - pkg/trigga-core_ext-0.1.0.gem
121
+ - test/helper.rb
122
+ - test/test_core_ext.rb
123
+ has_rdoc: true
124
+ homepage: http://gems.itrigga.com/trigga-core_ext
125
+ licenses:
126
+ - MIT
127
+ post_install_message:
128
+ rdoc_options: []
129
+
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ hash: 3
138
+ segments:
139
+ - 0
140
+ version: "0"
141
+ required_rubygems_version: !ruby/object:Gem::Requirement
142
+ none: false
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ hash: 3
147
+ segments:
148
+ - 0
149
+ version: "0"
150
+ requirements: []
151
+
152
+ rubyforge_project:
153
+ rubygems_version: 1.3.7
154
+ signing_key:
155
+ specification_version: 3
156
+ summary: extensions for core classes Hash, Array
157
+ test_files: []
158
+