Skip to content

Commit

Permalink
Dry up configuration (#14)
Browse files Browse the repository at this point in the history
* add class_cpmpser as dependency

* modify how errors are created; add composer and remove initialize

* update version bump

* nits
  • Loading branch information
matt-taylor authored Jul 4, 2022
1 parent 03613af commit 3ba75f1
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
2 changes: 2 additions & 0 deletions json_schematize.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "class_composer", "~> 1.0"

spec.add_development_dependency "pry-byebug"
spec.add_development_dependency "rake", "~> 12.0"
spec.add_development_dependency "rspec", "~> 3.0"
Expand Down
14 changes: 1 addition & 13 deletions lib/json_schematize.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
# frozen_string_literal: true

require "json_schematize/version"

require "json_schematize/base"
require "json_schematize/boolean"
require "json_schematize/configuration"
require "json_schematize/empty_value"
require "json_schematize/errors"
require "json_schematize/generator"
require "json_schematize/version"

module JsonSchematize
class Error < StandardError; end
class ConfigError < StandardError; end
class FieldError < Error; end
class InvalidField < Error; end
class InvalidFieldByValidator < InvalidField; end
class InvalidFieldByType < InvalidField; end
class InvalidFieldByArrayOfTypes < InvalidField; end

## Customized class errors
class UndefinedBoolean < Error; end

def self.configure
yield configuration if block_given?
end
Expand Down
20 changes: 10 additions & 10 deletions lib/json_schematize/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# frozen_string_literal: true

require "class_composer"
require "json_schematize/errors"

module JsonSchematize
class Configuration
include ClassComposer::Generator

DEFAULT_ONE_MIN = 60 * 60
DEFAULT_ONE_HOUR = DEFAULT_ONE_MIN * 60
DEFAULT_ONE_DAY = DEFAULT_ONE_HOUR * 24
Expand All @@ -14,16 +19,11 @@ class Configuration
cache_update_on_change: true,
}

attr_accessor *DEFAULT_CACHE_OPTIONS.keys

def initialize
@cache_client = DEFAULT_CACHE_OPTIONS[:cache_client]
@cache_key = DEFAULT_CACHE_OPTIONS[:cache_key]
@cache_namespace = DEFAULT_CACHE_OPTIONS[:cache_namespace]
@cache_stochastic_bust = DEFAULT_CACHE_OPTIONS[:cache_stochastic_bust]
@cache_ttl = DEFAULT_CACHE_OPTIONS[:cache_ttl]
@cache_update_on_change = DEFAULT_CACHE_OPTIONS[:cache_update_on_change]
end
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") }
add_composer :cache_namespace, allowed: [String, Symbol]
add_composer :cache_stochastic_bust, allowed: [Float, Integer], default: DEFAULT_CACHE_OPTIONS[:cache_stochastic_bust]
add_composer :cache_ttl, allowed: [Float, Integer], default: DEFAULT_CACHE_OPTIONS[:cache_ttl]
add_composer :cache_update_on_change, allowed: [TrueClass, FalseClass], default: DEFAULT_CACHE_OPTIONS[:cache_update_on_change]

def cache_hash
DEFAULT_CACHE_OPTIONS.map do |key, value|
Expand Down
15 changes: 15 additions & 0 deletions lib/json_schematize/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module JsonSchematize
class Error < StandardError; end
class ConfigError < Error; end
class FieldError < Error; end

class InvalidField < Error; end
class InvalidFieldByValidator < InvalidField; end
class InvalidFieldByType < InvalidField; end
class InvalidFieldByArrayOfTypes < InvalidField; end

## Customized class errors
class UndefinedBoolean < Error; end
end
2 changes: 1 addition & 1 deletion lib/json_schematize/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module JsonSchematize
VERSION = "0.7.0"
VERSION = "0.8.0"
end

0 comments on commit 3ba75f1

Please sign in to comment.