Class: Rubyzen::Declarations::AttributeDeclaration

Inherits:
Object
  • Object
show all
Includes:
Providers::ClassNameProvider, Providers::FilePathProvider, Providers::LineNumberProvider, Providers::VisibilityProvider
Defined in:
lib/rubyzen/declarations/attribute_declaration.rb

Overview

Represents an attr_reader, attr_writer, or attr_accessor declaration.

Examples:

attr = klass.attributes.first
attr.name      #=> "attr_reader"
attr.symbols   #=> ["name", "email"]
attr.reader?   #=> true
attr.private?  #=> false

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Providers::VisibilityProvider

#private?, #protected?, #public?, #visibility

Methods included from Providers::LineNumberProvider

#line

Methods included from Providers::ClassNameProvider

#class_name

Methods included from Providers::FilePathProvider

#file_path

Constructor Details

#initialize(node, parent_class) ⇒ AttributeDeclaration

Returns a new instance of AttributeDeclaration.

Parameters:



27
28
29
30
# File 'lib/rubyzen/declarations/attribute_declaration.rb', line 27

def initialize(node, parent_class)
  @node = node
  @parent_class = parent_class
end

Instance Attribute Details

#nodeRuboCop::AST::Node (readonly)

Returns:

  • (RuboCop::AST::Node)


19
20
21
# File 'lib/rubyzen/declarations/attribute_declaration.rb', line 19

def node
  @node
end

#parent_classClassDeclaration, ModuleDeclaration (readonly) Also known as: parent



22
23
24
# File 'lib/rubyzen/declarations/attribute_declaration.rb', line 22

def parent_class
  @parent_class
end

Instance Method Details

#accessor?Boolean

Returns true only for attr_accessor.

Returns:

  • (Boolean)

    true only for attr_accessor



57
58
59
# File 'lib/rubyzen/declarations/attribute_declaration.rb', line 57

def accessor?
  name == 'attr_accessor'
end

#nameString

Returns the attribute type name.

Returns:

  • (String)

    one of “attr_reader”, “attr_writer”, “attr_accessor”



35
36
37
# File 'lib/rubyzen/declarations/attribute_declaration.rb', line 35

def name
  node.method_name.to_s
end

#reader?Boolean

Returns true for attr_reader and attr_accessor.

Returns:

  • (Boolean)

    true for attr_reader and attr_accessor



47
48
49
# File 'lib/rubyzen/declarations/attribute_declaration.rb', line 47

def reader?
  %w[attr_reader attr_accessor].include?(name)
end

#symbolsArray<String>

Returns the declared symbol names.

Returns:

  • (Array<String>)

    e.g. [“name”, “email”]



42
43
44
# File 'lib/rubyzen/declarations/attribute_declaration.rb', line 42

def symbols
  node.arguments.map { |arg| arg.value.to_s if arg.type == :sym }.compact
end

#writer?Boolean

Returns true for attr_writer and attr_accessor.

Returns:

  • (Boolean)

    true for attr_writer and attr_accessor



52
53
54
# File 'lib/rubyzen/declarations/attribute_declaration.rb', line 52

def writer?
  %w[attr_writer attr_accessor].include?(name)
end