kori 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/Gemfile +19 -0
- data/LICENSE.txt +21 -0
- data/README.md +42 -0
- data/Rakefile +10 -0
- data/bin/console +10 -0
- data/bin/setup +7 -0
- data/kori.gemspec +26 -0
- data/lib/kori.rb +104 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9ccd3d346805c0b8a15b20049961683f91e36937
|
4
|
+
data.tar.gz: 44bd3c4d47a1171aeeb8297c4a5950112016a390
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5efc4afe173d503fc495e8fdf127a524eafe7dcae014ba83d9bc6f395ca1e94286ff5ce5d5fb3e2692a79a58cac3670c71c96bd8fd73a0dee7d3e4cfe07df9eb
|
7
|
+
data.tar.gz: 085b84e744a823e9330f1fa923453388d811f27bdf755954d74fdd7bf3e67533ff786bffe94f98f2c5432d00da6b4b3ef6c4edb4f03247ed15a01db79bb35839
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
group :development, :test do
|
4
|
+
gem 'pry'
|
5
|
+
gem 'pry-doc'
|
6
|
+
gem 'pry-byebug'
|
7
|
+
gem 'pry-power_assert'
|
8
|
+
end
|
9
|
+
|
10
|
+
group :test do
|
11
|
+
gem 'minitest'
|
12
|
+
gem 'minitest-around'
|
13
|
+
gem 'minitest-power_assert'
|
14
|
+
gem 'minitest-stub_any_instance'
|
15
|
+
gem 'minitest-reporters'
|
16
|
+
end
|
17
|
+
|
18
|
+
# Specify your gem's dependencies in kori.gemspec
|
19
|
+
gemspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 nalabjp
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Kori
|
2
|
+
|
3
|
+
Kori generate deep frozen objects from yaml or hash.
|
4
|
+
It using a [ice_nine](https://github.com/dkubb/ice_nine), inspired by [Settingslogic](https://github.com/settingslogic/settingslogic).
|
5
|
+
|
6
|
+
Kori(kōri) means Ice in japanese.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
$ gem install kori
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
require 'kori'
|
15
|
+
|
16
|
+
# From Hash
|
17
|
+
config = Kori.create({a: 1, b: { c: 'abc' } })
|
18
|
+
config.a # 1
|
19
|
+
config[:a] # 1
|
20
|
+
config['a'] # 1
|
21
|
+
config.b # { c: 'abc' }
|
22
|
+
config.b.class # Kori
|
23
|
+
config.b.c # 'abc'
|
24
|
+
config.forzen? # true
|
25
|
+
config.a.frozen? # true
|
26
|
+
config.b.frozen? # true
|
27
|
+
config.b.c.frozen? # true
|
28
|
+
|
29
|
+
# From YAML
|
30
|
+
config = Kori.create('app_config.yml') # load from ./app_config.yml
|
31
|
+
config = Kori.create('/path/to/app_config.yml') # load from /path/to/app_config.yml
|
32
|
+
|
33
|
+
# If you are using Rails, it is possible to use as `Rails.application.config_for`
|
34
|
+
config = Kori.create(:app_config) # load from /rails_root/config/app_config.yml
|
35
|
+
config = Kori.create('subdir/app_config') # load from /rails_root/config/subdir/app_config.yml
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
MIT License
|
40
|
+
|
41
|
+
see [LICENSE.txt](https://github.com/nalabjp/kori/blob/master/LICENSE.txt).
|
42
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/kori.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'kori'
|
7
|
+
spec.version = '0.1.0'
|
8
|
+
spec.authors = ['nalabjp']
|
9
|
+
spec.email = ['nalabjp@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = %q{Deep frozen object from yaml or hash.}
|
12
|
+
spec.description = %q{Deep frozen object from yaml or hash.}
|
13
|
+
spec.homepage = 'https://github.com/nalabjp/kori'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = 'exe'
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'ice_nine', '~> 0.11'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
25
|
+
spec.add_development_dependency 'minitest'
|
26
|
+
end
|
data/lib/kori.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'yaml'
|
3
|
+
require 'ice_nine'
|
4
|
+
|
5
|
+
class Kori < Hash
|
6
|
+
private_class_method :new
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def create(file_or_hash)
|
10
|
+
IceNine.deep_freeze(new(file_or_hash))
|
11
|
+
end
|
12
|
+
|
13
|
+
undef_method :[]
|
14
|
+
end
|
15
|
+
|
16
|
+
def get(key)
|
17
|
+
parts = key.split('.')
|
18
|
+
curs = self
|
19
|
+
while p = parts.shift
|
20
|
+
curs = curs.__send__(p)
|
21
|
+
end
|
22
|
+
curs
|
23
|
+
end
|
24
|
+
|
25
|
+
def [](key)
|
26
|
+
fetch(key, nil)
|
27
|
+
end
|
28
|
+
|
29
|
+
def fetch(*args)
|
30
|
+
arity = args.size
|
31
|
+
raise ArgumentError, "invalid number of elements (#{arity} for 1..2)" unless arity.between?(1, 2)
|
32
|
+
|
33
|
+
key, val = args
|
34
|
+
if respond_to?(key.to_s)
|
35
|
+
__send__(key.to_s)
|
36
|
+
elsif arity == 2
|
37
|
+
val
|
38
|
+
else
|
39
|
+
raise KeyError, "key '#{key}' not found"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def initialize(file_or_hash)
|
46
|
+
case file_or_hash
|
47
|
+
when Hash
|
48
|
+
replace(file_or_hash)
|
49
|
+
when String, Symbol
|
50
|
+
replace(load_yaml(file_or_hash))
|
51
|
+
else
|
52
|
+
raise "Could not load configuration. No such file - #{file_or_hash}"
|
53
|
+
end
|
54
|
+
|
55
|
+
create_accessors!
|
56
|
+
end
|
57
|
+
|
58
|
+
def load_yaml(file)
|
59
|
+
on_rails = !!defined?(Rails)
|
60
|
+
|
61
|
+
file = if on_rails
|
62
|
+
"#{rails_config_path}/#{file}.yml"
|
63
|
+
elsif file.is_a?(Symbol)
|
64
|
+
file.to_s.concat('.yml')
|
65
|
+
else
|
66
|
+
file
|
67
|
+
end
|
68
|
+
|
69
|
+
config = YAML.load(ERB.new(File.read(file)).result)
|
70
|
+
config = config[Rails.env] if on_rails
|
71
|
+
config
|
72
|
+
end
|
73
|
+
|
74
|
+
def rails_config_path
|
75
|
+
Rails.application.paths['config'].existent.first
|
76
|
+
end
|
77
|
+
|
78
|
+
def create_accessors!
|
79
|
+
self.each do |key, val|
|
80
|
+
value = type_cast_if_hash(val)
|
81
|
+
create_accessor_for(key, value)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def create_accessor_for(key, val)
|
86
|
+
return unless key.to_s =~ /^\w+\z/
|
87
|
+
instance_variable_set("@#{key}", val)
|
88
|
+
self.class.class_eval <<-EOCODE, __FILE__, __LINE__ + 1
|
89
|
+
def #{key}
|
90
|
+
@#{key}
|
91
|
+
end
|
92
|
+
EOCODE
|
93
|
+
end
|
94
|
+
|
95
|
+
def type_cast_if_hash(value)
|
96
|
+
if value.is_a?(Hash)
|
97
|
+
self.class.__send__(:new, value)
|
98
|
+
elsif value.is_a?(Array)
|
99
|
+
value.map { |item| type_cast_if_hash(item) }
|
100
|
+
else
|
101
|
+
value
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kori
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- nalabjp
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ice_nine
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.11'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
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
|
+
description: Deep frozen object from yaml or hash.
|
70
|
+
email:
|
71
|
+
- nalabjp@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- kori.gemspec
|
85
|
+
- lib/kori.rb
|
86
|
+
homepage: https://github.com/nalabjp/kori
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.4.5
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Deep frozen object from yaml or hash.
|
110
|
+
test_files: []
|
111
|
+
has_rdoc:
|