Class: Ginny::Attr

Inherits:
Object
  • Object
show all
Defined in:
lib/ginny/models/attr.rb

Overview

Used to generate an instance variable with getters/setters.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializevoid



26
27
28
# File 'lib/ginny/models/attr.rb', line 26

def initialize()
  self.read_only = false
end

Instance Attribute Details

#defaultString

Default value for the attribute; set in it's Class's initialize function.

Returns:

  • (String)


20
21
22
# File 'lib/ginny/models/attr.rb', line 20

def default
  @default
end

#descriptionString

Description of the attribute. Markdown is supported.

Returns:

  • (String)


14
15
16
# File 'lib/ginny/models/attr.rb', line 14

def description
  @description
end

#nameString

Name of the attribute.

Returns:

  • (String)


11
12
13
# File 'lib/ginny/models/attr.rb', line 11

def name
  @name
end

#read_onlyBoolean

If true, an attr_reader will be generated for the attribute instead of an attr_accessor.

Returns:

  • (Boolean)


23
24
25
# File 'lib/ginny/models/attr.rb', line 23

def read_only
  @read_only
end

#typeString

Type of the attribute.

Returns:

  • (String)


17
18
19
# File 'lib/ginny/models/attr.rb', line 17

def type
  @type
end

Class Method Details

.create(args = {}) ⇒ Attr

Constructor for an Attr. Use create, not new.

Parameters:

  • args (Hash) (defaults to: {})

Returns:



34
35
36
37
38
39
40
41
# File 'lib/ginny/models/attr.rb', line 34

def self.create(args = {})
  a = Ginny::Attr.new()
  a.name        = args[:name]
  a.description = args[:description]
  a.type        = args[:type]
  a.read_only   = args[:read_only]
  return a
end

.from_array(array) ⇒ Array<Ginny::Attr>

Parameters:

  • array (Array<Hash>)

Returns:



45
46
47
# File 'lib/ginny/models/attr.rb', line 45

def self.from_array(array)
  return array.map { |f| self.create(f) }
end

Instance Method Details

#renderString

Return generated code as a string.

Returns:

  • (String)


52
53
54
55
56
57
58
# File 'lib/ginny/models/attr.rb', line 52

def render()
  parts = []
  parts << (@description&.length&.positive? ? @description.comment : nil)
  parts << "@return [#{self.type}]".comment
  parts << "attr_#{self.read_only ? 'reader' : 'accessor'} :#{self.name.downcase}"
  return parts.compact.join("\n").gsub(/\s+$/, "")
end

#render_dynamicString

Used for documenting attributes that are "declared dynamically via meta-programming". See the documentation on YARD directives for more info.

Returns:

  • (String)


66
67
68
69
70
71
72
# File 'lib/ginny/models/attr.rb', line 66

def render_dynamic()
  parts = []
  parts << "@!attribute #{self.name.downcase} [#{self.read_only ? 'r' : 'rw'}]".comment
  parts << (@description&.length&.positive? ? @description.indent(2).comment : nil)
  parts << "@return [#{self.type}]".indent(2).comment
  return parts.compact.join("\n").gsub(/\s+$/, "")
end