ascetic 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: be538988aa7af75096c03b4d8e7890161855daa6
4
+ data.tar.gz: 36372d6ebf32580283a43f8852f73c77bc60c607
5
+ SHA512:
6
+ metadata.gz: bbc1a1df9f9d76c632f300350d47559c2f6a7444203e5599bb8bc120fa146c81926692de78496fe38121cccea06e6643918b498d6c450b36f480c4f78bc91c34
7
+ data.tar.gz: 92844482babf865e02dfef0c38d192185429f1eb599efdc808972f81052b54adf3002654f8d31900940a213b405af3f4db0532de90cdceef92ec0facbdb278ac
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ *.un~
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ .rspec
8
+ .rvmrc
9
+ Gemfile.lock
10
+ InstalledFiles
11
+ _yardoc
12
+ coverage
13
+ doc/
14
+ lib/bundler/man
15
+ pkg
16
+ rdoc
17
+ spec/reports
18
+ test/tmp
19
+ test/version_tmp
20
+ tmp
@@ -0,0 +1 @@
1
+ settler
@@ -0,0 +1 @@
1
+ 2.0
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Joseph McCormick
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.
@@ -0,0 +1,59 @@
1
+ # Ascetic
2
+
3
+ Ascetic is a basic tool to interact with JSON or YAML configuration files.
4
+
5
+ ## Installation
6
+
7
+ Place this in your `Gemfile`:
8
+
9
+ ```ruby
10
+ gem 'ascetic'
11
+ ```
12
+
13
+ Then `bundle install`.
14
+
15
+ ## Usage
16
+
17
+ ```ruby
18
+ require 'ascetic'
19
+
20
+ # Yaml files:
21
+ Ascetic.load! '/path/to/config.yml'
22
+
23
+ # Json files:
24
+ Ascetic.load! '/path/to/config.json'
25
+ ```
26
+
27
+ Ascetic will marshal the files and expose the options as methods. For example, if your `config.json` looks like this:
28
+
29
+ ```json
30
+ {
31
+ "api_key": "ec_fn_11h5bseljre35x"
32
+ }
33
+ ```
34
+
35
+ You will have the `api_key` method and a special `api_key?` presence check method available on your Ascetic object, like so:
36
+
37
+ ```ruby
38
+ Ascetic.api_key?
39
+ # => true
40
+
41
+ Ascetic.api_key
42
+ # => "ec_fn_11h5bseljre35x"
43
+ ```
44
+
45
+ Any value set to `false`, or value which is not present in your configuration file, will result in `false` when a presence check is run.
46
+
47
+ ```ruby
48
+ Ascetic.option_not_present_in_config?
49
+ # => false
50
+
51
+ Ascetic.option_not_present_in_config
52
+ # => nil
53
+
54
+ Ascetic.option_set_to_false_in_config?
55
+ # => false
56
+
57
+ Ascetic.option_set_to_false_in_config
58
+ # => false
59
+ ```
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |test_task|
5
+ test_task.libs << 'spec'
6
+ test_task.pattern = "spec/**/*_spec.rb"
7
+ end
8
+
9
+ task default: [:test]
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ascetic/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "ascetic"
8
+ gem.version = Ascetic::VERSION
9
+ gem.authors = ["Joseph McCormick"]
10
+ gem.email = ["esmevane@gmail.com"]
11
+ gem.description = %q{Basic configuration loader}
12
+ gem.summary = %q{Load .yml and .json files into a basic interface}
13
+ gem.homepage = "http://www.github.com/esmevane/ascetic"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_development_dependency "minitest", "~> 4.3"
20
+ gem.add_development_dependency "pry", "~> 0.9"
21
+ gem.add_development_dependency "simplecov", "~> 0.8"
22
+ end
@@ -0,0 +1,21 @@
1
+ module Ascetic
2
+ autoload :Config, 'ascetic/config'
3
+ autoload :Service, 'ascetic/service'
4
+ autoload :Store, 'ascetic/store'
5
+
6
+ def self.load! path
7
+ @service = Service.new Store.new Config.new(path).parse
8
+ end
9
+
10
+ def self.service
11
+ @service ||= Service.new
12
+ end
13
+
14
+ def self.respond_to? *arguments
15
+ service.respond_to? *arguments
16
+ end
17
+
18
+ def self.method_missing symbol, *arguments, &block
19
+ service.method_missing symbol, *arguments, &block
20
+ end
21
+ end
@@ -0,0 +1,34 @@
1
+ require 'json'
2
+ require 'yaml'
3
+
4
+ module Ascetic
5
+ class Config
6
+ attr_reader :contents, :message, :strategy
7
+
8
+ STRATEGIES = { '.json' => JSON, '.yml' => YAML }
9
+ MESSAGES = { '.json' => :parse, '.yml' => :load }
10
+
11
+ def initialize path
12
+ extension = File.extname path
13
+ @contents = File.read path
14
+ @strategy = STRATEGIES[extension]
15
+ @message = MESSAGES[extension]
16
+ end
17
+
18
+ def parse
19
+ result = strategy.send message, contents
20
+ symbolize result
21
+ end
22
+
23
+ private
24
+
25
+ def symbolize value
26
+ return value unless value.is_a? Hash
27
+ value.reduce(Hash.new) do |hash, (key, value)|
28
+ hash[key.to_sym] = symbolize value
29
+ hash
30
+ end
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,31 @@
1
+ module Ascetic
2
+ class Service
3
+ attr_reader :store
4
+
5
+ def initialize store = Store.new
6
+ @store = store
7
+ end
8
+
9
+ def method_missing symbol, *arguments, &block
10
+ last_character = symbol.slice -1, 1
11
+ last_character == "?" ? present?(symbol) : get_from_store(symbol)
12
+ end
13
+
14
+ def respond_to? *_
15
+ true
16
+ end
17
+
18
+ private
19
+
20
+ def present? symbol
21
+ key_name = symbol.slice(0...-1).to_sym
22
+ result = get_from_store key_name
23
+ !!result
24
+ end
25
+
26
+ def get_from_store symbol
27
+ @store.get symbol
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,17 @@
1
+ module Ascetic
2
+ class Store
3
+ attr_reader :contents
4
+
5
+ def initialize options = Hash.new
6
+ @contents = options
7
+ end
8
+
9
+ def set key, value
10
+ contents[key] = value
11
+ end
12
+
13
+ def get key
14
+ contents[key]
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Ascetic
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ascetic::Config do
4
+ let(:base_hash) do
5
+ { 'first_level' => { 'second_level' => true },
6
+ 'array_key' => ['array_value'] }
7
+ end
8
+
9
+ let(:symbolized_hash) { symbolize base_hash }
10
+ let(:config) { Ascetic::Config.new file }
11
+
12
+ describe 'with JSON' do
13
+ let(:file) { 'config.json' }
14
+
15
+ describe '#strategy' do
16
+ subject { config.strategy }
17
+ it { File.stub(:read, '') { subject.must_equal JSON }}
18
+ end
19
+
20
+ describe '#message' do
21
+ subject { config.message }
22
+ it { File.stub(:read, '') { subject.must_equal :parse } }
23
+ end
24
+
25
+ describe '#parse' do
26
+ let(:json) { base_hash.to_json }
27
+ subject { File.stub(:read, json) { config.parse } }
28
+ it { subject.must_be_instance_of Hash }
29
+ it { subject.must_equal symbolized_hash }
30
+ end
31
+
32
+ end
33
+
34
+ describe 'with YAML' do
35
+ let(:file) { 'config.yml' }
36
+
37
+ describe '#strategy' do
38
+ subject { config.strategy }
39
+ it { File.stub(:read, '') { subject.must_equal YAML }}
40
+ end
41
+
42
+ describe '#message' do
43
+ subject { config.message }
44
+ it { File.stub(:read, '') { subject.must_equal :load } }
45
+ end
46
+
47
+ describe '#parse' do
48
+ let(:yaml) { base_hash.to_yaml }
49
+ subject { File.stub(:read, yaml) { config.parse } }
50
+ it { subject.must_be_instance_of Hash }
51
+ it { subject.must_equal symbolized_hash }
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ascetic::Service do
4
+
5
+ describe 'default behavior' do
6
+ let(:service) { Ascetic::Service.new }
7
+ subject { service }
8
+ it { subject.respond_to?(:whatever).must_equal true }
9
+ it { subject.whatever.must_be_nil }
10
+ it { subject.whatever?.must_equal false }
11
+ end
12
+
13
+ describe 'with a populated Store object' do
14
+ let(:value) { "Value" }
15
+ let(:store) { Ascetic::Store.new key: value }
16
+ let(:service) { Ascetic::Service.new store }
17
+ subject { service }
18
+ it { subject.key.must_equal value }
19
+ it { subject.key?.must_equal true }
20
+ end
21
+
22
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ascetic::Store do
4
+ let(:store) { Ascetic::Store.new }
5
+
6
+ describe 'accessors' do
7
+ subject { store }
8
+ it { subject.must_respond_to :contents }
9
+ end
10
+
11
+ describe '#set' do
12
+ subject { store }
13
+ let(:value) { "Value" }
14
+ it 'sets a value in #contents' do
15
+ store.set :key, value
16
+ store.contents[:key].must_equal value
17
+ end
18
+ end
19
+
20
+ describe '#get' do
21
+ let(:store) { Ascetic::Store.new key: value }
22
+ let(:value) { "Value" }
23
+ subject { store.get :key }
24
+ it { subject.must_equal value }
25
+ end
26
+
27
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ascetic do
4
+ it { Ascetic.service.must_be_instance_of Ascetic::Service }
5
+
6
+ describe '#load!' do
7
+ let(:value) { "Value" }
8
+ let(:hash_config) { { "key" => value } }
9
+ let(:json_config) { hash_config.to_json }
10
+
11
+ before do
12
+ File.stub(:read, json_config) { Ascetic.load! 'config.json' }
13
+ end
14
+
15
+ it { Ascetic.whatever?.must_equal false }
16
+ it { Ascetic.key?.must_equal true }
17
+ it { Ascetic.key.must_equal value }
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ require 'simplecov'
2
+
3
+ SimpleCov.start { add_filter '/spec/' }
4
+
5
+ require 'minitest/autorun'
6
+ require 'minitest/pride'
7
+
8
+ require_relative '../lib/ascetic'
9
+
10
+ def symbolize value
11
+ return value unless value.is_a? Hash
12
+ value.reduce(Hash.new) do |hash, (key, value)|
13
+ hash[key.to_sym] = symbolize value
14
+ hash
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ascetic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Joseph McCormick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '4.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '4.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: simplecov
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '0.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.8'
55
+ description: Basic configuration loader
56
+ email:
57
+ - esmevane@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .ruby-gemset
64
+ - .ruby-version
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - ascetic.gemspec
71
+ - lib/ascetic.rb
72
+ - lib/ascetic/config.rb
73
+ - lib/ascetic/service.rb
74
+ - lib/ascetic/store.rb
75
+ - lib/ascetic/version.rb
76
+ - spec/lib/ascetic/config_spec.rb
77
+ - spec/lib/ascetic/service_spec.rb
78
+ - spec/lib/ascetic/store_spec.rb
79
+ - spec/lib/ascetic_spec.rb
80
+ - spec/spec_helper.rb
81
+ homepage: http://www.github.com/esmevane/ascetic
82
+ licenses: []
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.1.11
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Load .yml and .json files into a basic interface
104
+ test_files:
105
+ - spec/lib/ascetic/config_spec.rb
106
+ - spec/lib/ascetic/service_spec.rb
107
+ - spec/lib/ascetic/store_spec.rb
108
+ - spec/lib/ascetic_spec.rb
109
+ - spec/spec_helper.rb