ninja-collection 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/Guardfile +9 -0
- data/LICENSE.txt +22 -0
- data/README.md +59 -0
- data/Rakefile +8 -0
- data/lib/ninja/hash.rb +57 -0
- data/lib/ninja/version.rb +3 -0
- data/lib/ninja.rb +1 -0
- data/ninja.gemspec +27 -0
- data/spec/ninja/hash_spec.rb +83 -0
- data/spec/spec_helper.rb +2 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 04e0fd97008f001d4cd138d39d0536b1358057d4
|
4
|
+
data.tar.gz: 0283d7850f56cf449e68a6d23c8535ca1c588afe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9ffb0e286bfd15982db2c31fd88d13f34b7ec3ae6fc0f2e4838183ef05102aa38029a8f62ee4078cadf1f15ad429740c3cde5f036cb45229f106d6004e7af492
|
7
|
+
data.tar.gz: a2693947bf1e325d1c16e9042af78e416f593d6a218eb750fb02ec6d578f91c4c865ef15a0defbe75605492f58f70ea59823d47f4d31e91e89b05ba898aecf51
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard :rspec, cli: "--color --format nested --fail-fast" do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
end
|
9
|
+
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 yagince
|
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,59 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/yagince/ninja.svg?branch=master)](https://travis-ci.org/yagince/ninja)
|
2
|
+
|
3
|
+
# Ninja
|
4
|
+
|
5
|
+
the library behind the scenes
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'ninja', github: "myfoot/ninja"
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install specific_install
|
20
|
+
$ gem specific_install -l 'git://github.com/myfoot/ninja.git'
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
### Ninja::Hash
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require 'ninja/hash'
|
28
|
+
|
29
|
+
h = Ninja::Hash.new(hoge: 1, foo: 2)
|
30
|
+
h.hoge # => 1
|
31
|
+
h.foo # => 2
|
32
|
+
h.piyo # => nil
|
33
|
+
|
34
|
+
h.bar = 100
|
35
|
+
h.bar # => 100
|
36
|
+
|
37
|
+
h = {hoge: 1, foo: 2}.to_ninja_hash
|
38
|
+
h.hoge # => 1
|
39
|
+
h.foo # => 2
|
40
|
+
|
41
|
+
h_ninja = {hoge: 1, foo: 2}.ninja
|
42
|
+
h_ninja.hoge # => 1
|
43
|
+
h_ninja.foo # => 2
|
44
|
+
|
45
|
+
h = {hoge: 1, foo: { bar: 2 }}.ninja
|
46
|
+
h.foo.bar # => 2
|
47
|
+
|
48
|
+
# default value
|
49
|
+
h.get(:piyo) { 1 + 2 } # => 3
|
50
|
+
h.get(:piyo) # => nil
|
51
|
+
```
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
1. Fork it
|
56
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
57
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
58
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
59
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/ninja/hash.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
module Ninja
|
2
|
+
class Hash < Hash
|
3
|
+
def initialize hash
|
4
|
+
self.replace hash
|
5
|
+
hash.keys.each{|key| define(key) }
|
6
|
+
end
|
7
|
+
|
8
|
+
def []=(key, value)
|
9
|
+
super(key, value)
|
10
|
+
define(key)
|
11
|
+
end
|
12
|
+
|
13
|
+
def get(key, &block)
|
14
|
+
unless value = self.send(key)
|
15
|
+
block && block.call
|
16
|
+
else
|
17
|
+
value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def method_missing(name, *args)
|
22
|
+
if md = name.to_s.match(/(.+)=\z/)
|
23
|
+
define(md[1])
|
24
|
+
self.send(name, *args)
|
25
|
+
end
|
26
|
+
nil
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def define key
|
31
|
+
define_singleton_method(key) {
|
32
|
+
value = case key
|
33
|
+
when String
|
34
|
+
(self[key] || self[key.to_sym])
|
35
|
+
else
|
36
|
+
(self[key] || self[key.to_s])
|
37
|
+
end
|
38
|
+
if value.is_a?(::Hash)
|
39
|
+
value = Ninja::Hash.new(value)
|
40
|
+
self[key] = value
|
41
|
+
end
|
42
|
+
value
|
43
|
+
}
|
44
|
+
|
45
|
+
define_singleton_method("#{key}=") {|value|
|
46
|
+
self[key] = value
|
47
|
+
}
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class Hash
|
53
|
+
def to_ninja_hash
|
54
|
+
Ninja::Hash.new(self)
|
55
|
+
end
|
56
|
+
alias :ninja :to_ninja_hash
|
57
|
+
end
|
data/lib/ninja.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "ninja/version"
|
data/ninja.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ninja/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ninja-collection"
|
8
|
+
spec.version = Ninja::VERSION
|
9
|
+
spec.authors = ["yagince"]
|
10
|
+
spec.email = ["straitwalk@gmail.com"]
|
11
|
+
spec.description = %q{the library behind the scenes}
|
12
|
+
spec.summary = %q{behind the scenes}
|
13
|
+
spec.homepage = "https://github.com/yagince/ninja"
|
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"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "rb-fsevent"
|
25
|
+
spec.add_development_dependency "guard-rspec"
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'ninja/hash'
|
4
|
+
|
5
|
+
describe Ninja::Hash do
|
6
|
+
subject{ Ninja::Hash.new(a:1, "z" => 100, h: {a: 1, b: 2}) }
|
7
|
+
|
8
|
+
context "指定したキーが存在する場合" do
|
9
|
+
it "プロパティのようにキーを指定できる" do
|
10
|
+
expect(subject.a).to eq(1)
|
11
|
+
expect(subject.z).to eq(100)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "指定したキーが存在しない場合" do
|
16
|
+
it { expect(subject.c).to be_nil }
|
17
|
+
|
18
|
+
it "プロパティのように代入できる" do
|
19
|
+
hash = subject
|
20
|
+
value = 100
|
21
|
+
hash.c = value
|
22
|
+
expect(hash.c).to eq(value)
|
23
|
+
expect(hash["c"]).to eq(value)
|
24
|
+
expect(hash[:c]).to be_nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "エントリーを追加した場合" do
|
29
|
+
it "プロパティのようにアクセスできる" do
|
30
|
+
subject[:b] = 100
|
31
|
+
expect(subject.b).to eq(100)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "hashと同じように振る舞う" do
|
35
|
+
subject[:foo] = 200
|
36
|
+
subject[:bar] = 300
|
37
|
+
expect(subject.keys).to eq([:a, "z", :h, :foo, :bar])
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context "valueがHashの場合" do
|
42
|
+
it "valueもninja" do
|
43
|
+
expect(subject.h.a).to eq(1)
|
44
|
+
end
|
45
|
+
|
46
|
+
context "valueを破壊的に変更した場合" do
|
47
|
+
it "元のオブジェクトも変更される" do
|
48
|
+
hash = subject
|
49
|
+
x = hash.h
|
50
|
+
x[:test] = 1000
|
51
|
+
expect(hash.h.test).to eq(1000)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
it "別インスタンスには影響しない" do
|
57
|
+
expect(Ninja::Hash.new(hoge: 1).respond_to?(:a)).to eq(false)
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "#get" do
|
61
|
+
context "キーが存在する場合" do
|
62
|
+
it { expect(subject.get(:a)).to eq(1) }
|
63
|
+
it { expect(subject.get('a')).to eq(1) }
|
64
|
+
end
|
65
|
+
|
66
|
+
context "キーが存在しない場合" do
|
67
|
+
it { expect(subject.get(:hoge)).to be_nil }
|
68
|
+
|
69
|
+
context "ブロックを渡した場合" do
|
70
|
+
it "ブロックの実行結果が返る" do
|
71
|
+
expect(subject.get(:hoge){ 1 + 1 }).to eq(2)
|
72
|
+
expect(subject.get("hoge"){ 1 + 1 }).to eq(2)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe Hash do
|
80
|
+
it :ninja do
|
81
|
+
expect({hoge: 1}.ninja.hoge).to eq(1)
|
82
|
+
end
|
83
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ninja-collection
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- yagince
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-09 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: '0'
|
20
|
+
type: :development
|
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: 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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rb-fsevent
|
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: guard-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: the library behind the scenes
|
84
|
+
email:
|
85
|
+
- straitwalk@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
92
|
+
- Gemfile
|
93
|
+
- Guardfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- lib/ninja.rb
|
98
|
+
- lib/ninja/hash.rb
|
99
|
+
- lib/ninja/version.rb
|
100
|
+
- ninja.gemspec
|
101
|
+
- spec/ninja/hash_spec.rb
|
102
|
+
- spec/spec_helper.rb
|
103
|
+
homepage: https://github.com/yagince/ninja
|
104
|
+
licenses:
|
105
|
+
- MIT
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.2.2
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: behind the scenes
|
127
|
+
test_files:
|
128
|
+
- spec/ninja/hash_spec.rb
|
129
|
+
- spec/spec_helper.rb
|