raw_column 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5f1b12dba1fa63bcbbf4b85450ea135d644b2519
4
+ data.tar.gz: 8f6d88b5b5601482ad46e9676ddb8d659c7e739b
5
+ SHA512:
6
+ metadata.gz: ac23693ccef23a8b3d2df8ed13c561a841bc0e63008298c75b7053cf5c97bd49883a25b3129993e2a0c7d8fbee8964cec6438a408695f7f440437731cebbd365
7
+ data.tar.gz: a8f8fa4f6c55c645d5b1f8d1091eba079908d85dcc32a13a92aa1080abe84d1e9923b6d30decb73f2c3067ae10b683640b25de34ac03af429ca37f350d81f05b
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in raw_column.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Tony Schneider
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # RawColumn
2
+
3
+ Let me just start out by saying that this is an edge case.
4
+
5
+ With rails 4 came support for postgresql json columns. Every now and again I find myself wanting to stuff a blob into a column. However, when I pull out the JSON, rails turns it into a hash! That's great, but I'm just going to serialize that hash into JSON. I feel like there's a "yo dawg" in there somewhere, but I don't care to explore it more.
6
+
7
+ So, yeah, here's a gem that does that for you.
8
+
9
+ We don't need no stinking serialization.
10
+
11
+ ## Installation
12
+
13
+ `gem 'raw_column'`
14
+
15
+ Then `bundle install` that bad mamma jamma.
16
+
17
+ ## Usage
18
+
19
+ Include the module, specify your raw columns. Like so:
20
+
21
+ ```ruby
22
+ class DeathStar < ActiveRecord::Base
23
+
24
+ include RawColumn
25
+
26
+ raw_column :geonosian_blueprints
27
+
28
+ def moon?
29
+ false
30
+ end
31
+ end
32
+ ```
33
+
34
+ ```ruby
35
+ # In your controller somewhere
36
+ respond_to :json
37
+ def show
38
+ @deathstar = DeathStar.find(params[:id])
39
+ respond_with(@deathstar.raw_geonosian_blueprints)
40
+ end
41
+ ```
42
+
43
+ So there's one method: `raw_column`. All it's doing is defining a convenience method `raw_#{your_column}` that simply wraps its `before_type_cast` method in a proxy object to handle all the `as_json` wizardry.
44
+
45
+ I usually end up naming my blob column `content`, so I made that the default. If you're feeling super lazy and would like to confuse the next developer who inherits your one-a-kind codebase, you can just say `raw_column` and I'll go ahead and assume you meant `:content`. You're welcome.
46
+
47
+ ## Result
48
+
49
+ On a minor scale (maybe your show method)
50
+
51
+ ```
52
+ Completed 200 OK in 42ms (Views: 27.2ms | ActiveRecord: 2.6ms)
53
+
54
+ became
55
+
56
+ Completed 200 OK in 15ms (Views: 0.2ms | ActiveRecord: 3.2ms)
57
+ ```
58
+
59
+ On a much larger scale (say, your index)
60
+ ```
61
+ Completed 200 OK in 1257ms (Views: 854.6ms | ActiveRecord: 25.0ms)
62
+
63
+ became
64
+
65
+ Completed 200 OK in 240ms (Views: 60.1ms | ActiveRecord: 11.2ms)
66
+ ```
67
+
68
+ Sure, those are contrived, but you see what I mean: In cases where you're just spitting out preformatted JSON, the (JSON -> Hash -> JSON) conversion is a waste.
69
+
70
+ ## Todo
71
+
72
+ Write a test. I extracted it from an app. There are tests there, promise. Give me a minute, okay?
73
+
74
+ ## Contributing
75
+
76
+ There ain't much here. That said, if it's broke, do me a solid and fix it. We can have beers or something, somewhere, sometime.
77
+
78
+ ## Thanks
79
+
80
+ Created mostly while sitting on Jones' Couch. Thanks for the help. And the couch - man that thing's good.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,18 @@
1
+ module RawColumn
2
+
3
+ class JsonProxy
4
+
5
+ def initialize(record, column_name=:content)
6
+ @raw_content = record.send(:"#{column_name}_before_type_cast")
7
+ end
8
+
9
+ def as_json(*args)
10
+ self
11
+ end
12
+
13
+ def encode_json(encoder)
14
+ @raw_content
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module RawColumn
2
+ VERSION = "0.0.1"
3
+ end
data/lib/raw_column.rb ADDED
@@ -0,0 +1,21 @@
1
+ require "raw_column/version"
2
+ require "raw_column/json_proxy"
3
+
4
+ module RawColumn
5
+
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+
10
+ def raw_column(*column_names)
11
+ class_eval do
12
+ column_names.each do |column_name|
13
+ define_method :"raw_#{column_name}" do
14
+ JsonProxy.new(self, column_name)
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'raw_column/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "raw_column"
8
+ spec.version = RawColumn::VERSION
9
+ spec.authors = ["Tony Schneider"]
10
+ spec.email = ["tonywok@gmail.com"]
11
+ spec.summary = %q{Return json directly from the database}
12
+ spec.description = %q{You don't need no stinking serialization}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: raw_column
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Tony Schneider
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: You don't need no stinking serialization
42
+ email:
43
+ - tonywok@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/raw_column.rb
54
+ - lib/raw_column/json_proxy.rb
55
+ - lib/raw_column/version.rb
56
+ - raw_column.gemspec
57
+ homepage: ''
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.2.0
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Return json directly from the database
81
+ test_files: []