GithubHelp home page GithubHelp logo

Comments (3)

kgiszczak avatar kgiszczak commented on May 27, 2024

hey, you can achieve that using just plain Ruby, no need for special constructs:

class User < Shale::Mapper
  attribute :gender, Shale::Type::String

  def gender
    if super == 'f'
      'female'
    else
      'male'
    end
  end
end

although this behavior is confusing, the JSON doc you generate will be different then the one you're consuming (unless that's what you specifically need):

puts User.from_json('{"gender":"m"}').to_json
# => {"gender":"male"}

The proper way to solve this imo is to use custom type:

class Gender < Shale::Type::Value
  def self.of_json(value, **)
    value == 'm' ? 'male' : 'female'
  end

  def self.as_json(value, **)
    value == 'male' ? 'm' : 'f'
  end
end

class User < Shale::Mapper
  attribute :gender, Gender
end

This works as you would expect:

user = User.from_json('{"gender":"m"}')

puts user.gender
# => male

puts user.to_json
# => {"gender":"m"}

from shale.

glaucocustodio avatar glaucocustodio commented on May 27, 2024

Since I'm only parsing a json and not to converting to it back, I am gonna override the method like you showed:

class User < Shale::Mapper
  attribute :gender, Shale::Type::String

  def gender
    if super == 'f'
      'female'
    else
      'male'
    end
  end
end

Would you accept a PR describing this behaviour with its caveat on README?

from shale.

kgiszczak avatar kgiszczak commented on May 27, 2024

having that in README would be a great addition, PR is welcome

from shale.

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.