Giter Site home page Giter Site logo

Comments (4)

arianf avatar arianf commented on August 24, 2024

This is because if the type is not a recognized primitive, it defaults to Object, and coercing an object to an object will always work.

But I feel like if you have strict mode enabled, you should only be able to have the Class type you specified

from virtus.

celesteking avatar celesteking commented on August 24, 2024

Just came across this - what's the sense of specifying attribute type if that type isn't honored? That's a total disaster. You can assign random things to attribute and there's no casting done when you supply virtus model #new with a hash of stuff.

from virtus.

arianf avatar arianf commented on August 24, 2024

@celesteking you should check out https://github.com/AaronLasseigne/active_interaction

It’s a much better implementation

from virtus.

celesteking avatar celesteking commented on August 24, 2024

Me? Nope, because it's 3rd party (elasticsearch-persistence) that's using this stale software.
Anyways, I've managed to find a workaround:

require 'ipaddr'

class Mymodel
  include Virtus.model

  class TypeCoercer
    define_singleton_method(:[]) do |cast_type|
      Class.new(Virtus::Attribute).class_eval do
        define_method(:coerce) do |value|
          value && cast_type.new(value)
        end
        self
      end
    end
  end

  attribute :myattr, TypeCoercer[IPAddr], default: nil
end

puts Mymodel.new(myattr: '1.2.3.4').myattr.class

VIRTUS CUSTOM COERCION NOT WORKING attribute not coercing

from virtus.

Related Issues (20)

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.