inde_struct 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +6 -0
- data/LICENSE +22 -0
- data/Manifest.txt +16 -0
- data/README.md +123 -0
- data/Rakefile +21 -0
- data/bin/inde_struct +2 -0
- data/lib/inde_struct.rb +14 -0
- data/lib/inde_struct/parser.rb +24 -0
- data/lib/inde_struct/parser/hash_any_case.rb +15 -0
- data/lib/inde_struct/parser/open_struct_any_case.rb +21 -0
- data/lib/inde_struct/reader.rb +26 -0
- data/lib/inde_struct/version.rb +3 -0
- data/test/files/config.yml +2 -0
- data/test/files/empty.yml +0 -0
- data/test/test_inde_struct.rb +50 -0
- metadata +96 -0
data/History.txt
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Step1Profit
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/Manifest.txt
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
History.txt
|
2
|
+
LICENSE
|
3
|
+
Manifest.txt
|
4
|
+
README.md
|
5
|
+
Rakefile
|
6
|
+
bin/inde_struct
|
7
|
+
inde_struct.gemspec
|
8
|
+
lib/inde_struct.rb
|
9
|
+
lib/inde_struct/parser.rb
|
10
|
+
lib/inde_struct/parser/hash_any_case.rb
|
11
|
+
lib/inde_struct/parser/open_struct_any_case.rb
|
12
|
+
lib/inde_struct/reader.rb
|
13
|
+
lib/inde_struct/version.rb
|
14
|
+
test/files/config.yml
|
15
|
+
test/files/empty.yml
|
16
|
+
test/test_inde_struct.rb
|
data/README.md
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
#IndeStruct
|
2
|
+
|
3
|
+
https://github.com/step1profit/inde_struct
|
4
|
+
|
5
|
+
##DESCRIPTION
|
6
|
+
|
7
|
+
Ruby global Constant configuration variables with indifferent access. In other words, the same configuration value with non-case-sensitive accessor.
|
8
|
+
|
9
|
+
##FEATURES
|
10
|
+
|
11
|
+
* Case-insensitive access to variables - access your way... instead of changing the original config file
|
12
|
+
* Accepts Hash or YAML-string
|
13
|
+
* Read YAML from filepath or File class
|
14
|
+
|
15
|
+
##SYNOPSIS
|
16
|
+
|
17
|
+
###Ruby env
|
18
|
+
|
19
|
+
Hash
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
hash = {whatever: 'awesome'}
|
23
|
+
# create global constant
|
24
|
+
CONFIG = IndeStruct.ible(hash)
|
25
|
+
```
|
26
|
+
|
27
|
+
YAML-string
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
yaml_string = "---\nwhatever: awesome\n"
|
31
|
+
CONFIG = IndeStruct.ible(yaml_string)
|
32
|
+
```
|
33
|
+
|
34
|
+
Filepath
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
# some_dir/config.yml # => "---\nwhatever: awesome\n"
|
38
|
+
filepath = 'some_dir/config.yml'
|
39
|
+
CONFIG = IndeStruct.ible(filepath)
|
40
|
+
```
|
41
|
+
|
42
|
+
File
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
# some_dir/config.yml # => "---\nwhatever: awesome\n"
|
46
|
+
file = File.open('test/files/config.yml')
|
47
|
+
CONFIG = IndeStruct.ible(file)
|
48
|
+
```
|
49
|
+
|
50
|
+
###Rails env
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
# in your_rails_app/config/application.rb
|
54
|
+
|
55
|
+
require 'inde_struct' # if not required via Gemfile
|
56
|
+
# assuming existing your_rails_app/config/application.yml # => "---\nwhatever: awesome\n"
|
57
|
+
hash = YAML.load(File.read(File.expand_path('../application.yml', __FILE__)))
|
58
|
+
# using environment specific configs (optional)
|
59
|
+
hash.merge!(hash[Rails.env]||{})
|
60
|
+
# create global constant
|
61
|
+
CONFIG = IndeStruct.ible(hash)
|
62
|
+
|
63
|
+
#module YourRailsApp
|
64
|
+
# class Application < Rails::Application
|
65
|
+
# [...]
|
66
|
+
```
|
67
|
+
|
68
|
+
### Result
|
69
|
+
|
70
|
+
Result is the same for both Ruby env and Rails env
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
# wherever you want:
|
74
|
+
CONFIG.whaTeVer # => 'awesome'
|
75
|
+
CONFIG.wHAteveR # => 'awesome'
|
76
|
+
CONFIG.WHaTevER # => 'awesome'
|
77
|
+
CONFIG.whatEVEr # => 'awesome'
|
78
|
+
CONFIG.WHATevEr # => 'awesome'
|
79
|
+
CONFIG.WHateVeR # => 'awesome'
|
80
|
+
```
|
81
|
+
|
82
|
+
##REQUIREMENTS
|
83
|
+
|
84
|
+
* Ruby 1.9 or higher
|
85
|
+
|
86
|
+
##INSTALL
|
87
|
+
|
88
|
+
Command Line
|
89
|
+
|
90
|
+
```
|
91
|
+
$ gem install inde_struct
|
92
|
+
```
|
93
|
+
|
94
|
+
Gemfile
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
gem 'inde_struct'
|
98
|
+
```
|
99
|
+
|
100
|
+
##LICENSE
|
101
|
+
|
102
|
+
(The MIT License)
|
103
|
+
|
104
|
+
Copyright (c) 2015 Step1Profit
|
105
|
+
|
106
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
107
|
+
a copy of this software and associated documentation files (the
|
108
|
+
'Software'), to deal in the Software without restriction, including
|
109
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
110
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
111
|
+
permit persons to whom the Software is furnished to do so, subject to
|
112
|
+
the following conditions:
|
113
|
+
|
114
|
+
The above copyright notice and this permission notice shall be
|
115
|
+
included in all copies or substantial portions of the Software.
|
116
|
+
|
117
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
118
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
119
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
120
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
121
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
122
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
123
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
require "hoe"
|
5
|
+
|
6
|
+
# Hoe.plugin :compiler
|
7
|
+
# Hoe.plugin :gem_prelude_sucks
|
8
|
+
Hoe.plugin :gemspec
|
9
|
+
# Hoe.plugin :git
|
10
|
+
# Hoe.plugin :inline
|
11
|
+
# Hoe.plugin :racc
|
12
|
+
# Hoe.plugin :rcov
|
13
|
+
# Hoe.plugin :rdoc
|
14
|
+
Hoe.plugin :yard
|
15
|
+
|
16
|
+
Hoe.spec "inde_struct" do
|
17
|
+
developer("Step1Profit", "sales@step1profit.com")
|
18
|
+
license "MIT"
|
19
|
+
end
|
20
|
+
|
21
|
+
# vim: syntax=ruby
|
data/bin/inde_struct
ADDED
data/lib/inde_struct.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'inde_struct/version'
|
2
|
+
require 'inde_struct/reader'
|
3
|
+
require 'inde_struct/parser'
|
4
|
+
|
5
|
+
module IndeStruct
|
6
|
+
extend self
|
7
|
+
|
8
|
+
def ible(arg)
|
9
|
+
obj = arg.is_a?(Hash) ? arg : IndeStruct::Reader.read(arg)
|
10
|
+
IndeStruct::Parser.parse(obj)
|
11
|
+
end
|
12
|
+
|
13
|
+
alias_method :new, :ible
|
14
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'inde_struct/parser/open_struct_any_case'
|
2
|
+
|
3
|
+
module IndeStruct
|
4
|
+
module Parser
|
5
|
+
extend self
|
6
|
+
|
7
|
+
def parse(obj)
|
8
|
+
return case obj
|
9
|
+
when Hash
|
10
|
+
obj = obj.clone
|
11
|
+
obj.each do |k,v|
|
12
|
+
obj[k] = self.parse(v)
|
13
|
+
end
|
14
|
+
IndeStruct::Parser::OpenStructAnyCase.new(obj)
|
15
|
+
when Array
|
16
|
+
obj = obj.clone
|
17
|
+
obj.map! {|i| self.parse(i)}
|
18
|
+
else
|
19
|
+
obj
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module IndeStruct
|
2
|
+
module Parser
|
3
|
+
class HashAnyCase < Hash
|
4
|
+
|
5
|
+
def [](key)
|
6
|
+
key.respond_to?(:upcase) ? super(key.upcase) : super(key)
|
7
|
+
end
|
8
|
+
|
9
|
+
def []=(key, value)
|
10
|
+
key.respond_to?(:upcase) ? super(key.upcase, value) : super(key, value)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
require 'inde_struct/parser/hash_any_case'
|
3
|
+
|
4
|
+
module IndeStruct
|
5
|
+
module Parser
|
6
|
+
class OpenStructAnyCase < OpenStruct
|
7
|
+
|
8
|
+
def initialize(hash=nil)
|
9
|
+
@table = IndeStruct::Parser::HashAnyCase.new
|
10
|
+
if hash
|
11
|
+
hash.each_pair do |k, v|
|
12
|
+
k = k.to_sym
|
13
|
+
@table[k] = v
|
14
|
+
new_ostruct_member(k)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module IndeStruct
|
4
|
+
module Reader
|
5
|
+
extend self
|
6
|
+
|
7
|
+
class EmptyFileError < StandardError; end
|
8
|
+
class EmptyInputError < StandardError; end
|
9
|
+
class InvalidInputError < StandardError; end
|
10
|
+
class InputError < StandardError; end
|
11
|
+
|
12
|
+
#
|
13
|
+
# parses YAML file or string into Ruby Hash
|
14
|
+
#
|
15
|
+
def read(input)
|
16
|
+
raise EmptyInputError if input.is_a?(String) && input.strip.empty?
|
17
|
+
raise InvalidInputError unless input.is_a?(String) or input.is_a?(File)
|
18
|
+
# read to string if File
|
19
|
+
input = IO.read(input) if input.is_a?(File) or File.exist?(input)
|
20
|
+
raise EmptyFileError if input.strip.empty?
|
21
|
+
x = YAML.load(input)
|
22
|
+
raise InputError, 'YAML input did not parse to Hash' unless x.is_a?(Hash)
|
23
|
+
return x
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
File without changes
|
@@ -0,0 +1,50 @@
|
|
1
|
+
gem "minitest"
|
2
|
+
require "minitest/autorun"
|
3
|
+
require "inde_struct"
|
4
|
+
require "securerandom"
|
5
|
+
|
6
|
+
# randomize case of String
|
7
|
+
def rand_case(x)
|
8
|
+
x.gsub(/./){|y| [y,y.swapcase][rand(2)]}
|
9
|
+
end
|
10
|
+
|
11
|
+
class TestIndeStruct < Minitest::Test
|
12
|
+
def setup
|
13
|
+
@files = File.join(File.expand_path(File.dirname(__FILE__)), "files")
|
14
|
+
end
|
15
|
+
|
16
|
+
def text_ible_hash
|
17
|
+
act = IndeStruct.ible({awesome: 'dude'})
|
18
|
+
assert_kind_of(IndeStruct::Parser::OpenStructAnyCase, act)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_ible_string
|
22
|
+
act = IndeStruct.ible("awesome: dude")
|
23
|
+
assert_kind_of(IndeStruct::Parser::OpenStructAnyCase, act)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_ible_filepath
|
27
|
+
filepath = File.join @files, 'config.yml'
|
28
|
+
assert File.exist? filepath
|
29
|
+
act = IndeStruct.ible(filepath)
|
30
|
+
assert_kind_of(IndeStruct::Parser::OpenStructAnyCase, act)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_ible_file
|
34
|
+
filepath = File.join(@files, 'config.yml')
|
35
|
+
assert File.exist? filepath
|
36
|
+
file = File.open(filepath)
|
37
|
+
assert_kind_of(File, file)
|
38
|
+
act = IndeStruct.ible(file)
|
39
|
+
assert_kind_of(IndeStruct::Parser::OpenStructAnyCase, act)
|
40
|
+
file.close
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_ible_accessor_case_sensitivity
|
44
|
+
key = SecureRandom.urlsafe_base64(rand(1..16))
|
45
|
+
exp = 'awesome'
|
46
|
+
config = IndeStruct.ible({key => exp})
|
47
|
+
act = config.send(rand_case(key))
|
48
|
+
assert_equal(exp, act)
|
49
|
+
end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: inde_struct
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Step1Profit
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-03-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: hoe-yard
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.1.2
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.1.2
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: hoe
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '3.13'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.13'
|
46
|
+
description: Ruby global Constant configuration variables with indifferent access.
|
47
|
+
In other words, the same configuration value with non-case-sensitive accessor.
|
48
|
+
email:
|
49
|
+
- sales@step1profit.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- History.txt
|
55
|
+
- LICENSE
|
56
|
+
- Manifest.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- bin/inde_struct
|
60
|
+
- lib/inde_struct.rb
|
61
|
+
- lib/inde_struct/parser.rb
|
62
|
+
- lib/inde_struct/parser/hash_any_case.rb
|
63
|
+
- lib/inde_struct/parser/open_struct_any_case.rb
|
64
|
+
- lib/inde_struct/reader.rb
|
65
|
+
- lib/inde_struct/version.rb
|
66
|
+
- test/files/config.yml
|
67
|
+
- test/files/empty.yml
|
68
|
+
- test/test_inde_struct.rb
|
69
|
+
homepage: https://github.com/step1profit/inde_struct
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 1.8.23
|
91
|
+
signing_key:
|
92
|
+
specification_version: 3
|
93
|
+
summary: Ruby global Constant configuration variables with indifferent access. In
|
94
|
+
other words, the same configuration value with non-case-sensitive accessor.
|
95
|
+
test_files: []
|
96
|
+
has_rdoc:
|