Skip to content

Fixed rubocop offenses. Bumped version. #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions lib/paradocs.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require "paradocs/version"
require "paradocs/support"
require "paradocs/registry"
require "paradocs/field"
require "paradocs/results"
require "paradocs/schema"
require "paradocs/context"
require "paradocs/base_policy"
require 'paradocs/version'
require 'paradocs/support'
require 'paradocs/registry'
require 'paradocs/field'
require 'paradocs/results'
require 'paradocs/schema'
require 'paradocs/context'
require 'paradocs/base_policy'
require 'ostruct'

module Paradocs
Expand All @@ -19,17 +19,17 @@ def self.policy(name, plcy = nil, &block)

def self.config
@config ||= OpenStruct.new(
explicit_errors: false,
whitelisted_keys: [],
explicit_errors: false,
whitelisted_keys: [],
default_schema_name: :schema,
meta_prefix: "_",
whitelist_coercion: nil
meta_prefix: '_',
whitelist_coercion: nil
)
end

def self.configure
yield self.config if block_given?
self.config
yield config if block_given?
config
end
end

Expand Down
18 changes: 10 additions & 8 deletions lib/paradocs/base_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def self.meta_data(&block)
@meta_data_block
end

%w(error silent_error).each do |name|
%w[error silent_error].each do |name|
getter = "#{name}s"
define_singleton_method(getter) do
parent_errors = superclass.respond_to?(getter) ? superclass.send(getter) : []
Expand All @@ -49,22 +49,23 @@ def self.meta_data(&block)
next
end
next unless ex.is_a?(Class) || ex < StandardError

only_errors << ex
end
instance_variable_set("@#{getter}", ((self.public_send(getter) || []) + only_errors).uniq)
instance_variable_set("@#{getter}", ((public_send(getter) || []) + only_errors).uniq)
end

define_method(getter) do
instance_variable_set("@#{getter}", self.class.send("register_#{name}") || [])
end
end

def self.policy_name=(name)
@policy_name = name
class << self
attr_writer :policy_name
end

def self.policy_name
@policy_name || self.name.split("::").last.downcase.to_sym
@policy_name || name.split('::').last.downcase.to_sym
end

attr_accessor :environment
Expand All @@ -89,6 +90,7 @@ def valid?(value, key, payload)

def meta_data
return self.class.meta_data.call(*init_params) if self.class.meta_data

meta
end

Expand All @@ -97,7 +99,7 @@ def validate(*args)
end

def policy_name
(self.class.policy_name || self.to_s.demodulize.underscore).to_sym
(self.class.policy_name || to_s.demodulize.underscore).to_sym
end

def message
Expand All @@ -107,13 +109,13 @@ def message
protected

def validate(*args)
(self.class.validate || ->(*args) { true }).call(*args)
(self.class.validate || ->(*_args) { true }).call(*args)
end

private

def meta
@meta = {self.class.policy_name => {errors: self.class.errors}}
@meta = { self.class.policy_name => { errors: self.class.errors } }
end

def init_params
Expand Down
8 changes: 4 additions & 4 deletions lib/paradocs/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def add_error(key, msg)

class Context
attr_reader :environment
def initialize(path=nil, top=Top.new, environment={}, subschemes={})
def initialize(path = nil, top = Top.new, environment = {}, subschemes = {})
@top = top
@path = Array(path).compact
@environment = environment
Expand All @@ -24,6 +24,7 @@ def initialize(path=nil, top=Top.new, environment={}, subschemes={})
def subschema(subschema_name)
subschema = @subschemes[subschema_name]
return unless subschema

@subschemes.merge!(subschema.subschemes)
subschema
end
Expand All @@ -41,14 +42,13 @@ def sub(key)
end

protected

attr_reader :path, :top

def string_path
path.reduce(['$']) do |m, segment|
path.each_with_object(['$']) do |segment, m|
m << (segment.is_a?(Integer) ? "[#{segment}]" : ".#{segment}")
m
end.join
end
end

end
28 changes: 14 additions & 14 deletions lib/paradocs/default_types.rb
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
require "date"
require 'date'

module Paradocs
# type coercions
Paradocs.policy :integer do
coerce do |v, k, c|
coerce do |v, _k, _c|
v.to_i
end

meta_data do
{type: :integer}
{ type: :integer }
end
end

Paradocs.policy :number do
coerce do |v, k, c|
coerce do |v, _k, _c|
v.to_f
end

meta_data do
{type: :number}
{ type: :number }
end
end

Paradocs.policy :string do
coerce do |v, k, c|
coerce do |v, _k, _c|
v.to_s
end

meta_data do
{type: :string}
{ type: :string }
end
end

Paradocs.policy :boolean do
coerce do |v, k, c|
coerce do |v, _k, _c|
!!v
end

meta_data do
{type: :boolean}
{ type: :boolean }
end
end

Expand All @@ -53,7 +53,7 @@ module Paradocs
end

meta_data do
{type: :array}
{ type: :array }
end
end

Expand All @@ -65,21 +65,21 @@ module Paradocs
validate do |value, key, payload|
!payload.key?(key) ||
value.respond_to?(:[]) &&
value.respond_to?(:key?)
value.respond_to?(:key?)
end

meta_data do
{type: :object}
{ type: :object }
end
end

Paradocs.policy :datetime do
coerce do |v, k, c|
coerce do |v, _k, _c|
DateTime.parse(v.to_s)
end

meta_data do
{type: :datetime}
{ type: :datetime }
end
end
end
12 changes: 6 additions & 6 deletions lib/paradocs/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "paradocs"
require 'paradocs'

module Paradocs
module DSL
Expand All @@ -25,7 +25,7 @@ module DSL

def self.included(base)
base.extend(ClassMethods)
base.schemas = {Paradocs.config.default_schema_name => Paradocs::Schema.new}
base.schemas = { Paradocs.config.default_schema_name => Paradocs::Schema.new }
end

module ClassMethods
Expand All @@ -48,10 +48,10 @@ def schema(*args, &block)
key = args.first.is_a?(Symbol) ? args.shift : Paradocs.config.default_schema_name
current_schema = @schemas.fetch(key) { Paradocs::Schema.new }
new_schema = if block_given? || options.any?
Paradocs::Schema.new(options, &block)
elsif args.first.is_a?(Paradocs::Schema)
args.first
end
Paradocs::Schema.new(options, &block)
elsif args.first.is_a?(Paradocs::Schema)
args.first
end

return current_schema unless new_schema

Expand Down
19 changes: 11 additions & 8 deletions lib/paradocs/extensions/payload_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ def build_simple_structure(struct, &block)
struct.map do |key, value|
key = key.to_s
next if key.start_with?(Paradocs.config.meta_prefix) # skip all the meta fields

ex_value = restore_one(key, value, &block)
next if ex_value == @skip_word

key = value[:alias] || key
[key.to_s, ex_value]
end.compact.to_h
Expand All @@ -29,15 +31,16 @@ def build_simple_structure(struct, &block)
def restore_one(key, value, &block)
default = value[:default]
ex_value = if value[:structure]
data = build_simple_structure(value[:structure], &block)
value[:type] == :array ? [data] : data
elsif default
default.is_a?(Proc) ? default.call : default
elsif value[:options] && !value[:options].empty?
options = value[:options]
value[:type] == :array ? options : options.sample
end
data = build_simple_structure(value[:structure], &block)
value[:type] == :array ? [data] : data
elsif default
default.is_a?(Proc) ? default.call : default
elsif value[:options] && !value[:options].empty?
options = value[:options]
value[:type] == :array ? options : options.sample
end
return ex_value unless block_given?

yield(key, value, ex_value, @skip_word)
end
end
Expand Down
Loading