Skip to content

Commit 1b863df

Browse files
committed
Initial commit
0 parents  commit 1b863df

20 files changed

+292
-0
lines changed

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/.bundle/
2+
/.yardoc
3+
/Gemfile.lock
4+
/_yardoc/
5+
/coverage/
6+
/doc/
7+
/pkg/
8+
/spec/reports/
9+
/tmp/

.rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--format documentation
2+
--color

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
language: ruby
2+
rvm:
3+
- 2.2.2
4+
before_install: gem install bundler -v 1.10.6

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in attribute-string.gemspec
4+
gemspec

LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 syguer
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# StringAttributes
2+
String utility for ActiveRecord
3+
4+
## Installation
5+
6+
Add this line to your application's Gemfile:
7+
8+
```ruby
9+
gem 'string_attributes'
10+
```
11+
12+
And then execute:
13+
14+
$ bundle
15+
16+
Or install it yourself as:
17+
18+
$ gem install string_attributes
19+
20+
## Usage
21+
22+
### Strip
23+
strip attribute
24+
25+
```ruby
26+
class User
27+
attr_string :name, strip: true
28+
end
29+
30+
User.create(name: "hoge ")
31+
u = User.last
32+
p u.name #=> "hoge"
33+
```
34+
35+
## Development
36+
37+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38+
39+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40+
41+
## Contributing
42+
43+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/attribute-string.
44+
45+
46+
## License
47+
48+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
49+

Rakefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require "bundler/gem_tasks"
2+
require "rspec/core/rake_task"
3+
4+
RSpec::Core::RakeTask.new(:spec)
5+
6+
task :default => :spec

bin/console

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env ruby
2+
3+
require "bundler/setup"
4+
require "attribute/string"
5+
6+
# You can add fixtures and/or initialization code here to make experimenting
7+
# with your gem easier. You can also use a different console, if you like.
8+
9+
# (If you use this, don't forget to add pry to your Gemfile!)
10+
# require "pry"
11+
# Pry.start
12+
13+
require "irb"
14+
IRB.start

bin/setup

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
IFS=$'\n\t'
4+
5+
bundle install
6+
7+
# Do any other automated setup that you need to do here

lib/string_attributes.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require "string_attributes/version"
2+
require "string_attributes/error"
3+
require "string_attributes/base"
4+
require "string_attributes/railtie" if defined? Rails
5+
6+
module StringAttributes
7+
end

lib/string_attributes/base.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'active_support/concern'
2+
require 'active_support/dependencies/autoload'
3+
require 'string_attributes/strip'
4+
5+
module StringAttributes
6+
module Base
7+
extend ActiveSupport::Concern
8+
extend ActiveSupport::Autoload
9+
10+
include StringAttributes::Strip
11+
12+
module ClassMethods
13+
def attr_string(attribute, options = {})
14+
validate_attribute!(attribute)
15+
16+
set_strip(attribute) if options[:strip]
17+
end
18+
19+
private
20+
21+
def validate_attribute!(attribute)
22+
unless attribute.is_a?(String) || attribute.is_a?(Symbol)
23+
fail StringAttributes::InvalidArgument, 'Attribute must be String or Symbol'
24+
end
25+
26+
unless self.column_names.include? attribute.to_s
27+
fail StringAttributes::UnkownAttribute, "#{self.name} does not have #{attribute}"
28+
end
29+
30+
true
31+
end
32+
end
33+
end
34+
end

lib/string_attributes/error.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module StringAttributes
2+
class InvalidArgument < StandardError; end
3+
4+
class UnkownAttribute < StandardError; end
5+
end

lib/string_attributes/railtie.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module StringAttributes
2+
class Railtie < ::Rails::Railtie
3+
ActiveSupport.on_load :acrive_record do
4+
ActiveRecord::Base.send(:include, StringAttributes::Base)
5+
end
6+
end
7+
end

lib/string_attributes/strip.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'active_support/concern'
2+
3+
module StringAttributes
4+
module Strip
5+
extend ActiveSupport::Concern
6+
7+
module ClassMethods
8+
def set_strip(attribute)
9+
class_eval do
10+
before_validation do
11+
__send__("#{attribute}=", __send__(attribute).strip)
12+
end
13+
end
14+
end
15+
end
16+
end
17+
end

lib/string_attributes/version.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module StringAttributes
2+
VERSION = "0.1.0"
3+
end
4+

spec/spec_helper.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2+
$LOAD_PATH.unshift File.expand_path('./support/dummy_model', __FILE__)
3+
require 'string_attributes'
4+
require 'active_record'
5+
require 'support/dummy_model/user'
6+
7+
ActiveRecord::Base.send(:include, StringAttributes::Base)
8+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
9+
10+
RSpec.configure do |config|
11+
config.before :all do
12+
ActiveRecord::Schema.define(version: 1) do
13+
create_table :users do |t|
14+
t.string :name
15+
end
16+
end
17+
end
18+
19+
config.after :all do
20+
ActiveRecord::Base.connection.tables.each do |table|
21+
ActiveRecord::Base.connection.drop_table(table)
22+
end
23+
end
24+
end

spec/string_attributes_spec.rb

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require 'spec_helper'
2+
3+
describe StringAttributes do
4+
it 'has a version number' do
5+
expect(StringAttributes::VERSION).not_to be nil
6+
end
7+
8+
context ".attr_string" do
9+
subject { User.attr_string attribute }
10+
11+
context "when invalid attribute given" do
12+
let(:attribute) { [] }
13+
14+
it { expect{ subject }.to raise_error StringAttributes::InvalidArgument }
15+
end
16+
17+
context "when empty attribute given" do
18+
let(:attribute) { "" }
19+
20+
it { expect{ subject }.to raise_error StringAttributes::UnkownAttribute }
21+
end
22+
23+
context "when unknown column given" do
24+
let(:attribute) { "hoge" }
25+
26+
it { expect{ subject }.to raise_error StringAttributes::UnkownAttribute }
27+
end
28+
end
29+
30+
context "stlip" do
31+
subject { User.create(name: "hoge ") }
32+
33+
before { User.reset_callbacks(:validation) }
34+
35+
context "when strip: true" do
36+
before { User.attr_string :name, strip: true }
37+
38+
it { expect(subject.name).to eq "hoge" }
39+
end
40+
41+
context "when strip: false" do
42+
before { User.attr_string :name, strip: false }
43+
44+
it { expect(subject.name).to eq "hoge " }
45+
end
46+
end
47+
end

spec/support/dummy_model/user.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class User < ActiveRecord::Base
2+
end

string_attributes-0.1.0.gem

7.5 KB
Binary file not shown.

string_attributes.gemspec

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# coding: utf-8
2+
lib = File.expand_path('../lib', __FILE__)
3+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4+
require 'string_attributes/version'
5+
6+
Gem::Specification.new do |spec|
7+
spec.name = "string_attributes"
8+
spec.version = StringAttributes::VERSION
9+
spec.authors = ["syguer"]
10+
spec.email = ["[email protected]"]
11+
12+
spec.summary = %q{String utility for ActiveRecord}
13+
spec.description = %q{Add string utility to model of ActiveRecord}
14+
spec.homepage = "https://github.com/syguer/string_attributes"
15+
spec.license = "MIT"
16+
17+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18+
spec.bindir = "bin"
19+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20+
spec.require_paths = ["lib"]
21+
22+
spec.add_dependency "activerecord", "~> 4.0"
23+
spec.add_dependency "activesupport", "~> 4.0"
24+
25+
spec.add_development_dependency "bundler", "~> 1.10"
26+
spec.add_development_dependency "rake", "~> 10.0"
27+
spec.add_development_dependency "rspec"
28+
spec.add_development_dependency "pry"
29+
end

0 commit comments

Comments
 (0)