capital 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .rvmrc
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ spec/support/db
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Steve Richert
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,29 @@
1
+ # Capital [![Build Status](https://secure.travis-ci.org/laserlemon/capital.png)](http://travis-ci.org/laserlemon/capital) [![Dependency Status](https://gemnasium.com/laserlemon/capital.png)](https://gemnasium.com/laserlemon/capital)
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'capital'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install capital
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :test => :spec
9
+ task :default => :test
data/capital.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = "capital"
5
+ gem.version = "0.1.0"
6
+
7
+ gem.authors = ["Steve Richert"]
8
+ gem.email = ["steve.richert@gmail.com"]
9
+ gem.description = "Extend your database columns, converting values to/from rich objects"
10
+ gem.summary = "Top off your database columns"
11
+ gem.homepage = "https://github.com/laserlemon/capital"
12
+
13
+ gem.add_dependency "activerecord", "~> 3.0"
14
+
15
+ gem.add_development_dependency "active_support", "~> 3.0"
16
+ gem.add_development_dependency "rake", "~> 0.9"
17
+ gem.add_development_dependency "rspec", "~> 2.8"
18
+ gem.add_development_dependency "sqlite3", "~> 1.3"
19
+
20
+ gem.files = `git ls-files`.split("\n")
21
+ gem.test_files = `git ls-files -- spec/*`.split("\n")
22
+ gem.require_paths = ["lib"]
23
+ end
data/lib/capital.rb ADDED
@@ -0,0 +1,15 @@
1
+ require "active_record"
2
+ require "capital/core_ext"
3
+
4
+ module Capital
5
+ def rich(column, klass)
6
+ composed_of column,
7
+ :allow_nil => true,
8
+ :class_name => klass.name,
9
+ :constructor => :from_database,
10
+ :converter => proc{|v| klass === v ? v : klass.from_database(v) },
11
+ :mapping => [column, :to_database]
12
+ end
13
+ end
14
+
15
+ ActiveRecord::Base.extend(Capital)
@@ -0,0 +1 @@
1
+ Dir[File.expand_path("../core_ext/*.rb", __FILE__)].each{|f| require f }
@@ -0,0 +1,9 @@
1
+ class ActiveSupport::StringInquirer
2
+ def self.from_database(string)
3
+ new(string)
4
+ end
5
+
6
+ def to_database
7
+ to_s
8
+ end
9
+ end
@@ -0,0 +1,72 @@
1
+ require "spec_helper"
2
+
3
+ describe Capital do
4
+ let(:vehicle){ Vehicle.new }
5
+
6
+ it "accepts simple objects" do
7
+ expect{
8
+ vehicle.color = "red"
9
+ vehicle.save!
10
+ }.to_not raise_error
11
+ end
12
+
13
+ it "accepts rich objects" do
14
+ expect{
15
+ vehicle.color = Color.new("orange")
16
+ vehicle.save!
17
+ }.to_not raise_error
18
+ end
19
+
20
+ it "reduces rich objects to simple objects" do
21
+ color = Color.new("YELLOW")
22
+ color.to_database.should_not == color
23
+ vehicle.color = color
24
+ vehicle.save!
25
+ vehicle.color_before_type_cast.should == color.to_database
26
+ end
27
+
28
+ it "returns rich objects" do
29
+ vehicle.color = "green"
30
+ vehicle.color.should be_a(Color)
31
+ vehicle.color.should be_green
32
+ end
33
+
34
+ it "returns rich objects after save" do
35
+ vehicle.color = "blue"
36
+ vehicle.save!
37
+ vehicle.color.should be_a(Color)
38
+ vehicle.color.should be_blue
39
+ end
40
+
41
+ it "returns rich objects after reload" do
42
+ vehicle.color = "purple"
43
+ vehicle.save!
44
+ vehicle.reload
45
+ vehicle.color.should be_a(Color)
46
+ vehicle.color.should be_purple
47
+ end
48
+
49
+ it "accepts and returns nil values" do
50
+ vehicle.color = "red"
51
+ vehicle.save!
52
+ vehicle.color = nil
53
+ vehicle.color.should be_nil
54
+ end
55
+
56
+ it "accepts and returns nil values after save" do
57
+ vehicle.color = "red"
58
+ vehicle.save!
59
+ vehicle.color = nil
60
+ vehicle.save!
61
+ vehicle.color.should be_nil
62
+ end
63
+
64
+ it "accepts and returns nil values after reload" do
65
+ vehicle.color = "red"
66
+ vehicle.save!
67
+ vehicle.color = nil
68
+ vehicle.save!
69
+ vehicle.reload
70
+ vehicle.color.should be_nil
71
+ end
72
+ end
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+
3
+ describe ActiveSupport::StringInquirer do
4
+ it "should respond to from_database" do
5
+ ActiveSupport::StringInquirer.should respond_to(:from_database)
6
+ end
7
+
8
+ it "should respond to to_database" do
9
+ ActiveSupport::StringInquirer.allocate.should respond_to(:to_database)
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ require "capital"
2
+
3
+ Dir[File.expand_path("../support/**/*.rb", __FILE__)].each{|f| require f }
@@ -0,0 +1,29 @@
1
+ require "active_support/string_inquirer"
2
+
3
+ class Color < ActiveSupport::StringInquirer
4
+ WARM_COLORS = %w(red orange yellow)
5
+ COOL_COLORS = %w(green blue purple)
6
+ COLORS = WARM_COLORS + COOL_COLORS
7
+
8
+ def self.from_database(name)
9
+ new(name.downcase)
10
+ end
11
+
12
+ def to_database
13
+ downcase
14
+ end
15
+
16
+ def warm?
17
+ WARM_COLORS.include?(self)
18
+ end
19
+
20
+ def cool?
21
+ COOL_COLORS.include?(self)
22
+ end
23
+
24
+ def compliment
25
+ size = COLORS.size
26
+ name = COLORS[(COLORS.index(self) + (size / 2)) % size]
27
+ self.class.new(name)
28
+ end
29
+ end
@@ -0,0 +1,9 @@
1
+ require "active_record"
2
+ require File.expand_path("../color", __FILE__)
3
+
4
+ class Vehicle < ActiveRecord::Base
5
+ rich :color, Color
6
+ end
7
+
8
+ class Car < Vehicle
9
+ end
@@ -0,0 +1,30 @@
1
+ require "active_record"
2
+
3
+ ActiveRecord::Base.establish_connection(
4
+ :adapter => "sqlite3",
5
+ :database => File.expand_path("../db/test.db", __FILE__)
6
+ )
7
+
8
+ class CreateSchema < ActiveRecord::Migration
9
+ def self.up
10
+ create_table :vehicles, :force => true do |t|
11
+ t.string :type
12
+ t.string :name
13
+ t.string :color
14
+ t.integer :year
15
+ t.decimal :price, :precision => 8, :scale => 2
16
+ t.timestamps
17
+ end
18
+ end
19
+ end
20
+
21
+ RSpec.configure do |config|
22
+ config.before(:suite) do
23
+ FileUtils.mkdir_p(File.expand_path("../db", __FILE__))
24
+ CreateSchema.suppress_messages{ CreateSchema.migrate(:up) }
25
+ end
26
+
27
+ config.after(:suite) do
28
+ FileUtils.rm_rf(File.expand_path("../db", __FILE__))
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capital
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Steve Richert
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: &70346825159040 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70346825159040
25
+ - !ruby/object:Gem::Dependency
26
+ name: active_support
27
+ requirement: &70346825158540 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70346825158540
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &70346825158040 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '0.9'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70346825158040
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70346825157540 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.8'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70346825157540
58
+ - !ruby/object:Gem::Dependency
59
+ name: sqlite3
60
+ requirement: &70346825156980 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '1.3'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70346825156980
69
+ description: Extend your database columns, converting values to/from rich objects
70
+ email:
71
+ - steve.richert@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .travis.yml
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - capital.gemspec
83
+ - lib/capital.rb
84
+ - lib/capital/core_ext.rb
85
+ - lib/capital/core_ext/string_inquirer.rb
86
+ - spec/capital_spec.rb
87
+ - spec/core_ext/string_inquirer_spec.rb
88
+ - spec/spec_helper.rb
89
+ - spec/support/color.rb
90
+ - spec/support/models.rb
91
+ - spec/support/schema.rb
92
+ homepage: https://github.com/laserlemon/capital
93
+ licenses: []
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 1.8.17
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Top off your database columns
116
+ test_files:
117
+ - spec/capital_spec.rb
118
+ - spec/core_ext/string_inquirer_spec.rb
119
+ - spec/spec_helper.rb
120
+ - spec/support/color.rb
121
+ - spec/support/models.rb
122
+ - spec/support/schema.rb