Sha256: e0462c58ac635c0d499c1632ab2ca019098c5fb0d137c4bce5e305ceef6e69f1

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

##
# Abstraction for an attribute to determine its name, reader, writer, and
# instance variable name.
class Attribool::Attribute
  ##
  # The name of the attribute, without the leading "@".
  #
  # @return [String]
  attr_reader :name

  ##
  # The name of the instance variable for the attribute, with the leading "@".
  #
  # @return [String]
  attr_reader :ivar

  ##
  # The name of the reader for the attribute.
  #
  # @return [String]
  attr_reader :reader

  ##
  # The name of the writer for the attribute.
  #
  # @return [String]
  attr_reader :writer

  ##
  # Ensures that if multiple attributes are being defined, and +method_name+ is
  # provided, that +method_name+ is a +Proc+.
  #
  # @param [Integer] number_of_attributes
  #
  # @param [String, Symbol, Proc] method_name
  def self.validate_method_name(number_of_attributes, method_name)
    if number_of_attributes > 1 && method_name && !method_name.is_a?(Proc)
      raise ArgumentError, "Must use a Proc when creating multiple methods"
    end
  end

  ##
  # Create an Attribute. The attribute can either be a String or a Symbol, and
  # can also start with an "@", or not.
  #
  # @param [String, Symbol] attribute
  #
  # @param [String, Symbol, Proc, nil] reader_name
  def initialize(attribute, reader_name = nil)
    attribute.to_s.tap do |a|
      @ivar = a.start_with?('@') ? a : "@#{a}"
      @name = @ivar.delete_prefix('@')
      @reader =
        case reader_name
        when Proc then reader_name.call(name)
        when nil then "#{name}?"
        else reader_name.to_s
        end
      @writer = "#{name}="
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
attribool-1.0.2 lib/attribool/attribute.rb
attribool-1.0.1 lib/attribool/attribute.rb