├── .gitignore ├── .travis.yml ├── CODE_OF_CONDUCT.md ├── Gemfile ├── LICENSE.txt ├── README.md ├── Rakefile ├── dry-types-rails.gemspec ├── lib └── dry │ └── types │ ├── rails.rb │ └── rails │ ├── railtie.rb │ └── version.rb └── spec ├── dry └── types │ └── rails │ └── railtie_spec.rb ├── internal ├── autoload-lib │ └── autoload │ │ ├── custom_types.rb │ │ ├── schema_struct.rb │ │ └── types.rb ├── config │ ├── database.yml │ └── routes.rb ├── db │ └── schema.rb ├── lib │ ├── custom_types.rb │ ├── schema_struct.rb │ └── types.rb ├── log │ └── .gitignore └── public │ └── favicon.ico └── spec_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /Gemfile.lock 4 | /_yardoc/ 5 | /coverage/ 6 | /doc/ 7 | /pkg/ 8 | /spec/reports/ 9 | /tmp/ 10 | *.gem -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 2.2 4 | script: bundle exec rspec 5 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, and in the interest of 4 | fostering an open and welcoming community, we pledge to respect all people who 5 | contribute through reporting issues, posting feature requests, updating 6 | documentation, submitting pull requests or patches, and other activities. 7 | 8 | We are committed to making participation in this project a harassment-free 9 | experience for everyone, regardless of level of experience, gender, gender 10 | identity and expression, sexual orientation, disability, personal appearance, 11 | body size, race, ethnicity, age, religion, or nationality. 12 | 13 | Examples of unacceptable behavior by participants include: 14 | 15 | * The use of sexualized language or imagery 16 | * Personal attacks 17 | * Trolling or insulting/derogatory comments 18 | * Public or private harassment 19 | * Publishing other's private information, such as physical or electronic 20 | addresses, without explicit permission 21 | * Other unethical or unprofessional conduct 22 | 23 | Project maintainers have the right and responsibility to remove, edit, or 24 | reject comments, commits, code, wiki edits, issues, and other contributions 25 | that are not aligned to this Code of Conduct, or to ban temporarily or 26 | permanently any contributor for other behaviors that they deem inappropriate, 27 | threatening, offensive, or harmful. 28 | 29 | By adopting this Code of Conduct, project maintainers commit themselves to 30 | fairly and consistently applying these principles to every aspect of managing 31 | this project. Project maintainers who do not follow or enforce the Code of 32 | Conduct may be permanently removed from the project team. 33 | 34 | This code of conduct applies both within project spaces and in public spaces 35 | when an individual is representing the project or its community. 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 38 | reported by contacting a project maintainer at jeromegn@gmail.com. All 39 | complaints will be reviewed and investigated and will result in a response that 40 | is deemed necessary and appropriate to the circumstances. Maintainers are 41 | obligated to maintain confidentiality with regard to the reporter of an 42 | incident. 43 | 44 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 45 | version 1.3.0, available at 46 | [http://contributor-covenant.org/version/1/3/0/][version] 47 | 48 | [homepage]: http://contributor-covenant.org 49 | [version]: http://contributor-covenant.org/version/1/3/0/ -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in dry-data-rails.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Jerome Gravel-Niquet 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Dry::Types::Rails [![Build Status](https://travis-ci.org/jeromegn/dry-types-rails.svg?branch=master)](https://travis-ci.org/jeromegn/dry-types-rails) 2 | 3 | Really simple gem that will help you work with the [`dry-types`](https://github.com/dryrb/dry-types) gem in development. 4 | 5 | ## Current status: EXPERIMENTAL 6 | 7 | This gem currently "works for me", but as of right now I'm sure there are a number of edge cases that haven't been hit yet. As those occur, please report them so I can add specs to test them. 8 | 9 | ## Raison d'être 10 | 11 | Rails reloads code quite often in development, this broke the usage of `Dry::Types` since it would register the same types multiple times and raise exceptions. 12 | 13 | ## Installation 14 | 15 | Add this line to your application's Gemfile: 16 | 17 | ```ruby 18 | gem 'dry-types-rails', group: :development 19 | ``` 20 | 21 | Note that it should __only__ be included in your development environment. 22 | 23 | ## Usage 24 | 25 | Nothing to do, this is using `Rails::Railtie`, which means it gets loaded automatically. 26 | 27 | ## Contributing 28 | 29 | Bug reports and pull requests are welcome on GitHub at https://github.com/jeromegn/dry-types-rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. 30 | 31 | ## License 32 | 33 | The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT). 34 | 35 | ## Thanks 36 | 37 | - @AMHOL 38 | - @jeromegn 39 | - @pnomolos 40 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | task :default => :spec 3 | -------------------------------------------------------------------------------- /dry-types-rails.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'dry/types/rails/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "dry-types-rails" 8 | spec.version = Dry::Types::Rails::VERSION 9 | spec.authors = ["Jerome Gravel-Niquet"] 10 | spec.email = ["jeromegn@gmail.com"] 11 | 12 | spec.summary = "Simplifies dry-types usage with Rails." 13 | spec.description = "Simplifies dry-types usage with Rails." 14 | spec.homepage = "https://github.com/jeromegn/dry-types-rails" 15 | spec.license = "MIT" 16 | 17 | # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or 18 | # delete this section to allow pushing this gem to any host. 19 | # if spec.respond_to?(:metatypes) 20 | # spec.metatypes['allowed_push_host'] = "https://rubygems.org" 21 | # else 22 | # raise "RubyGems 2.0 or newer is required to protect against public gem pushes." 23 | # end 24 | 25 | spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } 26 | spec.bindir = "exe" 27 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } 28 | spec.require_paths = ["lib"] 29 | 30 | spec.add_development_dependency "rake", "~> 10.0" 31 | spec.add_development_dependency 'combustion', '~> 0.5.4' 32 | spec.add_development_dependency 'rspec-rails', '~> 3.4.0' 33 | 34 | spec.add_runtime_dependency 'rails', '>= 3' 35 | spec.add_runtime_dependency 'dry-types', '>= 0.8.1' 36 | end 37 | -------------------------------------------------------------------------------- /lib/dry/types/rails.rb: -------------------------------------------------------------------------------- 1 | require "dry/types/rails/version" 2 | 3 | require 'dry/types' 4 | require 'dry/types/rails/railtie' 5 | 6 | module Dry 7 | module Types 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /lib/dry/types/rails/railtie.rb: -------------------------------------------------------------------------------- 1 | module Dry 2 | module Types 3 | module Rails 4 | module TypesRegistration 5 | REGISTERED_TYPES = Concurrent::Array.new 6 | 7 | def register(name, type = nil, &block) 8 | return super unless type 9 | return if TypesRegistration::REGISTERED_TYPES.include?(name) 10 | 11 | super.tap do 12 | # Check to see if we need to remove the registered type 13 | autoloaded = ActiveSupport::Dependencies.will_unload?(type) 14 | # ActiveSupport::Dependencies.will_unload?(klass) won't return true yet 15 | # if it's the first time a constant is being autoloaded 16 | # so we have to see if we're in the middle of loading a missing constants 17 | autoloaded |= caller.any? { |line| line =~ /\:in.*?new_constants_in/ } 18 | 19 | TypesRegistration::REGISTERED_TYPES << name if autoloaded 20 | end 21 | end 22 | end 23 | 24 | Dry::Types.module_eval do 25 | class << self 26 | prepend(TypesRegistration) 27 | end 28 | end 29 | 30 | class Railtie < ::Rails::Railtie 31 | config.to_prepare do 32 | types = TypesRegistration::REGISTERED_TYPES.dup 33 | types.each do |type| 34 | TypesRegistration::REGISTERED_TYPES.delete(type) 35 | type = Dry::Types.identifier(type) 36 | Dry::Types.container._container.delete(type) 37 | Dry::Types.type_map.delete(type) 38 | end 39 | end 40 | end 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /lib/dry/types/rails/version.rb: -------------------------------------------------------------------------------- 1 | module Dry 2 | module Types 3 | module Rails 4 | VERSION = "0.3.4" 5 | end 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /spec/dry/types/rails/railtie_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Dry::Types::Rails::Railtie do 4 | def clear_autoload 5 | ActionDispatch::Reloader.prepare! 6 | ActiveSupport::Dependencies.clear 7 | end 8 | 9 | before(:each) do |example| 10 | if example.metadata[:clear_autoload] 11 | clear_autoload 12 | end 13 | end 14 | 15 | describe "non-autoload" do 16 | context "types" do 17 | it "registers them" do 18 | expect(Types::Coercible::String[1234]).to eq("1234") 19 | end 20 | 21 | it "doesn't remove them", :clear_autoload do 22 | expect(Types::Coercible::String[1234]).to eq("1234") 23 | end 24 | end 25 | 26 | context "custom types" do 27 | it "registers them" do 28 | expect(CustomTypes::ZeroBox[nil]).to eq(0) 29 | end 30 | 31 | it "doesn't remove them", :clear_autoload do 32 | expect(CustomTypes::ZeroBox[nil]).to eq(0) 33 | end 34 | end 35 | 36 | context "structs" do 37 | it "registers them" do 38 | expect{ Dry::Types[SchemaStruct] }.to_not raise_error 39 | end 40 | 41 | it "doesn't remove them", :clear_autoload do 42 | expect(Dry::Types[SchemaStruct]).to_not be_nil 43 | end 44 | end 45 | end 46 | 47 | describe "autoload" do 48 | context "types" do 49 | it "loads them" do 50 | expect(Autoload::Types::Coercible::String[1234]).to eq("1234") 51 | end 52 | 53 | it "doesn't remove them" do 54 | expect(Autoload::Types::Coercible::String[1234]).to eq("1234") 55 | clear_autoload 56 | expect(Autoload::Types::Coercible::String[1234]).to eq("1234") 57 | end 58 | end 59 | 60 | context "custom types" do 61 | it "loads them" do 62 | expect(Autoload::CustomTypes::ZeroBox[nil]).to eq(0) 63 | end 64 | 65 | it "doesn't remove them", :clear_autoload do 66 | expect(Autoload::CustomTypes::ZeroBox[nil]).to eq(0) 67 | clear_autoload 68 | expect(Autoload::CustomTypes::ZeroBox[nil]).to eq(0) 69 | end 70 | end 71 | 72 | context "structs" do 73 | before(:each) do 74 | Autoload::SchemaStruct 75 | end 76 | 77 | it "registers them" do 78 | expect{ Dry::Types[Autoload::SchemaStruct] }.to_not raise_error 79 | end 80 | 81 | it "deregisters after clearing autoload" do 82 | identifier = Dry::Types.identifier(Autoload::SchemaStruct) 83 | expect(ActiveSupport::Dependencies.autoloaded?(Autoload::SchemaStruct)).to be_truthy 84 | 85 | expect{ Dry::Types[identifier] }.to_not raise_error 86 | clear_autoload 87 | expect{ Dry::Types[identifier] }.to raise_error(Dry::Container::Error) 88 | end 89 | 90 | it "doesn't retain a refernce to the previous class" do 91 | class Autoload::SchemaStruct 92 | def self.previous_ref 93 | true 94 | end 95 | end 96 | 97 | expect(Autoload::SchemaStruct).to respond_to(:previous_ref) 98 | expect(Dry::Types[Autoload::SchemaStruct]).to respond_to(:previous_ref) 99 | clear_autoload 100 | expect(Autoload::SchemaStruct).to_not respond_to(:previous_ref) 101 | expect(Dry::Types[Autoload::SchemaStruct]).to_not respond_to(:previous_ref) 102 | end 103 | end 104 | end 105 | end 106 | -------------------------------------------------------------------------------- /spec/internal/autoload-lib/autoload/custom_types.rb: -------------------------------------------------------------------------------- 1 | module Autoload 2 | module CustomTypes 3 | include Dry::Types.module 4 | 5 | ZeroBox = Coercible::Int.default(0) 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /spec/internal/autoload-lib/autoload/schema_struct.rb: -------------------------------------------------------------------------------- 1 | module Autoload 2 | class SchemaStruct < Dry::Types::Struct 3 | constructor_type :schema 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /spec/internal/autoload-lib/autoload/types.rb: -------------------------------------------------------------------------------- 1 | module Autoload 2 | module Types 3 | include Dry::Types.module 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /spec/internal/config/database.yml: -------------------------------------------------------------------------------- 1 | test: 2 | adapter: sqlite3 3 | database: db/combustion_test.sqlite 4 | -------------------------------------------------------------------------------- /spec/internal/config/routes.rb: -------------------------------------------------------------------------------- 1 | Rails.application.routes.draw do 2 | # 3 | end 4 | -------------------------------------------------------------------------------- /spec/internal/db/schema.rb: -------------------------------------------------------------------------------- 1 | ActiveRecord::Schema.define do 2 | # 3 | end 4 | -------------------------------------------------------------------------------- /spec/internal/lib/custom_types.rb: -------------------------------------------------------------------------------- 1 | module CustomTypes 2 | include Dry::Types.module 3 | 4 | ZeroBox = Coercible::Int.default(0) 5 | end 6 | -------------------------------------------------------------------------------- /spec/internal/lib/schema_struct.rb: -------------------------------------------------------------------------------- 1 | class SchemaStruct < Dry::Types::Struct 2 | constructor_type :schema 3 | end 4 | -------------------------------------------------------------------------------- /spec/internal/lib/types.rb: -------------------------------------------------------------------------------- 1 | module Types 2 | include Dry::Types.module 3 | end 4 | -------------------------------------------------------------------------------- /spec/internal/log/.gitignore: -------------------------------------------------------------------------------- 1 | *.log -------------------------------------------------------------------------------- /spec/internal/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jeromegn/dry-types-rails/012d9b97afb4dc5e0a8a236a74b5586a49cc0889/spec/internal/public/favicon.ico -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'bundler/setup' 3 | 4 | require 'combustion' 5 | require 'active_support/dependencies' 6 | 7 | Combustion.initialize! :action_controller, :action_view, :sprockets do 8 | config.eager_load = false 9 | config.cache_classes = false 10 | 11 | ActiveSupport::Dependencies.autoload_paths += Dir["#{config.root}/autoload-lib"] 12 | end 13 | 14 | require_relative "internal/lib/types" 15 | require_relative "internal/lib/custom_types" 16 | require_relative "internal/lib/schema_struct" 17 | 18 | require 'rspec/rails' 19 | 20 | RSpec.configure do |config| 21 | config.use_transactional_fixtures = true 22 | end 23 | --------------------------------------------------------------------------------