pat-maddox-encode_field 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ ---
2
+ patch: 1
3
+ major: 1
4
+ minor: 0
@@ -0,0 +1,24 @@
1
+ module EncodeField
2
+ def encode_field(field_name)
3
+ column = columns.detect {|c| c.name == "encoded_#{field_name}" }
4
+ if column.nil?
5
+ raise "The field 'encoded_#{field_name}' does not exist"
6
+ elsif ![:string, :text].include?(column.type)
7
+ raise "'encoded_#{field_name}' must be of type :string or :text but is :#{column.type}"
8
+ end
9
+
10
+ define_method("#{field_name}=") do |val|
11
+ send("encoded_#{field_name}=", YAML.dump(val))
12
+ end
13
+
14
+ define_method(field_name) do
15
+ (encoded = send("encoded_#{field_name}")) ? YAML.load(encoded) : nil
16
+ end
17
+
18
+ define_method("#{field_name}?") do
19
+ send("encoded_#{field_name}?")
20
+ end
21
+ end
22
+ end
23
+
24
+ ActiveRecord::Base.extend EncodeField
@@ -0,0 +1,8 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :accounts, :force => true do |t|
3
+ t.text :encoded_preferences
4
+ t.text :encoded_text_field
5
+ t.string :encoded_string_field
6
+ t.integer :encoded_integer_field
7
+ end
8
+ end
@@ -0,0 +1,42 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "encode_field" do
4
+ class Account < ActiveRecord::Base
5
+ encode_field :preferences
6
+ end
7
+
8
+ it "should encode and retrieve" do
9
+ prefs = {:type => 'platinum', "email" => "joe@example.com", :age => 23}
10
+ Account.create! :preferences => prefs
11
+ Account.find(:first).preferences.should == prefs
12
+ end
13
+
14
+ it "should define query methods" do
15
+ Account.new.preferences?.should be_false
16
+ Account.new(:preferences => "foobar").preferences?.should be_true
17
+ end
18
+
19
+ it "should handle nil preferences okay" do
20
+ Account.new.preferences.should be_nil
21
+ end
22
+
23
+ it "should print a message if the database field doesn't exist" do
24
+ lambda {
25
+ Account.class_eval { encode_field :whereswaldo }
26
+ }.should raise_error(/The field 'encoded_whereswaldo' does not exist/)
27
+ end
28
+
29
+ it "should print a message if the database field is not a string or text" do
30
+ lambda {
31
+ Account.class_eval { encode_field :text_field }
32
+ }.should_not raise_error
33
+
34
+ lambda {
35
+ Account.class_eval { encode_field :string_field }
36
+ }.should_not raise_error
37
+
38
+ lambda {
39
+ Account.class_eval { encode_field :integer_field }
40
+ }.should raise_error(/'encoded_integer_field' must be of type :string or :text but is :int/)
41
+ end
42
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+ require 'active_record'
4
+ this_dir = File.expand_path(File.dirname(__FILE__))
5
+ require this_dir + '/../lib/encode_field.rb'
6
+
7
+ FileUtils.mkdir(this_dir + '/log') unless File.directory?(this_dir + '/log')
8
+ ActiveRecord::Base.logger = Logger.new(this_dir + "/log/test.log")
9
+
10
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => "#{this_dir}/db/test.sqlite3")
11
+ load(this_dir + '/db/schema.rb')
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pat-maddox-encode_field
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pat Maddox
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-14 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: TODO
17
+ email: pat.maddox@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - VERSION.yml
26
+ - lib/encode_field.rb
27
+ - spec/db
28
+ - spec/db/schema.rb
29
+ - spec/spec_helper.rb
30
+ - spec/encode_field_spec.rb
31
+ has_rdoc: false
32
+ homepage: http://github.com/pat-maddox/encode_field
33
+ post_install_message:
34
+ rdoc_options: []
35
+
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.2.0
54
+ signing_key:
55
+ specification_version: 2
56
+ summary: TODO
57
+ test_files: []
58
+