activerecord-bracket 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +4 -0
- data/README.md +71 -0
- data/Rakefile +3 -0
- data/activerecord-bracket.gemspec +25 -0
- data/lib/activerecord-bracket.rb +10 -0
- data/lib/activerecord-bracket/method.rb +12 -0
- data/lib/activerecord-bracket/version.rb +5 -0
- data/spec/activerecord/bracket_spec.rb +29 -0
- data/spec/spec_helper.rb +4 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZDEwNjEyYmM3MWI0YTM5ZTQzZjdhMGIzYzFiOGNmNmU1ZDk3NGQ4MA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZjM3NzAzNzgwZjc0ZGM1Mzk5OWJhM2VlNzFhZmE1YmM2MjBiZDQwNQ==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MWUzNWM2YjRiMDRkN2Y4OTBlMzE4ZTI0ZjNhYjM2OWZhYzFhMDRmYjlhNDUy
|
10
|
+
OTcxOTk2NDcwYThjZWRjNTUwNTgyNDNiMDJjNzMxNjZjNjRiMTVmN2YxNDRi
|
11
|
+
ZDkxYTVmZTRmYzAyYmJjNGU3M2U3MGIzOGU0NDY4MDY1YjZhYjM=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NjAzZDcwNDliMGQ2N2E1YTU5ZWE1ZmMxZWFhYjExM2QwNjkyNGJkYzhlZDcy
|
14
|
+
ZDUyYjNlYjM2MGM1YWRlZWE2MWE1ZTQzYTJkMThjODlhMTVjYjBjYWIyNDQy
|
15
|
+
ZGJjMmY0NGI5OWVmZWJiNGI0NTY0Y2JmMmViNjAzNmJkMTFjOTg=
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# ActiveRecord::Bracket [![Build Status](https://travis-ci.org/zonuexe/ruby-activerecord-bracket.png?branch=develop)](https://travis-ci.org/zonuexe/ruby-activerecord-bracket) [![Code Climate](https://codeclimate.com/github/zonuexe/ruby-activerecord-bracket.png)](https://codeclimate.com/github/zonuexe/ruby-activerecord-bracket) [![Dependency Status](https://gemnasium.com/zonuexe/ruby-activerecord-bracket.png)](https://gemnasium.com/zonuexe/ruby-activerecord-bracket)
|
2
|
+
|
3
|
+
Provide bracket-method `[]` to index-like access for ActiveRecord Class. It method is a syntax-sugar of `find_by_slug` (dynamic-finder of ActiveRecord).
|
4
|
+
|
5
|
+
## Requirement
|
6
|
+
|
7
|
+
* **Ruby** (1.9+, 2.0)
|
8
|
+
* Ruby on Rails (3.x)
|
9
|
+
* Not yet supported rails 4
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'activerecord-bracket'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install activerecord-bracket
|
24
|
+
|
25
|
+
## Introduction
|
26
|
+
|
27
|
+
*(Japanese)*
|
28
|
+
|
29
|
+
* **ID** とは *別の* 唯一の識別子を持つテーブルを前提とします (ex. Twitter)
|
30
|
+
* Twitterのアカウントには、作成時に任意で選択する変更可能な **Username** (一般に `@zonu_exe` のようにユーザを識別するために使用される) と、変更不可能でアカウント作成時に自動で付番される(10進整数のみからなる) **ID** が存在します
|
31
|
+
* **slug** とは、文字集合が概ねASCIIからなる、平易なURIとして *期待される* 文字列のことです
|
32
|
+
* この用語はWordPressから輸入しました。実際にはWordPressではASCIIのみといった制限はありませんし、 `ActiveRecord::Bracket` においてもその制限はModelのvalidationのみに依存します
|
33
|
+
|
34
|
+
### Setup
|
35
|
+
|
36
|
+
*(Japanese)*
|
37
|
+
|
38
|
+
* ActiveRecordを継承したモデルに `slug` 列を用意します (**カラム名の制限は撤廃予定です**)
|
39
|
+
* この列は **uniqueness** であることが望ましいです (必須ではありませんが、 `find_by_slug` の構文糖でしかないことに留意しないと想定外の挙動をするおそれがある)
|
40
|
+
|
41
|
+
## Usage
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
# define system supported language
|
45
|
+
Language.create(name: 'Japanese', slug: 'ja')
|
46
|
+
Language.create(name: 'English' , slug: 'en')
|
47
|
+
|
48
|
+
# get Language instance object
|
49
|
+
ja = Language[:ja] #=> #<Language id: 1, name: "Japanese", slug: "ja">
|
50
|
+
en = Language[:en] #=> #<Language id: 2, name: "English", slug: "en">
|
51
|
+
|
52
|
+
# filter non-supported languages
|
53
|
+
ko = Language[:ko] #=> nil
|
54
|
+
fr = Language[:fr] #=> nil
|
55
|
+
|
56
|
+
# sanitize
|
57
|
+
errorkeystr = 'kimiha jitsuni baka dana'
|
58
|
+
_ = Language[errorkeystr] #=> nil
|
59
|
+
```
|
60
|
+
|
61
|
+
*(Japanese)*
|
62
|
+
|
63
|
+
* `Model.[]` の引数は `String`, `Symbol` のどちらでもActiveRecordがよしなに扱ってくれます。たぶん。
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
|
67
|
+
1. Fork it
|
68
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
69
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
70
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
71
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'activerecord-bracket/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'activerecord-bracket'
|
8
|
+
spec.version = ActiveRecord::Bracket::VERSION
|
9
|
+
spec.authors = ['USAMI Kenta']
|
10
|
+
spec.email = ['tadsan@zonu.me']
|
11
|
+
spec.description = %q{Provide bracket-method `[]` for ActiveRecord Class}
|
12
|
+
spec.summary = %q{}
|
13
|
+
spec.homepage = 'http://dt.zonu.me'
|
14
|
+
spec.license = 'GPLv3 or NYSL'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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
|
+
spec.add_runtime_dependency 'activerecord'
|
21
|
+
spec.add_runtime_dependency 'activesupport'
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
spec.add_development_dependency 'rspec'
|
25
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# Implement of [] method
|
2
|
+
module ActiveRecord::Bracket::Method
|
3
|
+
extend ::ActiveSupport::Concern
|
4
|
+
|
5
|
+
# class methods for ActiveRecord
|
6
|
+
module ClassMethods
|
7
|
+
# [] method for ActiveRecord 3.x (using dynamic finder)
|
8
|
+
def [] (key)
|
9
|
+
self.send(:find_by_slug, key)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe ActiveRecord::Bracket do
|
4
|
+
context "sample class" do
|
5
|
+
let(:sample_class){
|
6
|
+
# pseudo-activerecord
|
7
|
+
Class.new do
|
8
|
+
include ActiveRecord::Bracket::Method
|
9
|
+
attr_accessor :name, :slug
|
10
|
+
def initialize (arg)
|
11
|
+
@name = arg[:name]
|
12
|
+
@slug = arg[:slug]
|
13
|
+
self.class.instances[@slug.intern] = self
|
14
|
+
end
|
15
|
+
def == (other)
|
16
|
+
[self.name.to_s, self.slug.to_s] == [other.name.to_s, other.slug.to_s]
|
17
|
+
end
|
18
|
+
def self.find_by_slug (key)
|
19
|
+
instances[key.intern]
|
20
|
+
end
|
21
|
+
def self.instances; @@instances ||= {} ; end
|
22
|
+
end
|
23
|
+
}
|
24
|
+
before(:each){ @sample = sample_class.new(name: "Foo", slug: "foo") }
|
25
|
+
it{ expect(sample_class.find_by_slug(:foo)).to eq @sample }
|
26
|
+
it{ expect(sample_class[:foo]).to eq @sample }
|
27
|
+
it{ expect(sample_class[:foo]).to eq sample_class.find_by_slug(:foo) }
|
28
|
+
end
|
29
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-bracket
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- USAMI Kenta
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-03-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Provide bracket-method `[]` for ActiveRecord Class
|
84
|
+
email:
|
85
|
+
- tadsan@zonu.me
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- .rspec
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- activerecord-bracket.gemspec
|
97
|
+
- lib/activerecord-bracket.rb
|
98
|
+
- lib/activerecord-bracket/method.rb
|
99
|
+
- lib/activerecord-bracket/version.rb
|
100
|
+
- spec/activerecord/bracket_spec.rb
|
101
|
+
- spec/spec_helper.rb
|
102
|
+
homepage: http://dt.zonu.me
|
103
|
+
licenses:
|
104
|
+
- GPLv3 or NYSL
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ! '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.0.3
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: ''
|
126
|
+
test_files:
|
127
|
+
- spec/activerecord/bracket_spec.rb
|
128
|
+
- spec/spec_helper.rb
|