Skip to content

Commit 3ba75f1

Browse files
authored
Dry up configuration (#14)
* add class_cpmpser as dependency * modify how errors are created; add composer and remove initialize * update version bump * nits
1 parent 03613af commit 3ba75f1

File tree

5 files changed

+29
-24
lines changed

5 files changed

+29
-24
lines changed

json_schematize.gemspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Gem::Specification.new do |spec|
2929
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
3030
spec.require_paths = ["lib"]
3131

32+
spec.add_dependency "class_composer", "~> 1.0"
33+
3234
spec.add_development_dependency "pry-byebug"
3335
spec.add_development_dependency "rake", "~> 12.0"
3436
spec.add_development_dependency "rspec", "~> 3.0"

lib/json_schematize.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
# frozen_string_literal: true
22

3-
require "json_schematize/version"
4-
53
require "json_schematize/base"
64
require "json_schematize/boolean"
75
require "json_schematize/configuration"
86
require "json_schematize/empty_value"
7+
require "json_schematize/errors"
98
require "json_schematize/generator"
109
require "json_schematize/version"
1110

1211
module JsonSchematize
13-
class Error < StandardError; end
14-
class ConfigError < StandardError; end
15-
class FieldError < Error; end
16-
class InvalidField < Error; end
17-
class InvalidFieldByValidator < InvalidField; end
18-
class InvalidFieldByType < InvalidField; end
19-
class InvalidFieldByArrayOfTypes < InvalidField; end
20-
21-
## Customized class errors
22-
class UndefinedBoolean < Error; end
23-
2412
def self.configure
2513
yield configuration if block_given?
2614
end

lib/json_schematize/configuration.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# frozen_string_literal: true
22

3+
require "class_composer"
4+
require "json_schematize/errors"
5+
36
module JsonSchematize
47
class Configuration
8+
include ClassComposer::Generator
9+
510
DEFAULT_ONE_MIN = 60 * 60
611
DEFAULT_ONE_HOUR = DEFAULT_ONE_MIN * 60
712
DEFAULT_ONE_DAY = DEFAULT_ONE_HOUR * 24
@@ -14,16 +19,11 @@ class Configuration
1419
cache_update_on_change: true,
1520
}
1621

17-
attr_accessor *DEFAULT_CACHE_OPTIONS.keys
18-
19-
def initialize
20-
@cache_client = DEFAULT_CACHE_OPTIONS[:cache_client]
21-
@cache_key = DEFAULT_CACHE_OPTIONS[:cache_key]
22-
@cache_namespace = DEFAULT_CACHE_OPTIONS[:cache_namespace]
23-
@cache_stochastic_bust = DEFAULT_CACHE_OPTIONS[:cache_stochastic_bust]
24-
@cache_ttl = DEFAULT_CACHE_OPTIONS[:cache_ttl]
25-
@cache_update_on_change = DEFAULT_CACHE_OPTIONS[:cache_update_on_change]
26-
end
22+
add_composer :cache_key, allowed: Proc, default: DEFAULT_CACHE_OPTIONS[:cache_key], validation_error_klass: ::JsonSchematize::ConfigError, invalid_message: -> (val) { _assign_msg_("cache_key", "->(val, cusom_key) { val.hash }", "Default proc to assign cache key") }
23+
add_composer :cache_namespace, allowed: [String, Symbol]
24+
add_composer :cache_stochastic_bust, allowed: [Float, Integer], default: DEFAULT_CACHE_OPTIONS[:cache_stochastic_bust]
25+
add_composer :cache_ttl, allowed: [Float, Integer], default: DEFAULT_CACHE_OPTIONS[:cache_ttl]
26+
add_composer :cache_update_on_change, allowed: [TrueClass, FalseClass], default: DEFAULT_CACHE_OPTIONS[:cache_update_on_change]
2727

2828
def cache_hash
2929
DEFAULT_CACHE_OPTIONS.map do |key, value|

lib/json_schematize/errors.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
module JsonSchematize
4+
class Error < StandardError; end
5+
class ConfigError < Error; end
6+
class FieldError < Error; end
7+
8+
class InvalidField < Error; end
9+
class InvalidFieldByValidator < InvalidField; end
10+
class InvalidFieldByType < InvalidField; end
11+
class InvalidFieldByArrayOfTypes < InvalidField; end
12+
13+
## Customized class errors
14+
class UndefinedBoolean < Error; end
15+
end

lib/json_schematize/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module JsonSchematize
4-
VERSION = "0.7.0"
4+
VERSION = "0.8.0"
55
end

0 commit comments

Comments
 (0)