Class: Peeky::AttrInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/peeky/attr_info.rb

Overview

Attr Info is a container that holds read, write or read/write attributes in the form of MethodInfo objects

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader: nil, writer: nil) ⇒ AttrInfo

Returns a new instance of AttrInfo.


12
13
14
15
16
17
# File 'lib/peeky/attr_info.rb', line 12

def initialize(reader: nil, writer: nil)
  guard(reader, writer)

  @reader = reader
  @writer = writer
end

Instance Attribute Details

#readerObject (readonly)

Returns the value of attribute reader


9
10
11
# File 'lib/peeky/attr_info.rb', line 9

def reader
  @reader
end

#writerObject (readonly)

Returns the value of attribute writer


10
11
12
# File 'lib/peeky/attr_info.rb', line 10

def writer
  @writer
end

Class Method Details

.create(*method_infos) ⇒ Object

Factory method that will create an AttrInfo from a list of method_infos

There is an expectation that the list will have 1 or 2 method_infos and that they will be of type :attr_reader and/or :attr_writer Currently there are some edge cases that I can see where this may not be true and in those cases we just ignore those cases


37
38
39
40
41
42
# File 'lib/peeky/attr_info.rb', line 37

def self.create(*method_infos)
  reader = method_infos.find(&:readable?)
  writer = method_infos.find(&:writable?)

  new(reader: reader, writer: writer)
end

Instance Method Details

#nameObject


27
28
29
# File 'lib/peeky/attr_info.rb', line 27

def name
  @name ||= @reader.nil? ? @writer.clean_name : @reader.clean_name
end

#typeObject


19
20
21
22
23
24
25
# File 'lib/peeky/attr_info.rb', line 19

def type
  @type ||= if @reader.nil?
              :attr_writer
            else
              @writer.nil? ? :attr_reader : :attr_accessor
            end
end