Sha256: 192455a9defeb5a908f3a2ecda9ba88eda7acfa191f767e07e349b6d3dce2d4b

Contents?: true

Size: 1.37 KB

Versions: 5

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module RbsActivesupport
  class ClassAttribute
    attr_reader :name #: Symbol
    attr_reader :options #: Hash[untyped, untyped]

    # @rbs name: Symbol
    # @rbs options: Hash[untyped, untyped]
    def initialize(name, options) #: void
      @name = name
      @options = options
    end

    def type #: String
      # @type var trailng_comment: String?
      trailing_comment = options[:trailing_comment]
      if trailing_comment&.start_with?("#:")
        trailing_comment[2..].strip
      elsif default?
        default_type
      else
        "untyped"
      end
    end

    def instance_accessor? #: bool
      options.fetch(:instance_accessor, true)
    end

    def instance_reader? #: bool
      options.fetch(:instance_reader, instance_accessor?)
    end

    def instance_writer? #: bool
      options.fetch(:instance_writer, instance_accessor?)
    end

    def instance_predicate? #: bool
      options.fetch(:instance_predicate, true)
    end

    def default? #: boolish
      options.fetch(:default, nil)
    end

    def default_type #: String
      default = options.fetch(:default, nil)
      RbsActivesupport::Types.guess_type(default)
    end

    def public? #: bool
      !private?
    end

    def private? #: bool
      options.fetch(:private, false)
    end

    def included? #: bool
      options.fetch(:included, false)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rbs_activesupport-1.4.3 lib/rbs_activesupport/class_attribute.rb
rbs_activesupport-1.4.2 lib/rbs_activesupport/class_attribute.rb
rbs_activesupport-1.4.1 lib/rbs_activesupport/class_attribute.rb
rbs_activesupport-1.4.0 lib/rbs_activesupport/class_attribute.rb
rbs_activesupport-1.3.0 lib/rbs_activesupport/class_attribute.rb