code_holder 0.0.4

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c16e9e9611d3070766e037b0dab0207771193917
4
+ data.tar.gz: cb4ff1df4a76c3397d5034f478245af082176f85
5
+ SHA512:
6
+ metadata.gz: ebb8e66c73e9d55ab7244752361103c5bc5afebe3515c0367e6c780de5d1bf2fca97961941102253410a682c37d5092beeea1d5b16663d261922ea64465c1815
7
+ data.tar.gz: c47a98a69608a80c490f681738a3aa8c10991392b7a9aee6d87b7ef929bd356124c1f0c02e93236d0d47f5200c0fecb4b99c36b0f1c48730c5af447a1a593c75
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ pkg/*
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2006-2013 RAWHIDE.Co.,Ltd (http://raw-hide.co.jp/)
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,77 @@
1
+ # CodeHolder
2
+
3
+ RAILS_ROOT/config/code_holder 配下にcsvを配置することでEnumを管理することができる。
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'code_holder', github: rawhide/code_holder
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as: (未対応)
16
+
17
+ $ gem install code_holder
18
+
19
+ ## 基本的な使い方
20
+
21
+ ### 例)RAILS_ROOT/config/code_holder/gender.csv
22
+
23
+ title,性別
24
+ #DATA
25
+ key,value,position,name,short_name
26
+ MALE,m,1,男性,男
27
+ FEMALE,f,2,女性,女
28
+
29
+ key,value,position,name 必須
30
+ この後に任意の項目を付け足すことが可能(上記例ではshort_name)
31
+
32
+ > C.gender.MALE => "m"
33
+ > C.gender.MALE.name => "男性"
34
+ > C.gender["m"] => "m"
35
+ > C.gender["m"].name => "男性"
36
+ > C.gender["m"].short_name => "男"
37
+
38
+ ### このセレクトボックスを作りたければ
39
+
40
+ <%= f.select :gender, C.gender.to_opt, :include_blank => "選択してください" %>
41
+
42
+ CSVのpositionカラムの数字を入れ替えることで、セレクトボックスの順番を変えることも可能
43
+
44
+ ### データを表示するには
45
+
46
+ <%= C.gender[user.gender].name %>
47
+
48
+ ## Customizing
49
+
50
+ ### 定義ファイルのディレクトリは、initializerに以下のように書くことで、変更可能
51
+
52
+ ENUMS = Railstar::CodeHolder.new("resources/code")
53
+
54
+ ### 型はintegerのみ指定可能(指定がなければString)
55
+
56
+ title,性別テストデータ
57
+ type,int_val,integer
58
+ #DATA
59
+ key,value,position,name,short_name,int_val
60
+ FEMALE,f,2,女性,女,2
61
+ MALE,m,1,男性,男,1
62
+
63
+ > C.gender.MALE.int_val => 1
64
+ > C.gender.map{|k,v| v.data } => [{:key=>"FEMALE", :value=>"f", :position=>"2", :name=>"女性", :short_name=>"女", :int_val=>"2"}, {:key=>"MALE", :value=>"m", :position=>"1", :name=>"男性", :short_name=>"男", :int_val=>"1"}]
65
+
66
+ ### 任意の項目のセレクトボックスを作成することも可能
67
+
68
+ <%= f.select :gender, C.gender.to_opt(:short_name), :include_blank => "選択してください" %>
69
+ <%= f.select :gender, C.gender.to_opt(:short_name, :int_val), :include_blank => "選択してください" %>
70
+
71
+ ## Contributing
72
+
73
+ 1. Fork it
74
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
75
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
76
+ 4. Push to the branch (`git push origin my-new-feature`)
77
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'code_holder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "code_holder"
8
+ spec.version = CodeHolder::VERSION
9
+ spec.authors = ["RAWHIDE. Co., Ltd."]
10
+ spec.email = ["info@raw-hide.co.jp"]
11
+ spec.description = %q{Enumerated attributes with I18n}
12
+ spec.summary = %q{Enumerated attributes with I18n}
13
+ spec.homepage = "http://raw-hide.co.jp"
14
+ spec.license = "MIT"
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
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,120 @@
1
+ require "csv"
2
+
3
+ module Railstar
4
+ class DuplicateValue < Exception; end
5
+ class DuplicatePosition < Exception; end
6
+ class UndefinedCode < Exception; end
7
+
8
+ class CodeHolder < Hash
9
+ def initialize(code_dir=nil)
10
+ @code_dir = code_dir || "./config/code_holder"
11
+ end
12
+
13
+ def method_missing(name, *args)
14
+ self[name] ||= load(name, *args)
15
+ end
16
+
17
+ def load(name, options={})
18
+ CodeList.new(name, @code_dir)
19
+ end
20
+
21
+ def codes
22
+ dirs = Dir.glob(File.join(@code_dir, "*.csv"))
23
+ files = []
24
+ dirs.each do |dir|
25
+ files << dir.split("/").last
26
+ end
27
+ files.sort.uniq.map{|a| a.sub(/.csv$/, "")}
28
+ end
29
+ end
30
+
31
+ class CodeList < Hash
32
+ def initialize(name, code_dir)
33
+ puts "LOAD code #{name}"
34
+ super(nil)
35
+
36
+ @order = []
37
+ @type = {}
38
+
39
+ mode = "config"
40
+ CSV.foreach(search_file(name, code_dir), :encoding => "UTF-8") do |row|
41
+ if mode == "config"
42
+ if row.first == "type"
43
+ @type[row[1].to_sym] = row[2]
44
+ end
45
+ mode = "header" if row.first == "#DATA"
46
+ elsif mode == "header"
47
+ @headers = row.map(&:to_sym)
48
+ mode = "data"
49
+ elsif mode == "data" && (row.first != "") && (row.first != nil)
50
+ code = Code.new(@headers.zip(row).flatten, @type)
51
+ raise DuplicateValue, code.value if self[code.value]
52
+ self[code.value.to_s] = code
53
+ self.instance_eval <<-EOS
54
+ def #{code.key}
55
+ self["#{code.value}"]
56
+ end
57
+ EOS
58
+ if code.position != "" && code.position != nil
59
+ raise DuplicatePosition if @order[code.position]
60
+ @order[code.position] = code.value
61
+ end
62
+ end
63
+ end
64
+ @order.compact!
65
+ end
66
+
67
+ def headers
68
+ @headers
69
+ end
70
+
71
+ def search_file(name, code_dir)
72
+ files = [ File.join(code_dir, "#{name}.csv") ]
73
+ files.each do |file|
74
+ return file if File.exist?(file)
75
+ end
76
+ raise UndefinedCode, "not found #{files}"
77
+ end
78
+
79
+ def to_opt(column1=:name, column2=nil)
80
+ if column2.nil?
81
+ @order.map{|k| [self[k].data(column1), self[k]]}
82
+ else
83
+ @order.map{|k| [self[k].data(column1), self[k].data(column2)]}
84
+ end
85
+ end
86
+ end
87
+
88
+ class Code < String
89
+ def initialize(args, type={})
90
+ @data = Hash[*args]
91
+ @type = type
92
+ super(@data[:value])
93
+ end
94
+
95
+ def data(key=nil)
96
+ return @data if key.nil?
97
+ case @type[key]
98
+ when "integer"
99
+ @data[key.to_sym].to_i
100
+ else
101
+ @data[key.to_sym]
102
+ end
103
+ end
104
+
105
+ def position
106
+ @data[:position].to_i
107
+ end
108
+
109
+ def method_missing(name, *args)
110
+ self.data(name)
111
+ end
112
+
113
+ def value
114
+ self.data(:value)
115
+ end
116
+ end
117
+ end
118
+
119
+ C = Railstar::CodeHolder.new
120
+
@@ -0,0 +1,3 @@
1
+ module CodeHolder
2
+ VERSION = "0.0.4"
3
+ end
@@ -0,0 +1,69 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'spec_helper'
3
+
4
+ describe "CodeHolder" do
5
+ describe "load" do
6
+ it "できる" do
7
+ C_TEST.gender.map{|k,v| v.data }.should == [{:key=>"FEMALE", :value=>"f", :position=>"2", :name=>"女性", :short_name=>"女", :int_val=>"2"}, {:key=>"MALE", :value=>"m", :position=>"1", :name=>"男性", :short_name=>"男", :int_val=>"1"}]
8
+ end
9
+ end
10
+
11
+ describe "key名" do
12
+ it "を渡すと、その行のvalueが返る" do
13
+ C_TEST.gender.FEMALE.should == "f"
14
+ end
15
+
16
+ it ".nameとすると、表示名が表示される" do
17
+ C_TEST.gender.FEMALE.name.should == "女性"
18
+ end
19
+
20
+ it ".column名とすると、指定した列の値が表示される" do
21
+ C_TEST.gender.FEMALE.short_name.should == "女"
22
+ end
23
+
24
+ it ".positionとすると、ポジションがintegerで返る" do
25
+ C_TEST.gender.FEMALE.position.should == 2
26
+ end
27
+
28
+ it "type,column,integer を宣言しておくとintegerで返る" do
29
+ C_TEST.gender.FEMALE.int_val.should == 2
30
+ end
31
+ end
32
+
33
+ describe "[value]" do
34
+ it "を渡すと、その行のvalueが返る" do
35
+ C_TEST.gender["m"].should == "m"
36
+ end
37
+
38
+ it ".nameとすると、表示名が表示される" do
39
+ C_TEST.gender["m"].name.should == "男性"
40
+ end
41
+
42
+ it ".column名とすると、指定した列の値が返る" do
43
+ C_TEST.gender["m"].short_name.should == "男"
44
+ end
45
+
46
+ it ".positionとすると、ポジションがintegerで返る" do
47
+ C_TEST.gender["m"].position.should == 1
48
+ end
49
+
50
+ it "type,column,integer を宣言しておくとintegerで返る" do
51
+ C_TEST.gender["m"].int_val.should == 1
52
+ end
53
+
54
+ end
55
+
56
+ describe ".to_opt" do
57
+ it "とすると[[name,value],...]の形でposition順の配列が返る" do
58
+ C_TEST.gender.to_opt.should == [["男性","m"],["女性","f"]]
59
+ end
60
+
61
+ it "(column1)とすると、指定列の配列が返る" do
62
+ C_TEST.gender.to_opt(:short_name).should == [["男", "m"], ["女", "f"]]
63
+ end
64
+
65
+ it "(column1, column2)とすると、指定列の配列が返る" do
66
+ C_TEST.gender.to_opt(:name, :int_val).should == [["男性", 1], ["女性", 2]]
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,4 @@
1
+ require 'rspec'
2
+ require './lib/code_holder'
3
+
4
+ C_TEST = Railstar::CodeHolder.new("./spec/test_data/code")
@@ -0,0 +1,6 @@
1
+ title,性別テストデータ
2
+ type,int_val,integer
3
+ #DATA
4
+ key,value,position,name,short_name,int_val
5
+ FEMALE,f,2,女性,女,2
6
+ MALE,m,1,男性,男,1
@@ -0,0 +1,52 @@
1
+ title,テストCSVファイル
2
+ type,jis,integer
3
+ #DATA
4
+ key,value,position,jis,name,full_name
5
+ HOKKAIDO,hokkaido,1,1,北海道,北海道
6
+ AOMORI,aomori,2,2,青森,青森県
7
+ IWATE,iwate,3,3,岩手,岩手県
8
+ MIYAGI,miyagi,4,4,宮城,宮城県
9
+ AKITA,akita,5,5,秋田,秋田県
10
+ YAMAGATA,yamagata,6,6,山形,山形県
11
+ FUKUSHIMA,fukushima,7,7,福島,福島県
12
+ IBARAKI,ibaraki,8,8,茨城,茨城県
13
+ TOCHIGI,tochigi,9,9,栃木,栃木県
14
+ GUNMA,gunma,10,10,群馬,群馬県
15
+ SAITAMA,saitama,11,11,埼玉,埼玉県
16
+ CHIBA,chiba,12,12,千葉,千葉県
17
+ TOKYO,tokyo,13,13,東京,東京都
18
+ KANAGAWA,kanagawa,14,14,神奈川,神奈川県
19
+ NIIGATA,niigata,15,15,新潟,新潟県
20
+ TOYAMA,toyama,16,16,富山,富山県
21
+ ISHIKAWA,ishikawa,17,17,石川,石川県
22
+ FUKUI,fukui,18,18,福井,福井県
23
+ YAMANASHI,yamanashi,19,19,山梨,山梨県
24
+ NAGANO,nagano,20,20,長野,長野県
25
+ GIFU,gifu,21,21,岐阜,岐阜県
26
+ SHIZUOKA,shizuoka,22,22,静岡,静岡県
27
+ AICHI,aichi,23,23,愛知,愛知県
28
+ MIE,mie,24,24,三重,三重県
29
+ SHIGA,shiga,25,25,滋賀,滋賀県
30
+ KYOTO,kyoto,26,26,京都,京都府
31
+ OSAKA,osaka,27,27,大阪,大阪府
32
+ HYOGO,hyogo,28,28,兵庫,兵庫県
33
+ NARA,nara,29,29,奈良,奈良県
34
+ WAKAYAMA,wakayama,30,30,和歌山,和歌山県
35
+ TOTTORI,tottori,31,31,鳥取,鳥取県
36
+ SHIMANE,shimane,32,32,島根,島根県
37
+ OKAYAMA,okayama,33,33,岡山,岡山県
38
+ HIROSHIMA,hiroshima,34,34,広島,広島県
39
+ YAMAGUCHI,yamaguchi,35,35,山口,山口県
40
+ TOKUSHIMA,tokushima,36,36,徳島,徳島県
41
+ KAGAWA,kagawa,37,37,香川,香川県
42
+ EHIME,ehime,38,38,愛媛,愛媛県
43
+ KOUCHI,kochi,39,39,高知,高知県
44
+ FUKUOKA,fukuoka,40,40,福岡,福岡県
45
+ SAGA,saga,41,41,佐賀,佐賀県
46
+ NAGASAKI,nagasaki,42,42,長崎,長崎県
47
+ KUMAMOTO,kumamoto,43,43,熊本,熊本県
48
+ OITA,oita,44,44,大分,大分県
49
+ MIYAZAKI,miyazaki,45,45,宮崎,宮崎県
50
+ KAGOSHIMA,kagoshima,46,46,鹿児島,鹿児島県
51
+ OKINAWA,okinawa,47,47,沖縄,沖縄県
52
+
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: code_holder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - RAWHIDE. Co., Ltd.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Enumerated attributes with I18n
56
+ email:
57
+ - info@raw-hide.co.jp
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - code_holder.gemspec
68
+ - lib/code_holder.rb
69
+ - lib/code_holder/version.rb
70
+ - spec/code_holder_spec.rb
71
+ - spec/spec_helper.rb
72
+ - spec/test_data/code/gender.csv
73
+ - spec/test_data/code/prefecture.csv
74
+ homepage: http://raw-hide.co.jp
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.0.0
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Enumerated attributes with I18n
98
+ test_files:
99
+ - spec/code_holder_spec.rb
100
+ - spec/spec_helper.rb
101
+ - spec/test_data/code/gender.csv
102
+ - spec/test_data/code/prefecture.csv