Class: Ginny::Attr
- Inherits:
-
Object
- Object
- Ginny::Attr
- Defined in:
- lib/ginny/models/attr.rb
Overview
Used to generate an instance variable with getters/setters.
Instance Attribute Summary collapse
-
#default ⇒ String
Default value for the attribute; set in it's Class's
initialize
function. -
#description ⇒ String
Description of the attribute.
-
#name ⇒ String
Name of the attribute.
-
#read_only ⇒ Boolean
If
true
, anattr_reader
will be generated for the attribute instead of anattr_accessor
. -
#type ⇒ String
Type of the attribute.
Class Method Summary collapse
-
.create(args = {}) ⇒ Attr
Constructor for an Attr.
- .from_array(array) ⇒ Array<Ginny::Attr>
Instance Method Summary collapse
- #initialize ⇒ void constructor
-
#render ⇒ String
Return generated code as a string.
-
#render_dynamic ⇒ String
Used for documenting attributes that are "declared dynamically via meta-programming".
Constructor Details
#initialize ⇒ void
26 27 28 |
# File 'lib/ginny/models/attr.rb', line 26 def initialize() self.read_only = false end |
Instance Attribute Details
#default ⇒ String
Default value for the attribute; set in it's Class's initialize
function.
20 21 22 |
# File 'lib/ginny/models/attr.rb', line 20 def default @default end |
#description ⇒ String
Description of the attribute. Markdown is supported.
14 15 16 |
# File 'lib/ginny/models/attr.rb', line 14 def description @description end |
#name ⇒ String
Name of the attribute.
11 12 13 |
# File 'lib/ginny/models/attr.rb', line 11 def name @name end |
#read_only ⇒ Boolean
If true
, an attr_reader
will be generated for the attribute instead of an attr_accessor
.
23 24 25 |
# File 'lib/ginny/models/attr.rb', line 23 def read_only @read_only end |
#type ⇒ String
Type of the attribute.
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
.
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>
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
#render ⇒ String
Return generated code as a 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_dynamic ⇒ String
Used for documenting attributes that are "declared dynamically via meta-programming". See the documentation on YARD directives for more info.
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 |