accessible_seed_yaml 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +14 -0
- data/.travis.yml +3 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +75 -0
- data/Rakefile +7 -0
- data/accessible_seed_yaml.gemspec +23 -0
- data/lib/accessible_seed_yaml/record.rb +60 -0
- data/lib/accessible_seed_yaml/table.rb +73 -0
- data/lib/accessible_seed_yaml/version.rb +3 -0
- data/lib/accessible_seed_yaml.rb +6 -0
- data/test/accessible_seed/test_record.rb +22 -0
- data/test/accessible_seed/test_table.rb +21 -0
- data/test/test_data.rb +31 -0
- data/test/test_helper.rb +11 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e1c5f8ab0391ca5c7a2ed2d56a5f1dbaa05bbbc2
|
4
|
+
data.tar.gz: 2593fd1b4465c338028b5ab9db9765a4bee4f387
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bbc8e7f9e273be8784ba1c8d0828b66dc37817b4871b745b977a0884a2b7d0bc4ec054b5a2fc3dc9dc0fb8bb8357726edbf65526fac2122986848cb5900c5d87
|
7
|
+
data.tar.gz: 9d3490313fbb241a6c3ed97fe978b567cb5843924fad932245dc8b3b2415f14fca90cbf87d6ca13cb5fbb1e1b545605b42c2743f45fdabd6554a7c1b97bcee4f
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Shinya131
|
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,75 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/Shinya131/accessible_seed_yaml.svg?branch=master)](https://travis-ci.org/Shinya131/accessible_seed_yaml)
|
2
|
+
[![Coverage Status](https://coveralls.io/repos/Shinya131/accessible_seed_yaml/badge.png?branch=develop)](https://coveralls.io/r/Shinya131/accessible_seed_yaml?branch=develop)
|
3
|
+
[![Code Climate](https://codeclimate.com/github/Shinya131/accessible_seed_yaml/badges/gpa.svg)](https://codeclimate.com/github/Shinya131/accessible_seed_yaml)
|
4
|
+
|
5
|
+
## Introduction
|
6
|
+
`AccessibleSeedYaml` is wrapper of rails seed file.
|
7
|
+
This wrapper provides following function:
|
8
|
+
|
9
|
+
1. Easy access interface for seed data.
|
10
|
+
2. Hold original seed data.
|
11
|
+
|
12
|
+
## Example
|
13
|
+
### Sample data
|
14
|
+
numbers.yaml
|
15
|
+
```yaml
|
16
|
+
data1:
|
17
|
+
id: 1
|
18
|
+
name: "one"
|
19
|
+
data2:
|
20
|
+
id: 2
|
21
|
+
name: "two"
|
22
|
+
data3:
|
23
|
+
id: 3
|
24
|
+
name: "three"
|
25
|
+
```
|
26
|
+
|
27
|
+
### Example of `AccessibleSeedYaml::Table`
|
28
|
+
```ruby
|
29
|
+
require 'accessible_seed_yaml'
|
30
|
+
|
31
|
+
original_seed = File.read('numbers.yaml')
|
32
|
+
|
33
|
+
@table = AccessibleSeedYaml::Table.new(original_seed)
|
34
|
+
|
35
|
+
# Instance is providing data access interface.
|
36
|
+
@table.records
|
37
|
+
# =>
|
38
|
+
# [
|
39
|
+
# #<AccessibleSeedYaml::Record:0x007f91552770c0
|
40
|
+
# @original_seed="data1:\n id: 1\n name: \"one\"\n",
|
41
|
+
# @seed_data_by_hash={"data1"=>{"id"=>1, "name"=>"one"}}>,
|
42
|
+
# #<AccessibleSeedYaml::Record:0x007f915526c238
|
43
|
+
# @original_seed="data2:\n id: 2\n name: \"two\"\n",
|
44
|
+
# @seed_data_by_hash={"data2"=>{"id"=>2, "name"=>"two"}}>,
|
45
|
+
# #<AccessibleSeedYaml::Record:0x007f91552662c0
|
46
|
+
# @original_seed="data3:\n id: 3\n name: \"three\"",
|
47
|
+
# @seed_data_by_hash={"data3"=>{"id"=>3, "name"=>"three"}}>
|
48
|
+
# ]
|
49
|
+
|
50
|
+
@table.records[0]
|
51
|
+
# => #<AccessibleSeedYaml::Record:0x007f9155725068
|
52
|
+
# @original_seed="data1:\n id: 1\n name: \"one\"\n",
|
53
|
+
# @seed_data_by_hash={"data1"=>{"id"=>1, "name"=>"one"}}>
|
54
|
+
|
55
|
+
# Instance is holding original seed.
|
56
|
+
@table.original_seed == original_seed
|
57
|
+
# => true
|
58
|
+
```
|
59
|
+
### Example of `AccessibleSeedYaml::Record`
|
60
|
+
```ruby
|
61
|
+
@recoed = @table.records[0]
|
62
|
+
|
63
|
+
@recoed
|
64
|
+
# => #<AccessibleSeedYaml::Record:0x007f9155725068
|
65
|
+
# @original_seed="data1:\n id: 1\n name: \"one\"\n",
|
66
|
+
# @seed_data_by_hash={"data1"=>{"id"=>1, "name"=>"one"}}>
|
67
|
+
|
68
|
+
# Instance is providing data access interface.
|
69
|
+
@recoed.attributes # => {"id"=>1, "name"=>"one"}
|
70
|
+
@recoed.attributes["id"] # => 1
|
71
|
+
@recoed.attributes["name"] # => "one"
|
72
|
+
|
73
|
+
# Instance is holding original seed.
|
74
|
+
@recoed.original_seed # => "data1:\n id: 1\n name: \"one\"\n"
|
75
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'accessible_seed_yaml/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "accessible_seed_yaml"
|
8
|
+
spec.version = AccessibleSeedYaml::VERSION
|
9
|
+
spec.authors = ["Shinya131"]
|
10
|
+
spec.email = ["nagai3mt5b@gmail.com"]
|
11
|
+
spec.summary = "AccessibleSeedYaml is wrapper of rails seed file."
|
12
|
+
spec.description = "AccessibleSeedYaml is wrapper of rails seed file. This wrapper provides following function: 1. Easy access interface for seed data. 2. Hold original seed data."
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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.6"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
module AccessibleSeedYaml
|
3
|
+
# This class is wrapper of seed for one record.
|
4
|
+
# Functions:
|
5
|
+
# - Fetch seed attributes by hash.
|
6
|
+
# - Stored original seed string. It can fetch anytime.
|
7
|
+
class Record
|
8
|
+
attr_reader :original_seed
|
9
|
+
|
10
|
+
# @param [String] seed_for_one_record seed string for one record.
|
11
|
+
# @raise [ArgumentError] if argument has not only one key then raise.
|
12
|
+
# @example
|
13
|
+
# <Correct>
|
14
|
+
# ````
|
15
|
+
# data1:
|
16
|
+
# id: 1
|
17
|
+
# name: "one"
|
18
|
+
# ````
|
19
|
+
# => this is one record.
|
20
|
+
#
|
21
|
+
# <Wrong>
|
22
|
+
# ````
|
23
|
+
# data1:
|
24
|
+
# id: 1
|
25
|
+
# name: "one"
|
26
|
+
# data2:
|
27
|
+
# id: 2
|
28
|
+
# name: "two"
|
29
|
+
# ````
|
30
|
+
# => this is tow record.
|
31
|
+
#
|
32
|
+
def initialize(seed_for_one_record)
|
33
|
+
@original_seed = seed_for_one_record
|
34
|
+
exchange_to_hash
|
35
|
+
end
|
36
|
+
|
37
|
+
# @return [Hash] attributes of seed data by hash.
|
38
|
+
# @example
|
39
|
+
# <source>
|
40
|
+
# data1:
|
41
|
+
# id: 1
|
42
|
+
# name: "one"
|
43
|
+
#
|
44
|
+
# <return>
|
45
|
+
# {"id" => 1, "name" => "one"}
|
46
|
+
def attributes
|
47
|
+
@seed_data_by_hash.values.first
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def exchange_to_hash
|
53
|
+
@seed_data_by_hash ||= YAML.load(@original_seed)
|
54
|
+
|
55
|
+
unless @seed_data_by_hash.size == 1
|
56
|
+
raise ArgumentError.new('original_seed key size is not 1.')
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# This module has seed wrapper classes.
|
2
|
+
# Those wrapper provides following function:
|
3
|
+
# 1. Easy access interface for seed data.
|
4
|
+
# 2. Hold original seed data.
|
5
|
+
module AccessibleSeedYaml
|
6
|
+
# This class is wrapper of seed.
|
7
|
+
# Functions:
|
8
|
+
# - Fetch seed data by record unit.
|
9
|
+
# - Stored original seed string. It can fetch anytime.
|
10
|
+
class Table
|
11
|
+
attr_reader :original_seed
|
12
|
+
|
13
|
+
# @param [String] seed for data source.
|
14
|
+
def initialize(seed)
|
15
|
+
@original_seed = seed
|
16
|
+
end
|
17
|
+
|
18
|
+
# @return [Array of AccessibleSeedYaml::Record] seed data. It split by record unit.
|
19
|
+
# @example
|
20
|
+
#
|
21
|
+
# <source data>
|
22
|
+
# data1:
|
23
|
+
# id: 1
|
24
|
+
# name: "one"
|
25
|
+
# data2:
|
26
|
+
# id: 2
|
27
|
+
# name: "two"
|
28
|
+
#
|
29
|
+
# <return>
|
30
|
+
# [
|
31
|
+
# #<AccessibleSeedYaml::Record:0x007f91552770c0
|
32
|
+
# @original_seed="data1:\n id: 1\n name: \"one\"\n",
|
33
|
+
# @seed_data_by_hash={"data1"=>{"id"=>1, "name"=>"one"}}>,
|
34
|
+
# #<AccessibleSeedYaml::Record:0x007f915526c238
|
35
|
+
# @original_seed="data2:\n id: 2\n name: \"two\"\n",
|
36
|
+
# @seed_data_by_hash={"data2"=>{"id"=>2, "name"=>"two"}}>,
|
37
|
+
# ]
|
38
|
+
def records
|
39
|
+
setup_records if @records.nil?
|
40
|
+
@records
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def setup_records
|
46
|
+
@records = []
|
47
|
+
seed = @original_seed.dup
|
48
|
+
|
49
|
+
loop do
|
50
|
+
if seed.empty?
|
51
|
+
break
|
52
|
+
end
|
53
|
+
|
54
|
+
record = Record.new(fetch_first_record!(seed))
|
55
|
+
@records << record
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def fetch_first_record!(seed)
|
60
|
+
record_boader = seed.index(record_boader_regexp)
|
61
|
+
|
62
|
+
if record_boader.nil?
|
63
|
+
return seed.slice!(0..-1)
|
64
|
+
end
|
65
|
+
|
66
|
+
seed.slice!(0..record_boader)
|
67
|
+
end
|
68
|
+
|
69
|
+
def record_boader_regexp
|
70
|
+
/\n[^ ]/
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
MiniTest::Unit.autorun
|
4
|
+
|
5
|
+
class TestRecord < MiniTest::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@record = AccessibleSeedYaml::Record.new(TestData::Seed::Record01)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_initialize
|
11
|
+
assert_raises(ArgumentError){ AccessibleSeedYaml::Record.new(TestData::Seed::Table) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_original_seed
|
15
|
+
assert_equal(TestData::Seed::Record01, @record.original_seed)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_records
|
19
|
+
assert_equal(1, @record.attributes["id"])
|
20
|
+
assert_equal("coffee", @record.attributes["name"])
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
MiniTest::Unit.autorun
|
4
|
+
|
5
|
+
class TestTable < MiniTest::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@seed = AccessibleSeedYaml::Table.new(TestData::Seed::Table)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_original_seed
|
11
|
+
assert_equal(TestData::Seed::Table, @seed.original_seed)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_records
|
15
|
+
assert_instance_of(AccessibleSeedYaml::Record, @seed.records.first)
|
16
|
+
|
17
|
+
assert_equal(TestData::Seed::Record01, @seed.records[0].original_seed)
|
18
|
+
assert_equal(TestData::Seed::Record02, @seed.records[1].original_seed)
|
19
|
+
assert_equal(TestData::Seed::Record03, @seed.records[2].original_seed)
|
20
|
+
end
|
21
|
+
end
|
data/test/test_data.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module TestData
|
2
|
+
module Seed
|
3
|
+
Record01 =
|
4
|
+
[
|
5
|
+
"data1:\n",
|
6
|
+
" id: 1\n",
|
7
|
+
" name: \"coffee\"\n"
|
8
|
+
].join
|
9
|
+
|
10
|
+
Record02 =
|
11
|
+
[
|
12
|
+
"data2:\n",
|
13
|
+
" id: 2\n",
|
14
|
+
" name: \"tea\"\n"
|
15
|
+
].join
|
16
|
+
|
17
|
+
Record03 =
|
18
|
+
[
|
19
|
+
"data3:\n",
|
20
|
+
" id: 3\n",
|
21
|
+
" name: \"water\"\n"
|
22
|
+
].join
|
23
|
+
|
24
|
+
Table =
|
25
|
+
[
|
26
|
+
Record01,
|
27
|
+
Record02,
|
28
|
+
Record03
|
29
|
+
].join
|
30
|
+
end
|
31
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: accessible_seed_yaml
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shinya131
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-24 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: 'AccessibleSeedYaml is wrapper of rails seed file. This wrapper provides
|
42
|
+
following function: 1. Easy access interface for seed data. 2. Hold original seed
|
43
|
+
data.'
|
44
|
+
email:
|
45
|
+
- nagai3mt5b@gmail.com
|
46
|
+
executables: []
|
47
|
+
extensions: []
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- ".coveralls.yml"
|
51
|
+
- ".gitignore"
|
52
|
+
- ".travis.yml"
|
53
|
+
- Gemfile
|
54
|
+
- LICENSE.txt
|
55
|
+
- README.md
|
56
|
+
- Rakefile
|
57
|
+
- accessible_seed_yaml.gemspec
|
58
|
+
- lib/accessible_seed_yaml.rb
|
59
|
+
- lib/accessible_seed_yaml/record.rb
|
60
|
+
- lib/accessible_seed_yaml/table.rb
|
61
|
+
- lib/accessible_seed_yaml/version.rb
|
62
|
+
- test/accessible_seed/test_record.rb
|
63
|
+
- test/accessible_seed/test_table.rb
|
64
|
+
- test/test_data.rb
|
65
|
+
- test/test_helper.rb
|
66
|
+
homepage: ''
|
67
|
+
licenses:
|
68
|
+
- MIT
|
69
|
+
metadata: {}
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
require_paths:
|
73
|
+
- lib
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 2.2.0
|
87
|
+
signing_key:
|
88
|
+
specification_version: 4
|
89
|
+
summary: AccessibleSeedYaml is wrapper of rails seed file.
|
90
|
+
test_files:
|
91
|
+
- test/accessible_seed/test_record.rb
|
92
|
+
- test/accessible_seed/test_table.rb
|
93
|
+
- test/test_data.rb
|
94
|
+
- test/test_helper.rb
|
95
|
+
has_rdoc:
|