Skip to content
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

Replace use of randexp with faker to upgrade to latest attributor #34

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
1 change: 1 addition & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

# Config file for Guard
# More info at https://github.com/guard/guard#readme
group :red_green_refactor, halt_on_fail: true do
Expand Down
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require 'bundler/setup'

require 'bundler/gem_tasks'
Expand Down
3 changes: 2 additions & 1 deletion lib/praxis-blueprints.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# frozen_string_literal: true

require 'json'
require 'yaml'
require 'logger'

require 'attributor'

require 'active_support/notifications'
require 'praxis-blueprints/version'

require 'praxis-blueprints/finalizable'
Expand Down
42 changes: 20 additions & 22 deletions lib/praxis-blueprints/blueprint.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require 'ostruct'

# Blueprint ==
Expand All @@ -22,13 +23,10 @@ class Blueprint
@@caching_enabled = false

attr_reader :validating
attr_accessor :object
attr_accessor :decorators
attr_accessor :object, :decorators

class << self
attr_reader :views
attr_reader :attribute
attr_reader :options
attr_reader :views, :attribute, :options
attr_accessor :reference
end

Expand Down Expand Up @@ -110,16 +108,18 @@ def self.attributes(opts = {}, &block)

def self.domain_model(klass = nil)
return @domain_model if klass.nil?

@domain_model = klass
end

def self.check_option!(name, value)
case name
when :identity
raise Attributor::AttributorException, "Invalid identity type #{value.inspect}" unless value.is_a?(::Symbol)
return :ok

:ok
else
return Attributor::Struct.check_option!(name, value)
Attributor::Struct.check_option!(name, value)
end
end

Expand Down Expand Up @@ -185,19 +185,16 @@ def self.example(context = nil, **values)

def self.validate(value, context = Attributor::DEFAULT_ROOT_CONTEXT, _attribute = nil)
raise ArgumentError, "Invalid context received (nil) while validating value of type #{self.name}" if context.nil?

context = [context] if context.is_a? ::String

unless value.is_a?(self)
raise ArgumentError, "Error validating #{Attributor.humanize_context(context)} as #{self.name} for an object of type #{value.class.name}."
end
raise ArgumentError, "Error validating #{Attributor.humanize_context(context)} as #{self.name} for an object of type #{value.class.name}." unless value.is_a?(self)

value.validate(context)
end

def self.view(name, **options, &block)
if block_given?
return self.views[name] = View.new(name, self, **options, &block)
end
return self.views[name] = View.new(name, self, **options, &block) if block_given?

self.views[name]
end
Expand Down Expand Up @@ -260,6 +257,7 @@ def self.define_reader!(name)
else
value = @object.__send__(name)
return value if value.nil? || value.is_a?(attribute.type)

attribute.load(value)
end
end
Expand All @@ -269,13 +267,13 @@ def self.generate_master_view!
attributes = self.attributes
view :master do
attributes.each do |name, _attr|
# Note: we can freely pass master view for attributes that aren't blueprint/containers because
# NOTE: we can freely pass master view for attributes that aren't blueprint/containers because
# their dump methods will ignore it (they always dump everything regardless)
attribute name, view: :default
end
end
end

def initialize(object, decorators = nil)
# TODO: decide what sort of type checking (if any) we want to perform here.
@object = object
Expand Down Expand Up @@ -310,13 +308,12 @@ def render(view_name = nil, context: Attributor::DEFAULT_ROOT_CONTEXT, renderer:
unless (view = self.class.views[view_name])
raise "view with name '#{view_name.inspect}' is not defined in #{self.class}"
end

return view.render(self, context: context, renderer: renderer)
end

# Accept a simple array of fields, and transform it to a 1-level hash with true values
if fields.is_a? Array
fields = fields.each_with_object({}) { |field, hash| hash[field] = true }
end
fields = fields.each_with_object({}) { |field, hash| hash[field] = true } if fields.is_a? Array

# expand fields
expanded_fields = FieldExpander.expand(self.class, fields)
Expand All @@ -332,10 +329,12 @@ def to_h

def validate(context = Attributor::DEFAULT_ROOT_CONTEXT)
raise ArgumentError, "Invalid context received (nil) while validating value of type #{self.name}" if context.nil?

context = [context] if context.is_a? ::String
keys_with_values = []

raise 'validation conflict' if @validating

@validating = true

errors = []
Expand All @@ -344,9 +343,8 @@ def validate(context = Attributor::DEFAULT_ROOT_CONTEXT)
value = self.send(sub_attribute_name)
keys_with_values << sub_attribute_name unless value.nil?

if value.respond_to?(:validating) # really, it's a thing with sub-attributes
next if value.validating
end
next if value.respond_to?(:validating) && value.validating # really, it's a thing with sub-attributes

errors.concat(sub_attribute.validate(value, sub_context))
end
self.class.attribute.type.requirements.each do |req|
Expand All @@ -367,7 +365,7 @@ def _get_attr(name)
def self.as_json_schema(**args)
@attribute.type.as_json_schema(args)
end

def self.json_schema_type
@attribute.type.json_schema_type
end
Expand Down
1 change: 1 addition & 0 deletions lib/praxis-blueprints/collection_view.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

module Praxis
class CollectionView < View
def initialize(name, schema, member_view = nil)
Expand Down
3 changes: 2 additions & 1 deletion lib/praxis-blueprints/config_hash.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

module Praxis
class ConfigHash < BasicObject
attr_reader :hash
Expand All @@ -21,7 +22,7 @@ def respond_to_missing?(_method_name, _include_private = false)
true
end

def method_missing(name, value, *rest, &block) # rubocop:disable Style/MethodMissing
def method_missing(name, value, *rest, &block)
if (existing = @hash[name])
if block
existing << [value, block]
Expand Down
15 changes: 8 additions & 7 deletions lib/praxis-blueprints/field_expander.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# frozen_string_literal: true

module Praxis
class FieldExpander
def self.expand(object, fields = true)
new.expand(object, fields)
end

attr_reader :stack
attr_reader :history
attr_reader :stack, :history

def initialize
@stack = Hash.new do |hash, key|
Expand All @@ -20,6 +20,7 @@ def initialize
def expand(object, fields = true)
if stack[object].include? fields
return history[object][fields] if history[object].include? fields

# We should probably never get here, since we should have a record
# of the history of an expansion if we're trying to redo it,
# but we should also be conservative and raise here just in case.
Expand Down Expand Up @@ -82,11 +83,10 @@ def expand_view(object, fields = true)

def expand_type(object, fields = true)
unless object.respond_to?(:attributes)
if object.respond_to?(:member_attribute)
return expand_with_member_attribute(object, fields)
else
return true
end
return expand_with_member_attribute(object, fields) if object.respond_to?(:member_attribute)

return true

end

# just include the full thing if it has no attributes
Expand All @@ -103,6 +103,7 @@ def expand_type(object, fields = true)

def expand_with_member_attribute(object, fields = true)
return history[object][fields] if history[object].include? fields

history[object][fields] = []

new_fields = fields.is_a?(Array) ? fields[0] : fields
Expand Down
1 change: 1 addition & 0 deletions lib/praxis-blueprints/finalizable.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

module Praxis
module Finalizable
def self.extended(klass)
Expand Down
9 changes: 4 additions & 5 deletions lib/praxis-blueprints/renderer.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# frozen_string_literal: true

module Praxis
class Renderer
attr_reader :include_nil
attr_reader :cache
attr_reader :include_nil, :cache

class CircularRenderingError < StandardError
attr_reader :object
attr_reader :context
attr_reader :object, :context

def initialize(object, context)
@object = object
Expand Down Expand Up @@ -75,7 +74,7 @@ def _render(object, fields, view = nil, context: Attributor::DEFAULT_ROOT_CONTEX
fields.each_with_object({}) do |(key, subfields), hash|
begin
value = object._get_attr(key)
rescue => e
rescue StandardError => e
raise Attributor::DumpError.new(context: context, name: key, type: object.class, original_exception: e)
end

Expand Down
1 change: 1 addition & 0 deletions lib/praxis-blueprints/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

module Praxis
BLUEPRINTS_VERSION = '3.5'
end
12 changes: 4 additions & 8 deletions lib/praxis-blueprints/view.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# frozen_string_literal: true

module Praxis
class View
attr_reader :schema
attr_reader :contents
attr_reader :name
attr_reader :options
attr_reader :schema, :contents, :name, :options

def initialize(name, schema, **options, &block)
@name = name
@schema = schema
@contents = ::Hash.new
@contents = {}
@block = block

@options = options
Expand Down Expand Up @@ -85,9 +83,7 @@ def describe

contents.each do |k, dumpable|
inner_desc = {}
if dumpable.is_a?(Praxis::View)
inner_desc[:view] = dumpable.name if dumpable.name
end
inner_desc[:view] = dumpable.name if dumpable.is_a?(Praxis::View) && dumpable.name
view_attributes[k] = inner_desc
end

Expand Down
22 changes: 11 additions & 11 deletions praxis-blueprints.gemspec
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true
lib = File.expand_path('../lib', __FILE__)

lib = File.expand_path('lib', __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'praxis-blueprints/version'

Expand Down Expand Up @@ -27,25 +28,24 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

spec.add_runtime_dependency('randexp', ['~> 0'])
spec.add_runtime_dependency('activesupport', '>= 6')
spec.add_runtime_dependency('attributor', ['>= 5.5'])
spec.add_runtime_dependency('activesupport', ['>= 3'])

spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake'

spec.add_development_dependency('redcarpet', ['< 3.0'])
spec.add_development_dependency('yard')
spec.add_development_dependency('coveralls')
spec.add_development_dependency('fuubar')
spec.add_development_dependency('guard', ['~> 2'])
spec.add_development_dependency('guard-rspec', ['>= 0'])
spec.add_development_dependency('rspec')
spec.add_development_dependency('rspec-its')
spec.add_development_dependency('rspec-collection_matchers')
spec.add_development_dependency 'guard-rubocop'
spec.add_development_dependency('pry')
spec.add_development_dependency('pry-byebug')
spec.add_development_dependency('pry-stack_explorer')
spec.add_development_dependency('fuubar')
spec.add_development_dependency('coveralls')
spec.add_development_dependency('redcarpet', ['< 3.0'])
spec.add_development_dependency('rspec')
spec.add_development_dependency('rspec-collection_matchers')
spec.add_development_dependency('rspec-its')
spec.add_development_dependency 'rubocop'
spec.add_development_dependency 'guard-rubocop'
spec.add_development_dependency('yard')
end
15 changes: 7 additions & 8 deletions spec/praxis-blueprints/blueprint_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Praxis::Blueprint do
Expand Down Expand Up @@ -108,13 +109,11 @@

context 'from Blueprint.example' do
subject(:blueprint_instance) do
blueprint_class.example('ExamplePerson',
address: nil,
prior_addresses: [],
work_address: nil,
myself: nil,
friends: []
)
blueprint_class.example('ExamplePerson',
prior_addresses: [],
work_address: nil,
myself: nil,
friends: [])
end
it_behaves_like 'a blueprint instance'
end
Expand All @@ -128,7 +127,7 @@
email: '[email protected]',
aliases: [],
prior_addresses: [],
parents: { father: Randgen.first_name, mother: Randgen.first_name },
parents: { father: Faker::Name.first_name, mother: Faker::Name.first_name },
href: 'www.example.com',
alive: true
}
Expand Down
3 changes: 2 additions & 1 deletion spec/praxis-blueprints/collection_view_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

require_relative '../spec_helper'

describe Praxis::CollectionView do
Expand Down Expand Up @@ -58,7 +59,7 @@
subject(:output) { collection_view.render(people, context: root_context) }

it { should be_kind_of(Array) }
it { should eq people.collect { |person| member_view.render(person) } }
it { should eq(people.collect { |person| member_view.render(person) }) }
end

context '#example' do
Expand Down
Loading