army-negative 1.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
+ = 1.0.0, release 2011-03-11
2
+
3
+ * Initial Release
4
+
data/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (C) 2011 by Kendall Gifford
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ the Software, and to permit persons to whom the Software is furnished to do so,
8
+ subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,45 @@
1
+ = army-negative: negative one's for true!
2
+
3
+ This gem is a simple ActiveRecord MySQL (+ARMy+) connection adapter
4
+ monkey-patch. Put it in your +Gemfile+ and all your +true+ are belong to us! Or,
5
+ rather, they'll suddenly become very negative, negative one specifically.
6
+
7
+ == What This Does
8
+
9
+ This makes ActiveRecord store the value -1 into your +TINYINT+ +boolean+ columns
10
+ whenever they're set to +true+. It also makes ActiveRecord recognize -1 as true
11
+ when a +boolean+ field is queried.
12
+
13
+ *NOTE:* positive one and all other values that were interpreted as +true+ by the
14
+ MySQL connection adapter will still be recognized as +true+ too.
15
+
16
+ == Why
17
+
18
+ This was written for a rails application that needed to access a legacy MySQL
19
+ database. The new application needed to work concurrently with the existing
20
+ (old) application, following its conventions.
21
+
22
+ The old application is a Microsoft Access 2003 _program_ that uses the "linked
23
+ tables" feature to store the data in a MySQL database. Microsoft's Visual Basic
24
+ for Applications (VBA) stores +true+ values in memory with all bits turned on.
25
+ If interpreted as an integer using two's complement, this is negative one. So
26
+ the old application stores all our +true+ values as negative one. Because of
27
+ this, many of the hand-written legacy queries also write and expect to read true
28
+ values as negative one.
29
+
30
+ == The Name
31
+
32
+ Though no one besides myself is likely to see or use this gem, I still wanted to
33
+ push it out to the public for posterity. The +ar+ in ARmy is for ActiveRecord
34
+ while the +my+ in arMy is for MySQL. I'm sure you can guess what _negative_ is
35
+ supposed to reference.
36
+
37
+ == Authors and Credits
38
+
39
+ Authors:: Kendall Gifford
40
+
41
+ == License
42
+
43
+ Licensed using the standard {MIT License}[http://en.wikipedia.org/wiki/MIT_License].
44
+ See the file {LICENSE}[http://github.com/zettabyte/army-negative/blob/master/LICENSE]
45
+ in the root folder of the project.
@@ -0,0 +1,23 @@
1
+ require 'army-negative/railtie' if defined?(Rails)
2
+
3
+ module Army
4
+ module Negative
5
+
6
+ autoload :Column, 'army-negative/column'
7
+ autoload :Quoting, 'army-negative/quoting'
8
+ autoload :MysqlAdapter, 'army-negative/mysql_adapter'
9
+
10
+ def patch_active_record!
11
+ ActiveRecord::ConnectionAdapters::Column.send :include, Column
12
+ ActiveRecord::ConnectionAdapters::Quoting.send :include, Quoting
13
+ end
14
+ module_function :patch_active_record!
15
+
16
+ def patch_mysql_adapter!
17
+ require 'active_record/connection_adapters/mysql_adapter'
18
+ ActiveRecord::ConnectionAdapters::MysqlAdapter.send :include, MysqlAdapter
19
+ end
20
+ module_function :patch_mysql_adapter!
21
+
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ require 'set'
2
+
3
+ module Army
4
+ module Negative
5
+ module Column
6
+ TRUE_VALUES_WITH_NEGATIVE_ONES = [-1, '-1'].to_set
7
+
8
+ #
9
+ # Extend ActiveRecord::ConnectionAdapters::Column
10
+ #
11
+ def self.included(klass)
12
+ klass.send(:extend, ClassMethods)
13
+ klass.instance_eval do
14
+ class << self
15
+ alias_method_chain :value_to_boolean, :negative_ones
16
+ end
17
+ end
18
+ end
19
+
20
+ module ClassMethods
21
+ #
22
+ # Recognize integer -1 and string '-1' as "true" values.
23
+ #
24
+ def value_to_boolean_with_negative_ones(value)
25
+ return true if TRUE_VALUES_WITH_NEGATIVE_ONES.include?(value)
26
+ value_to_boolean_without_negative_ones(value)
27
+ end
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,26 @@
1
+ require 'set'
2
+
3
+ module Army
4
+ module Negative
5
+ module MysqlAdapter
6
+
7
+ QUOTED_TRUE_AS_NEGATIVE_ONE = "-1".freeze
8
+
9
+ def self.included(klass)
10
+ klass.instance_eval do
11
+ alias_method :quoted_true, :quoted_true_as_negative_one
12
+ end
13
+ end
14
+
15
+ #
16
+ # This doesn't seem to actually be necessary in rails 3.0 (though it doesn't
17
+ # break anything either) but I'm doing this for completeness: make is so
18
+ # that MySQL adapter-specific calls to #quoted_true also get "-1".
19
+ #
20
+ def quoted_true_as_negative_one
21
+ QUOTED_TRUE_AS_NEGATIVE_ONE
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ module Army
2
+ module Negative
3
+ module Quoting
4
+
5
+ #
6
+ # Extend ActiveRecord::ConnectionAdapters::Quoting
7
+ #
8
+ def self.included(klass)
9
+ klass.instance_eval do
10
+ alias_method_chain :quote, :negative_one
11
+ end
12
+ end
13
+
14
+ #
15
+ # Ensure "true" values get stored as -1 in the database where they'd
16
+ # normally have been stored as 1
17
+ #
18
+ def quote_with_negative_one(value, column = nil)
19
+ result = quote_without_negative_one(value, column)
20
+ (value.is_a?(TrueClass) and result == '1') ? '-1' : result
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ require 'army-negative'
2
+ require 'rails'
3
+
4
+ module Army
5
+ module Negative
6
+ class Railtie < Rails::Railtie
7
+ initializer "army-negative.patch_active_record", :after => "active_record.initialize_database" do
8
+ Army::Negative.patch_active_record!
9
+ Army::Negative.patch_mysql_adapter!
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module Army
2
+ module Negative
3
+ module Version
4
+ MAJOR = 1
5
+ MINOR = 0
6
+ TINY = 0
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: army-negative
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Kendall Gifford
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-11 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: |-
23
+ When this gem is loaded in your rails app (Gemfile) your MySQL connection
24
+ adapter for ActiveRecord will be monkey-patched. The patch simply tweaks it
25
+ to store all boolean "true" values as negative one instead of positive one
26
+ inside your TINYINT columns. It also patches it to recognize and interpret
27
+ negative one as "true". Positive one will still be recognized as true as
28
+ well.
29
+ Used for special cases, such as developing rails apps that must, for
30
+ example, work with existing databases that use such a convention.
31
+ email:
32
+ - zettabyte@gmail.com
33
+ executables: []
34
+
35
+ extensions: []
36
+
37
+ extra_rdoc_files: []
38
+
39
+ files:
40
+ - lib/army-negative/column.rb
41
+ - lib/army-negative/railtie.rb
42
+ - lib/army-negative/mysql_adapter.rb
43
+ - lib/army-negative/quoting.rb
44
+ - lib/army-negative/version.rb
45
+ - lib/army-negative.rb
46
+ - LICENSE
47
+ - README.rdoc
48
+ - CHANGELOG.rdoc
49
+ has_rdoc: true
50
+ homepage: http://github.com/zettabyte/army-negative/wiki
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !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
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 23
73
+ segments:
74
+ - 1
75
+ - 3
76
+ - 6
77
+ version: 1.3.6
78
+ requirements: []
79
+
80
+ rubyforge_project: army-negative
81
+ rubygems_version: 1.3.7
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Monkey-patches ActiveRecord's MySQL (ARMy) connection adapter to store boolean trues as negative ones
85
+ test_files: []
86
+