cymbalize 0.1.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,13 @@
1
+ require 'cymbalize'
2
+
3
+ module Cymbalize
4
+ require 'rails'
5
+
6
+ class Railtie < Rails::Railtie
7
+ initializer 'cymbalize.insert_into_active_record' do
8
+ ActiveSupport.on_load(:active_record) do
9
+ ActiveRecord::Base.send(:include, Cymbalize)
10
+ end
11
+ end
12
+ end
13
+ end
data/lib/cymbalize.rb ADDED
@@ -0,0 +1,60 @@
1
+ require 'cymbalize/railtie'
2
+ require 'active_support/concern'
3
+
4
+ module Cymbalize
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ @@symbolized_options = {}
9
+
10
+ def symbolize(*attribute_names)
11
+ options = attribute_names.extract_options!
12
+
13
+ attribute_names.each do |attribute_name|
14
+ symbolize_attribute(attribute_name, options)
15
+ end
16
+ end
17
+
18
+ def symbolize_attribute(attribute_name, options)
19
+ define_method attribute_name do
20
+ read_symbolized_attribute(attribute_name)
21
+ end
22
+
23
+ if options[:in].present?
24
+ validates_inclusion_of attribute_name,
25
+ :in => options[:in],
26
+ :allow_blank => !!options[:allow_blank]
27
+
28
+ @@symbolized_options[attribute_name] = options[:in]
29
+
30
+ if !!options[:methods]
31
+ options[:in].each do |valid_type|
32
+ define_method "#{valid_type}?" do
33
+ read_symbolized_attribute(attribute_name) == valid_type
34
+ end
35
+ end
36
+ end
37
+
38
+ if !!options[:scopes]
39
+ options[:in].each do |valid_type|
40
+ scope valid_type, where(attribute_name => valid_type)
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ def options_for(attribute_name)
47
+ @@symbolized_options[attribute_name]
48
+ end
49
+ end
50
+
51
+ def read_symbolized_attribute(attribute_name)
52
+ value = read_attribute(attribute_name)
53
+
54
+ if value.present?
55
+ value.to_sym
56
+ else
57
+ nil
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,106 @@
1
+ require 'database'
2
+
3
+ shared_examples 'it has a valid, symbolized gender' do
4
+ it { should be_valid }
5
+ its(:gender) { should be_a(Symbol) }
6
+ end
7
+
8
+ # User is:
9
+ #
10
+ # t.string :name
11
+ # t.string :gender
12
+ # t.string :status
13
+ # t.string :mood
14
+ describe 'Cymbalize' do
15
+ let(:user_class) { create_user_class }
16
+ subject { user_class }
17
+
18
+ it { should respond_to(:symbolize) }
19
+ it { should respond_to(:symbolize_attribute) }
20
+ its(:instance_methods) { should include('read_symbolized_attribute') }
21
+
22
+ context 'with symbolized gender' do
23
+ let(:symbolize_options) { {} }
24
+ before { user_class.symbolize :gender, symbolize_options }
25
+ specify { subject.options_for(:gender).should be_nil }
26
+
27
+ describe 'user instance' do
28
+ subject { user_class.new(:gender => :robot) }
29
+ it_behaves_like 'it has a valid, symbolized gender'
30
+
31
+ context 'initialized with a string' do
32
+ subject { user_class.new(:gender => 'robot') }
33
+ it_behaves_like 'it has a valid, symbolized gender'
34
+ end
35
+ end
36
+
37
+ context 'with the :in option' do
38
+ let(:symbolize_options) { {:in => [:robot, :alien]} }
39
+ specify { subject.options_for(:gender).should =~ [:robot, :alien] }
40
+
41
+ describe 'user instance' do
42
+ subject { user_class.new(:gender => gender) }
43
+
44
+ context 'with a gender in the list' do
45
+ let(:gender) { :robot }
46
+ it_behaves_like 'it has a valid, symbolized gender'
47
+ end
48
+
49
+ context 'with a gender not in the list' do
50
+ let(:gender) { :cowboy }
51
+ it { should_not be_valid }
52
+ end
53
+ end
54
+ end
55
+
56
+ context 'with the :allow_blank option' do
57
+ let(:symbolize_options) { {:in => [:robot, :alien], :allow_blank => true} }
58
+
59
+ describe 'user instance' do
60
+ subject { user_class.new(:gender => gender) }
61
+
62
+ context 'with an empty string value' do
63
+ let(:gender) { '' }
64
+ it { should be_valid }
65
+ its(:gender) { should be_nil }
66
+ end
67
+
68
+ context 'with a nil value' do
69
+ let(:gender) { nil }
70
+ it { should be_valid }
71
+ its(:gender) { should be_nil }
72
+ end
73
+ end
74
+ end
75
+
76
+ context 'with the :methods option' do
77
+ let(:symbolize_options) {{
78
+ :in => [:robot, :alien, :cowboy],
79
+ :methods => true
80
+ }}
81
+
82
+ its(:instance_methods) { should include('robot?', 'alien?') }
83
+
84
+ describe 'robot user instance' do
85
+ subject { user_class.new(:gender => :robot) }
86
+
87
+ it { should respond_to(:robot?, :alien?) }
88
+ it { should be_robot }
89
+ it { should_not be_alien }
90
+ end
91
+ end
92
+
93
+ context 'with the :scopes option' do
94
+ let(:symbolize_options) {{
95
+ :in => [:robot, :alien, :cowboy],
96
+ :scopes => true
97
+ }}
98
+
99
+ it { should respond_to(:robot, :alien, :cowboy) }
100
+
101
+ let(:robot_user) { user_class.create(:gender => :robot) }
102
+ its(:robot) { should include(robot_user) }
103
+ its(:alien) { should_not include(robot_user) }
104
+ end
105
+ end
106
+ end
data/spec/database.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'active_record'
2
+ require 'cymbalize'
3
+
4
+ # Include Cymbalize manually, since Railtie hook won't fire.
5
+ ActiveRecord::Base.send(:include, Cymbalize)
6
+
7
+ # Create our database.
8
+ ActiveRecord::Base.establish_connection(
9
+ :adapter => 'sqlite3',
10
+ :database => ':memory:'
11
+ )
12
+
13
+ ActiveRecord::Schema.define do
14
+ create_table :users do |t|
15
+ t.string :name
16
+ t.string :gender
17
+ t.string :status
18
+ t.string :mood
19
+
20
+ t.timestamps
21
+ end
22
+ end
23
+
24
+ def create_user_class
25
+ Class.new(ActiveRecord::Base) do
26
+
27
+ def self.name
28
+ 'User'
29
+ end
30
+
31
+ self.table_name = 'users'
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cymbalize
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Mukund Lakshman
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-10-14 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rails
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 7
29
+ segments:
30
+ - 3
31
+ - 0
32
+ - 0
33
+ version: 3.0.0
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: activerecord
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 7
45
+ segments:
46
+ - 3
47
+ - 0
48
+ - 0
49
+ version: 3.0.0
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: rspec
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 35
61
+ segments:
62
+ - 2
63
+ - 11
64
+ - 0
65
+ version: 2.11.0
66
+ type: :development
67
+ version_requirements: *id003
68
+ - !ruby/object:Gem::Dependency
69
+ name: sqlite3
70
+ prerelease: false
71
+ requirement: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 23
77
+ segments:
78
+ - 1
79
+ - 3
80
+ - 6
81
+ version: 1.3.6
82
+ type: :development
83
+ version_requirements: *id004
84
+ description: |
85
+ Cymbalize is a tiny extension to support symbolized columns in ActiveRecord,
86
+ with optional convenience methods. It's heavily inspired by nofxx's symbolize
87
+ Gem.
88
+
89
+ email: yaymukund@gmail.com
90
+ executables: []
91
+
92
+ extensions: []
93
+
94
+ extra_rdoc_files: []
95
+
96
+ files:
97
+ - lib/cymbalize.rb
98
+ - lib/cymbalize/railtie.rb
99
+ - spec/cymbalize_spec.rb
100
+ - spec/database.rb
101
+ homepage: http://github.com/yaymukund/cymbalize
102
+ licenses: []
103
+
104
+ post_install_message:
105
+ rdoc_options: []
106
+
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ hash: 3
115
+ segments:
116
+ - 0
117
+ version: "0"
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ hash: 3
124
+ segments:
125
+ - 0
126
+ version: "0"
127
+ requirements: []
128
+
129
+ rubyforge_project:
130
+ rubygems_version: 1.8.24
131
+ signing_key:
132
+ specification_version: 3
133
+ summary: Use symbols in ActiveRecord columns.
134
+ test_files:
135
+ - spec/cymbalize_spec.rb
136
+ - spec/database.rb