Class: RubyLint::MethodCall::Attribute

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby-lint/method_call/attribute.rb

Overview

The Attribute class is used for evaluating method calls to attr, attr_reader and similar methods.

Instance Attribute Summary

Attributes inherited from Base

#node, #vm

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from RubyLint::MethodCall::Base

Instance Method Details

#define_attribute(name, context, setter = false) ⇒ Object (private)

Parameters:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ruby-lint/method_call/attribute.rb', line 81

def define_attribute(name, context, setter = false)
  ivar_name = '@' + name

  if setter
    name = name + '='
  end

  context.define_instance_method(name) do |method|
    method.define_argument('value') if setter
  end

  unless context.has_definition?(:ivar, ivar_name)
    ivar = Definition::RubyObject.new(
      :type             => :ivar,
      :name             => ivar_name,
      :reference_amount => 1
    )

    context.add_definition(ivar)
  end
end

#evaluate(arguments, context, block = nil) ⇒ Object

See Also:



11
12
13
14
15
# File 'lib/ruby-lint/method_call/attribute.rb', line 11

def evaluate(arguments, context, block = nil)
  method = "evaluate_#{node.children[1]}"

  send(method, arguments, context)
end

#evaluate_attr(arguments, context) ⇒ Object (private)

Evaluates a call to attr. The attr method can be used in two different ways (thank you Ruby for being consistent):

  1. attr [NAME], [TRUE|FALSE]
  2. `attr [NAME], [NAME], etc

See Also:



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby-lint/method_call/attribute.rb', line 28

def evaluate_attr(arguments, context)
  if arguments[1] and arguments[1].type == :true
    names = [arguments[0].value.to_s, arguments[0].value.to_s + '=']
  else
    names = arguments.map { |arg| arg.value.to_s }
  end

  names.each do |name|
    define_attribute(name, context)
  end
end

#evaluate_attr_accessor(arguments, context) ⇒ Object (private)

Evaluates method calls to attr_accessor.

See Also:



67
68
69
70
71
72
73
74
# File 'lib/ruby-lint/method_call/attribute.rb', line 67

def evaluate_attr_accessor(arguments, context)
  arguments.each do |arg|
    name = arg.value.to_s

    define_attribute(name, context)
    define_attribute(name, context, true)
  end
end

#evaluate_attr_reader(arguments, context) ⇒ Object (private)

Evaluates method calls to attr_reader.

See Also:



45
46
47
48
49
# File 'lib/ruby-lint/method_call/attribute.rb', line 45

def evaluate_attr_reader(arguments, context)
  arguments.each do |arg|
    define_attribute(arg.value.to_s, context)
  end
end

#evaluate_attr_writer(arguments, context) ⇒ Object (private)

Evaluates method calls to attr_writer.

See Also:



56
57
58
59
60
# File 'lib/ruby-lint/method_call/attribute.rb', line 56

def evaluate_attr_writer(arguments, context)
  arguments.each do |arg|
    define_attribute(arg.value.to_s, context, true)
  end
end