smart-rb / smart_initializer Goto Github PK
View Code? Open in Web Editor NEWA simple and convenient way to declare complex constructors with a support for various commonly used type systems.
License: MIT License
A simple and convenient way to declare complex constructors with a support for various commonly used type systems.
License: MIT License
Relates to the type-redifinition logic (shadowing of an existing type)
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.
🤔 optional attribute was not provided (and this attrbite has no provided :default option) => finalize is not called
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.
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
Возникает ошибка(Неверно количество аргументов), если создать option с название :send - пытается переписать дефолтный метод send.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.