zaikio / serialize_attributes Goto Github PK
View Code? Open in Web Editor NEWSerialize ActiveModel attributes in JSON using type casting
License: MIT License
Serialize ActiveModel attributes in JSON using type casting
License: MIT License
serialize_attributes
doesn't add the same predicated method on boolean field that the regular rails attributes have. This might be a nice little addition, so one doesn't have to remember which one, one is using.
Currently:
adhesive = Articles::AdhesiveTape.new(double_sided: true)
adhesive.double_sided? # => NoMethodError: undefined method `double_sided?'
Suggested:
adhesive = Articles::AdhesiveTape.new(double_sided: true)
adhesive.double_sided? # => true
Rails' ActiveModel Type names somehow confusingly BigDecimal
in Decimal
.
This pattern creates friction when creating product. The Open API doesn't know of a Decimal
type. This adds an extra step to the creation of product.
One way to solve this issue would be to rename decimal
into big_decimal
inside SerizalizeAttribute. This would match the actual Ruby class and avoid trouble with OpenAPI.
Another possible option would be to change the generator to do the conversion from decimal
to big_decimal
class Model
serialize_attributes(:opts) do
attribute :thickness, :decimal
end
end
object = Model.new
object.thickness = BigDecimal("0.44")
object.opts
#=> { "thickness" => "0.44" }
object.thickness
#=> "0.44"
This does work if the record is saved to the database and reloaded though...
object = Model.first
object.thickness
#=> BigDecimal("0.44")
We thought we had completely fixed BigDecimal
typecasting with #37, but the beast is still alive.
variant = Variant.new(decimal: 42.0)
variant.decimal #=> BigDecimal("42.0") # All good!
variant.whatever = "chunky bacon"
variant.decimal #=> "42.0" # Doh!
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.