Giter Site home page Giter Site logo

private_attrs's Introduction

PrivateAttrs

Build Status

Introducing Private Attrs

The private_attrs gem is a quick addition to Ruby’s Class object that allows for defining private attribute methods.

Usage

class Crocodile
  private_attr_reader :temper

  def initialize(temper)
    @temper = temper
  end
end
Crocodile.new('angry').temper # => NoMethodError "private method 'temper' called for ...

But, why?

In short, because existing patterns to do this are ugly. This particular pattern violates the ‘Scissors Rule' of coding in that there is not a clear separation between public and private methods.

class Crocodile
  private

  attr_reader :temper

  public

  attr_reader :asleep

  def initialize(temper)
    @temper = temper
  end

  def bites?
    if angry? && !asleep?
  end

  private

  def angry?
    temper == 'angry'
  end

  def asleep?
    current_time = Time.now
    (current_time.hour >= 17) and (current_time.hour <= 21)
  end
end

This example is better but still not ideal as we want all of our attribute methods defined at the top of any given class.

class Truck

  attr_reader :transmission_type
  attr_reader :weight

  def initialize(wheel_count:, transmission_type:, weight:)
    @wheel_count = wheel_count
    @transmission_type = transmission_type
    @weight =  weight
  end

  def oversized
    wheel_count > 4
  end

  def driver_must_shift?
    transmission_type == :manual
  end

  private

  attr_reader :wheel_count
end

Other patterns are even worse…

class Pet

  attr_accessor :species
  private :species
  private :species=

  attr_reader :hairy
  private :hairy

  def initialize(species, hairy)
    #...
  end
end

Installation

Add this line to your application's Gemfile:

gem 'private_attrs'

And then execute:

$ bundle

Or install it yourself as:

$ gem install private_attrs

License

The gem is available as open source under the terms of the MIT License.

private_attrs's People

Contributors

wzcolon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

private_attrs's Issues

QUESTION: differences between this and jswanner/private_attr

Not an issue so much as a comment that I think would help this library. I'm also interested in introducing this pattern, and it looks like there's an older (and possibly unmaintained) implementation at https://github.com/jswanner/private_attr .

Are there any features this library provides that private_attr does not (or vice versa)? It might help to at least disambiguate between the two in the README, as anyone who finds this repo organically will have likely stumbled across the other as well.

Thanks!

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.