chozo 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.travis.yml +5 -0
- data/Gemfile +3 -0
- data/Guardfile +21 -0
- data/LICENSE +201 -0
- data/README.md +34 -0
- data/Thorfile +38 -0
- data/chozo.gemspec +35 -0
- data/lib/chozo.rb +6 -0
- data/lib/chozo/config.rb +100 -0
- data/lib/chozo/config/json.rb +59 -0
- data/lib/chozo/errors.rb +7 -0
- data/lib/chozo/version.rb +3 -0
- data/spec/shared_examples/chozo/config.rb +5 -0
- data/spec/spec_helper.rb +28 -0
- data/spec/support/helpers.rb +20 -0
- data/spec/unit/chozo/config/json_spec.rb +109 -0
- data/spec/unit/chozo/config_spec.rb +33 -0
- metadata +312 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
guard 'spork' do
|
2
|
+
watch('Gemfile')
|
3
|
+
watch('spec/spec_helper.rb') { :rspec }
|
4
|
+
watch(%r{^spec/support/.+\.rb$}) { :rspec }
|
5
|
+
end
|
6
|
+
|
7
|
+
guard 'yard', stdout: '/dev/null', stderr: '/dev/null' do
|
8
|
+
watch(%r{app/.+\.rb})
|
9
|
+
watch(%r{lib/.+\.rb})
|
10
|
+
watch(%r{ext/.+\.c})
|
11
|
+
end
|
12
|
+
|
13
|
+
guard 'rspec', version: 2, cli: "--color --drb --format Fuubar", all_on_start: false, all_after_pass: false, notification: false do
|
14
|
+
watch(%r{^spec/unit/.+_spec\.rb$})
|
15
|
+
watch(%r{^spec/acceptance/.+_spec\.rb$})
|
16
|
+
|
17
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/unit/#{m[1]}_spec.rb" }
|
18
|
+
watch('spec/spec_helper.rb') { "spec" }
|
19
|
+
watch(%r{^spec/support/.+\.rb$}) { "spec" }
|
20
|
+
watch(%r{^spec/shared_examples/(.*)\.rb$}) { |m| ["spec/unit/#{m[1]}_spec.rb", "spec/unit/#{m[1]}"] }
|
21
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright 2012, Jamie Winsor
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# Chozo
|
2
|
+
[![Build Status](https://secure.travis-ci.org/reset/chozo.png?branch=master)](http://travis-ci.org/reset/chozo)
|
3
|
+
[![Dependency Status](https://gemnasium.com/reset/chozo.png?travis)](https://gemnasium.com/reset/chozo)
|
4
|
+
[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/reset/chozo)
|
5
|
+
|
6
|
+
A collection of supporting libraries and Ruby core extensions
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
$ gem install chozo
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
Include all of Chozo into your application
|
15
|
+
|
16
|
+
require 'chozo'
|
17
|
+
|
18
|
+
Or maybe just a piece?
|
19
|
+
|
20
|
+
require 'chozo/config'
|
21
|
+
|
22
|
+
### JSON Config
|
23
|
+
|
24
|
+
require 'chozo/config'
|
25
|
+
|
26
|
+
module MyApp
|
27
|
+
class Config
|
28
|
+
include Chozo::Config::JSON
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Authors and Contributors
|
33
|
+
|
34
|
+
* Jamie Winsor (<jamie@vialstudios.com>)
|
data/Thorfile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
require 'bundler'
|
5
|
+
require 'bundler/setup'
|
6
|
+
|
7
|
+
require 'chozo'
|
8
|
+
require 'thor/rake_compat'
|
9
|
+
|
10
|
+
class Default < Thor
|
11
|
+
include Thor::RakeCompat
|
12
|
+
Bundler::GemHelper.install_tasks
|
13
|
+
|
14
|
+
desc "build", "Build chozo-#{Chozo::VERSION}.gem into the pkg directory"
|
15
|
+
def build
|
16
|
+
Rake::Task["build"].execute
|
17
|
+
end
|
18
|
+
|
19
|
+
desc "install", "Build and install chozo-#{Chozo::VERSION}.gem into system gems"
|
20
|
+
def install
|
21
|
+
Rake::Task["install"].execute
|
22
|
+
end
|
23
|
+
|
24
|
+
desc "release", "Create tag v#{Chozo::VERSION} and build and push chozo-#{Chozo::VERSION}.gem to Rubygems"
|
25
|
+
def release
|
26
|
+
Rake::Task["release"].execute
|
27
|
+
end
|
28
|
+
|
29
|
+
class Spec < Thor
|
30
|
+
namespace :spec
|
31
|
+
default_task :all
|
32
|
+
|
33
|
+
desc "all", "run all tests"
|
34
|
+
def all
|
35
|
+
exec "rspec --color --format=documentation spec"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/chozo.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/chozo/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.authors = ["Jamie Winsor"]
|
6
|
+
s.email = ["jamie@vialstudios.com"]
|
7
|
+
s.description = %q{A collection of supporting libraries and Ruby core extensions}
|
8
|
+
s.summary = s.description
|
9
|
+
s.homepage = "https://github.com/reset/chozo"
|
10
|
+
|
11
|
+
s.files = `git ls-files`.split($\)
|
12
|
+
s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
s.test_files = s.files.grep(%r{^(spec)/})
|
14
|
+
s.name = "chozo"
|
15
|
+
s.require_paths = ["lib"]
|
16
|
+
s.version = Chozo::VERSION
|
17
|
+
s.required_ruby_version = ">= 1.9.1"
|
18
|
+
|
19
|
+
s.add_runtime_dependency 'activemodel'
|
20
|
+
s.add_runtime_dependency 'multi_json', '>= 1.3.0'
|
21
|
+
|
22
|
+
s.add_development_dependency 'thor'
|
23
|
+
s.add_development_dependency 'rake'
|
24
|
+
s.add_development_dependency 'rspec'
|
25
|
+
s.add_development_dependency 'fuubar'
|
26
|
+
s.add_development_dependency 'spork'
|
27
|
+
s.add_development_dependency 'yard'
|
28
|
+
s.add_development_dependency 'guard'
|
29
|
+
s.add_development_dependency 'guard-rspec'
|
30
|
+
s.add_development_dependency 'guard-spork'
|
31
|
+
s.add_development_dependency 'guard-yard'
|
32
|
+
s.add_development_dependency 'coolline'
|
33
|
+
s.add_development_dependency 'redcarpet'
|
34
|
+
s.add_development_dependency 'json_spec'
|
35
|
+
end
|
data/lib/chozo.rb
ADDED
data/lib/chozo/config.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'chozo/errors'
|
3
|
+
|
4
|
+
module Chozo
|
5
|
+
module Config
|
6
|
+
autoload :JSON, 'chozo/config/json'
|
7
|
+
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
included do
|
11
|
+
include ActiveModel::AttributeMethods
|
12
|
+
include ActiveModel::Validations
|
13
|
+
attribute_method_suffix('=')
|
14
|
+
end
|
15
|
+
|
16
|
+
module ClassMethods
|
17
|
+
# @return [Set]
|
18
|
+
def attributes
|
19
|
+
@attributes ||= Set.new
|
20
|
+
end
|
21
|
+
|
22
|
+
# @return [Hash]
|
23
|
+
def defaults
|
24
|
+
@defaults ||= Hash.new
|
25
|
+
end
|
26
|
+
|
27
|
+
# @param [Symbol] name
|
28
|
+
# @param [Hash] options
|
29
|
+
#
|
30
|
+
# @return [Hash]
|
31
|
+
def attribute(name, options = {})
|
32
|
+
if options[:default]
|
33
|
+
default_for_attribute(name, options[:default])
|
34
|
+
end
|
35
|
+
define_attribute_method(name)
|
36
|
+
attributes << name.to_sym
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def default_for_attribute(name, value)
|
42
|
+
defaults[name.to_sym] = value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
attr_accessor :path
|
47
|
+
|
48
|
+
# @param [String] path
|
49
|
+
# @param [Hash] attributes
|
50
|
+
def initialize(path = nil, attributes = {})
|
51
|
+
@path = File.expand_path(path) if path
|
52
|
+
self.attributes = attributes
|
53
|
+
end
|
54
|
+
|
55
|
+
# @param [Symbol] key
|
56
|
+
#
|
57
|
+
# @return [Object]
|
58
|
+
def attribute(key)
|
59
|
+
instance_variable_get("@#{key}") || self.class.defaults[key]
|
60
|
+
end
|
61
|
+
alias_method :[], :attribute
|
62
|
+
|
63
|
+
# @param [Symbol] key
|
64
|
+
# @param [Object] value
|
65
|
+
def attribute=(key, value)
|
66
|
+
instance_variable_set("@#{key}", value)
|
67
|
+
end
|
68
|
+
alias_method :[]=, :attribute=
|
69
|
+
|
70
|
+
# @param [Hash] new_attributes
|
71
|
+
def attributes=(new_attributes)
|
72
|
+
new_attributes.symbolize_keys!
|
73
|
+
|
74
|
+
self.class.attributes.each do |attr_name|
|
75
|
+
send(:attribute=, attr_name, new_attributes[attr_name.to_sym])
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# @param [Symbol] key
|
80
|
+
#
|
81
|
+
# @return [Boolean]
|
82
|
+
def attribute?(key)
|
83
|
+
instance_variable_get("@#{key}").present?
|
84
|
+
end
|
85
|
+
|
86
|
+
# @return [Hash]
|
87
|
+
def attributes
|
88
|
+
{}.tap do |attrs|
|
89
|
+
self.class.attributes.each do |attr|
|
90
|
+
attrs[attr] = attribute(attr)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# @return [String]
|
96
|
+
def to_s
|
97
|
+
"[#{self.class}] '#{self.path}': #{self.attributes}"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'multi_json'
|
2
|
+
|
3
|
+
module Chozo
|
4
|
+
module Config
|
5
|
+
module JSON
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
include Chozo::Config
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
# @param [String] data
|
11
|
+
#
|
12
|
+
# @return [~Chozo::Config::JSON]
|
13
|
+
def from_json(data)
|
14
|
+
new.from_json(data)
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param [String] path
|
18
|
+
#
|
19
|
+
# @raise [Chozo::Errors::ConfigNotFound]
|
20
|
+
#
|
21
|
+
# @return [~Chozo::Config::JSON]
|
22
|
+
def from_file(path)
|
23
|
+
data = File.read(path)
|
24
|
+
new(path).from_json(data)
|
25
|
+
rescue Errno::ENOENT, Errno::EISDIR
|
26
|
+
raise Chozo::Errors::ConfigNotFound, "No configuration found at: '#{path}'"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# @param (see MultiJson.encode) options
|
31
|
+
#
|
32
|
+
# @return [String]
|
33
|
+
def to_json(options = {})
|
34
|
+
MultiJson.encode(self.attributes, options)
|
35
|
+
end
|
36
|
+
alias_method :as_json, :to_json
|
37
|
+
|
38
|
+
# @param (see MultiJson.decode) json
|
39
|
+
# @param (see MultiJson.decode) options
|
40
|
+
#
|
41
|
+
# @raise [Chozo::Errors::InvalidConfig]
|
42
|
+
#
|
43
|
+
# @return [~Chozo::Config::JSON]
|
44
|
+
def from_json(json, options = {})
|
45
|
+
self.attributes = MultiJson.decode(json, options)
|
46
|
+
self
|
47
|
+
rescue MultiJson::DecodeError => e
|
48
|
+
raise Chozo::Errors::InvalidConfig, e
|
49
|
+
end
|
50
|
+
|
51
|
+
def save(destination = self.path)
|
52
|
+
FileUtils.mkdir_p(File.dirname(destination))
|
53
|
+
File.open(destination, 'w+') do |f|
|
54
|
+
f.write(self.to_json)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/lib/chozo/errors.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
require 'spork'
|
4
|
+
|
5
|
+
Spork.prefork do
|
6
|
+
require 'rspec'
|
7
|
+
require 'json_spec'
|
8
|
+
|
9
|
+
Dir[File.join(File.expand_path("../../spec/support/**/*.rb", __FILE__))].each { |f| require f }
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
config.include JsonSpec::Helpers
|
13
|
+
config.include Chozo::Spec::Helpers
|
14
|
+
|
15
|
+
config.mock_with :rspec
|
16
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
17
|
+
config.filter_run focus: true
|
18
|
+
config.run_all_when_everything_filtered = true
|
19
|
+
|
20
|
+
config.before(:each) do
|
21
|
+
clean_tmp_path
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
Spork.each_run do
|
27
|
+
require 'chozo'
|
28
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
|
3
|
+
module Chozo
|
4
|
+
module Spec
|
5
|
+
module Helpers
|
6
|
+
def app_root_path
|
7
|
+
Pathname.new(File.expand_path('../../../', __FILE__))
|
8
|
+
end
|
9
|
+
|
10
|
+
def tmp_path
|
11
|
+
app_root_path.join('spec/tmp')
|
12
|
+
end
|
13
|
+
|
14
|
+
def clean_tmp_path
|
15
|
+
FileUtils.rm_rf(tmp_path)
|
16
|
+
FileUtils.mkdir_p(tmp_path)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'shared_examples/chozo/config'
|
3
|
+
|
4
|
+
TestJSONConfig = Class.new do
|
5
|
+
include Chozo::Config::JSON
|
6
|
+
|
7
|
+
attribute :name
|
8
|
+
validates_presence_of :name
|
9
|
+
|
10
|
+
attribute :job
|
11
|
+
end
|
12
|
+
|
13
|
+
describe Chozo::Config::JSON do
|
14
|
+
it_behaves_like "Chozo::Config", TestJSONConfig
|
15
|
+
|
16
|
+
let(:json) do
|
17
|
+
%(
|
18
|
+
{
|
19
|
+
"name": "reset",
|
20
|
+
"job": "programmer",
|
21
|
+
"status": "awesome"
|
22
|
+
}
|
23
|
+
)
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "ClassMethods" do
|
27
|
+
subject { TestJSONConfig }
|
28
|
+
|
29
|
+
describe "::from_json" do
|
30
|
+
it "returns an instance of the including class" do
|
31
|
+
subject.from_json(json).should be_a(subject)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "assigns values for each defined attribute" do
|
35
|
+
config = subject.from_json(json)
|
36
|
+
|
37
|
+
config[:name].should eql("reset")
|
38
|
+
config[:job].should eql("programmer")
|
39
|
+
end
|
40
|
+
|
41
|
+
it "does not set an attribute value for undefined attributes" do
|
42
|
+
config = subject.from_json(json)
|
43
|
+
|
44
|
+
config[:status].should be_nil
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "::from_file" do
|
49
|
+
let(:file) { tmp_path.join("test_config.json").to_s }
|
50
|
+
|
51
|
+
before(:each) do
|
52
|
+
File.open(file, "w") { |f| f.write(json) }
|
53
|
+
end
|
54
|
+
|
55
|
+
it "returns an instance of the including class" do
|
56
|
+
subject.from_file(file).should be_a(TestJSONConfig)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "sets the object's filepath to the path of the loaded file" do
|
60
|
+
subject.from_file(file).path.should eql(file)
|
61
|
+
end
|
62
|
+
|
63
|
+
context "given a file that does not exist" do
|
64
|
+
it "raises a Chozo::Errors::ConfigNotFound error" do
|
65
|
+
lambda {
|
66
|
+
subject.from_file(tmp_path.join("asdf.txt"))
|
67
|
+
}.should raise_error(Chozo::Errors::ConfigNotFound)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
subject { TestJSONConfig.new }
|
74
|
+
|
75
|
+
describe "#to_json" do
|
76
|
+
before(:each) do
|
77
|
+
subject.name = "reset"
|
78
|
+
subject.job = "programmer"
|
79
|
+
end
|
80
|
+
|
81
|
+
it "returns JSON with key values for each attribute" do
|
82
|
+
hash = parse_json(subject.to_json)
|
83
|
+
|
84
|
+
hash.should have_key("name")
|
85
|
+
hash["name"].should eql("reset")
|
86
|
+
hash.should have_key("job")
|
87
|
+
hash["job"].should eql("programmer")
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "#from_json" do
|
92
|
+
it "returns an instance of the updated class" do
|
93
|
+
subject.from_json(json).should be_a(subject.class)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "assigns values for each defined attribute" do
|
97
|
+
config = subject.from_json(json)
|
98
|
+
|
99
|
+
config[:name].should eql("reset")
|
100
|
+
config[:job].should eql("programmer")
|
101
|
+
end
|
102
|
+
|
103
|
+
it "does not set an attribute value for undefined attributes" do
|
104
|
+
config = subject.from_json(json)
|
105
|
+
|
106
|
+
config[:status].should be_nil
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'shared_examples/chozo/config'
|
3
|
+
|
4
|
+
TestConfig = Class.new do
|
5
|
+
include Chozo::Config
|
6
|
+
|
7
|
+
attribute :name
|
8
|
+
validates_presence_of :name
|
9
|
+
|
10
|
+
attribute :job
|
11
|
+
end
|
12
|
+
|
13
|
+
describe Chozo::Config do
|
14
|
+
it_behaves_like "Chozo::Config", TestConfig
|
15
|
+
|
16
|
+
subject do
|
17
|
+
TestConfig.new
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "Validations" do
|
21
|
+
it "is valid if all required attributes are specified" do
|
22
|
+
subject.name = "reset"
|
23
|
+
|
24
|
+
subject.should be_valid
|
25
|
+
end
|
26
|
+
|
27
|
+
it "is not valid if a required attribute is not specified" do
|
28
|
+
subject.name = nil
|
29
|
+
|
30
|
+
subject.should_not be_valid
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,312 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chozo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jamie Winsor
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activemodel
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: multi_json
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 1.3.0
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.3.0
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: thor
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rspec
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: fuubar
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: spork
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: yard
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
none: false
|
138
|
+
requirements:
|
139
|
+
- - ! '>='
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
- !ruby/object:Gem::Dependency
|
143
|
+
name: guard
|
144
|
+
requirement: !ruby/object:Gem::Requirement
|
145
|
+
none: false
|
146
|
+
requirements:
|
147
|
+
- - ! '>='
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
type: :development
|
151
|
+
prerelease: false
|
152
|
+
version_requirements: !ruby/object:Gem::Requirement
|
153
|
+
none: false
|
154
|
+
requirements:
|
155
|
+
- - ! '>='
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0'
|
158
|
+
- !ruby/object:Gem::Dependency
|
159
|
+
name: guard-rspec
|
160
|
+
requirement: !ruby/object:Gem::Requirement
|
161
|
+
none: false
|
162
|
+
requirements:
|
163
|
+
- - ! '>='
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '0'
|
166
|
+
type: :development
|
167
|
+
prerelease: false
|
168
|
+
version_requirements: !ruby/object:Gem::Requirement
|
169
|
+
none: false
|
170
|
+
requirements:
|
171
|
+
- - ! '>='
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
- !ruby/object:Gem::Dependency
|
175
|
+
name: guard-spork
|
176
|
+
requirement: !ruby/object:Gem::Requirement
|
177
|
+
none: false
|
178
|
+
requirements:
|
179
|
+
- - ! '>='
|
180
|
+
- !ruby/object:Gem::Version
|
181
|
+
version: '0'
|
182
|
+
type: :development
|
183
|
+
prerelease: false
|
184
|
+
version_requirements: !ruby/object:Gem::Requirement
|
185
|
+
none: false
|
186
|
+
requirements:
|
187
|
+
- - ! '>='
|
188
|
+
- !ruby/object:Gem::Version
|
189
|
+
version: '0'
|
190
|
+
- !ruby/object:Gem::Dependency
|
191
|
+
name: guard-yard
|
192
|
+
requirement: !ruby/object:Gem::Requirement
|
193
|
+
none: false
|
194
|
+
requirements:
|
195
|
+
- - ! '>='
|
196
|
+
- !ruby/object:Gem::Version
|
197
|
+
version: '0'
|
198
|
+
type: :development
|
199
|
+
prerelease: false
|
200
|
+
version_requirements: !ruby/object:Gem::Requirement
|
201
|
+
none: false
|
202
|
+
requirements:
|
203
|
+
- - ! '>='
|
204
|
+
- !ruby/object:Gem::Version
|
205
|
+
version: '0'
|
206
|
+
- !ruby/object:Gem::Dependency
|
207
|
+
name: coolline
|
208
|
+
requirement: !ruby/object:Gem::Requirement
|
209
|
+
none: false
|
210
|
+
requirements:
|
211
|
+
- - ! '>='
|
212
|
+
- !ruby/object:Gem::Version
|
213
|
+
version: '0'
|
214
|
+
type: :development
|
215
|
+
prerelease: false
|
216
|
+
version_requirements: !ruby/object:Gem::Requirement
|
217
|
+
none: false
|
218
|
+
requirements:
|
219
|
+
- - ! '>='
|
220
|
+
- !ruby/object:Gem::Version
|
221
|
+
version: '0'
|
222
|
+
- !ruby/object:Gem::Dependency
|
223
|
+
name: redcarpet
|
224
|
+
requirement: !ruby/object:Gem::Requirement
|
225
|
+
none: false
|
226
|
+
requirements:
|
227
|
+
- - ! '>='
|
228
|
+
- !ruby/object:Gem::Version
|
229
|
+
version: '0'
|
230
|
+
type: :development
|
231
|
+
prerelease: false
|
232
|
+
version_requirements: !ruby/object:Gem::Requirement
|
233
|
+
none: false
|
234
|
+
requirements:
|
235
|
+
- - ! '>='
|
236
|
+
- !ruby/object:Gem::Version
|
237
|
+
version: '0'
|
238
|
+
- !ruby/object:Gem::Dependency
|
239
|
+
name: json_spec
|
240
|
+
requirement: !ruby/object:Gem::Requirement
|
241
|
+
none: false
|
242
|
+
requirements:
|
243
|
+
- - ! '>='
|
244
|
+
- !ruby/object:Gem::Version
|
245
|
+
version: '0'
|
246
|
+
type: :development
|
247
|
+
prerelease: false
|
248
|
+
version_requirements: !ruby/object:Gem::Requirement
|
249
|
+
none: false
|
250
|
+
requirements:
|
251
|
+
- - ! '>='
|
252
|
+
- !ruby/object:Gem::Version
|
253
|
+
version: '0'
|
254
|
+
description: A collection of supporting libraries and Ruby core extensions
|
255
|
+
email:
|
256
|
+
- jamie@vialstudios.com
|
257
|
+
executables: []
|
258
|
+
extensions: []
|
259
|
+
extra_rdoc_files: []
|
260
|
+
files:
|
261
|
+
- .gitignore
|
262
|
+
- .travis.yml
|
263
|
+
- Gemfile
|
264
|
+
- Guardfile
|
265
|
+
- LICENSE
|
266
|
+
- README.md
|
267
|
+
- Thorfile
|
268
|
+
- chozo.gemspec
|
269
|
+
- lib/chozo.rb
|
270
|
+
- lib/chozo/config.rb
|
271
|
+
- lib/chozo/config/json.rb
|
272
|
+
- lib/chozo/errors.rb
|
273
|
+
- lib/chozo/version.rb
|
274
|
+
- spec/shared_examples/chozo/config.rb
|
275
|
+
- spec/spec_helper.rb
|
276
|
+
- spec/support/helpers.rb
|
277
|
+
- spec/unit/chozo/config/json_spec.rb
|
278
|
+
- spec/unit/chozo/config_spec.rb
|
279
|
+
homepage: https://github.com/reset/chozo
|
280
|
+
licenses: []
|
281
|
+
post_install_message:
|
282
|
+
rdoc_options: []
|
283
|
+
require_paths:
|
284
|
+
- lib
|
285
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
286
|
+
none: false
|
287
|
+
requirements:
|
288
|
+
- - ! '>='
|
289
|
+
- !ruby/object:Gem::Version
|
290
|
+
version: 1.9.1
|
291
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
292
|
+
none: false
|
293
|
+
requirements:
|
294
|
+
- - ! '>='
|
295
|
+
- !ruby/object:Gem::Version
|
296
|
+
version: '0'
|
297
|
+
segments:
|
298
|
+
- 0
|
299
|
+
hash: 3619207911934707426
|
300
|
+
requirements: []
|
301
|
+
rubyforge_project:
|
302
|
+
rubygems_version: 1.8.23
|
303
|
+
signing_key:
|
304
|
+
specification_version: 3
|
305
|
+
summary: A collection of supporting libraries and Ruby core extensions
|
306
|
+
test_files:
|
307
|
+
- spec/shared_examples/chozo/config.rb
|
308
|
+
- spec/spec_helper.rb
|
309
|
+
- spec/support/helpers.rb
|
310
|
+
- spec/unit/chozo/config/json_spec.rb
|
311
|
+
- spec/unit/chozo/config_spec.rb
|
312
|
+
has_rdoc:
|