Coder Social home page Coder Social logo

smart-rb / smart_initializer Goto Github PK

View Code? Open in Web Editor NEW
19.0 19.0 3.0 256 KB

A simple and convenient way to declare complex constructors with a support for various commonly used type systems.

License: MIT License

Ruby 99.91% Shell 0.09%
ruby-constructor ruby-initializer smart-rb

smart_initializer's People

Contributors

0exp avatar anotherregulardude avatar nulldef avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

smart_initializer's Issues

Verbose validation error logging

Hi!

First of all, thank you for the great job!

Our work scope needs a little more verbose logging, we want to see the processed object (attribute holder) id and also the attribute name.
I cannot argue with the fact that we could fulfil our needs by the stack trace, but it is quite challenging due to its' depth.

We might need something like this:

raise(SmartCore::Initializer::ThyTypeValidationError, <<~ERROR_MESSAGE)
  Incorrect type of #{attribute.name.inspect} attribute for #{instance.class}
  Expected: #{type.inspect}, but got: #{value.inspect}
ERROR_MESSAGE

We could use something like validation context and store it in attribute class, so public API won't change.

Add metadata to options

In some cases it would be useful to define metadata for options while initializing.

For example we can use SmartInitializer for describing relational database tables:

class Model
  include SmartCore::Initializer

  def self.column(...)
    option(...)
  end
end

class Author < Model
  column :id, Types::Integer, metadata: { primary_key: true }
  column :name, Types::String, metadata: { unique: true }
end

class Book < Model
  column :author, Types::Integer, metadata: { foreign_key: Author }
  column :title, Types::String
end

class CoveredBook < Book
  column :cover, Types::Base64Image
end

In that case some static metadata that can be inherited would be very useful.

Finalize param can't work with &:symbol notation

Is it by design?

> Proc.new{|obj|obj.x}.arity
=> 1
> Proc.new(&:x).arity
=> -2
> class A < SmartCore::Operation; param :text, finalize: Proc.new{|obj|obj.x}; end
=> nil
> class A < SmartCore::Operation; param :text, finalize: proc(&:x); end
/home/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/smart_initializer-0.9.0/lib/smart_core/initializer/attribute/factory/base.rb:95:in `prepare_finalize_param': Lambda-based finalizer should have arity equal to 1 or equal to -1 (your lambda object should require one attribute) (SmartCore::Initializer::ArgumentError)

В спеках инстанс даблы не проходят валидацию

Допустим, в сервисе есть какой-то параметр с валидацией типа:

param :example, Types::InstanceOf(Example)

Если в RSpec создать инстанс дабл:

instance_double(Example)

и попытаться передать его в сервис, то будет ошибка:

SmartCore::Initializer::IncorrectTypeError: Validation of attribute 'example' (InstanceOf, got RSpec::Mocks::InstanceVerifyingDouble) failed: Invalid type

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.