Skip to content

Commit

Permalink
Add class body generation
Browse files Browse the repository at this point in the history
  • Loading branch information
tcd committed Nov 21, 2019
1 parent 376b17e commit 95b4ab8
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
## master (unreleased)


## 0.4.0 (2019-11-21)

### Added

- Add `body` argument to `Ginny::Class` for code to be generated in the class body.

## 0.3.0 (2019-11-21)

### Added

- Add the option to wrap generated code in modules.

## 0.2.1 (2019-11-18)
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
ginny (0.3.0)
ginny (0.4.0)

GEM
remote: https://rubygems.org/
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ end
| --------------- | -------------------- | -------------------------------------------------------------- |
| name (required) | `String` | Name of the class. |
| description | `String` | Description of the class. [Markdown][markdown] is supported. |
| body | `String` | String to write into the body of the class. |
| parent | `String` | Name of a class to inherit from. (Ex: `YourNewClass < Parent`) |
| modules | `Array<String>` | List of modules to declare the class inside of |
| attrs | `Array<Ginny::Attr>` | An array of Attrs. |
Expand Down
5 changes: 5 additions & 0 deletions lib/ginny/models/class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class Class
# An array of {Ginny::Attr}s.
# @return [Array<Ginny::Attr>]
attr_accessor :attrs
# String to write into the body of the class.
# @return [String]
attr_accessor :body

# @return [void]
def initialize()
Expand All @@ -35,6 +38,7 @@ def self.create(args = {})
c.parent = args[:parent]
c.modules = args[:modules] unless args[:modules].nil?
c.attrs = Ginny::Attr.from_array(args[:attrs]) if args[:attrs]&.is_a?(Array)
c.body = args[:body] unless args[:body].nil?
return c
end

Expand All @@ -53,6 +57,7 @@ def render()
parts << (self.description&.length&.positive? ? self.description.comment.strip : nil)
parts << (self.parent.nil? ? "class #{self.name}" : "class #{self.name} < #{self.parent}")
parts << self.render_attributes()
parts << (self.body&.length&.positive? ? self.body.indent(2) : nil)
parts << "end"
if self.modules.length > 0
body = parts.compact.join("\n").gsub(/\s+$/, "")
Expand Down
2 changes: 1 addition & 1 deletion lib/ginny/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Ginny
VERSION = "0.3.0".freeze
VERSION = "0.4.0".freeze
end
47 changes: 47 additions & 0 deletions test/ginny/class_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,51 @@ class Human < Mammal
assert_equal(want.strip, have.strip)
end

def test_class_with_body
want = <<~RUBY.strip
module C4
module Elements
# - Id: 112
# - Name: Pier Name
# - Type: AN
# - Min/Max: 2/14
# - Description: Free-form name of the pier.
class PierName < C4::Element::AN
# @return [void]
def initialize()
@id = 112
@name = "Pier Name"
@type = "AN"
self.min = 2
self.max = 14
end
end
end
end
RUBY
have = Ginny::Class.create(
name: "PierName",
parent: "C4::Element::AN",
modules: ["C4", "Elements"],
description: <<~STRING.strip,
- Id: 112
- Name: Pier Name
- Type: AN
- Min/Max: 2/14
- Description: Free-form name of the pier.
STRING
body: <<~RUBY.strip,
# @return [void]
def initialize()
@id = 112
@name = "Pier Name"
@type = "AN"
self.min = 2
self.max = 14
end
RUBY
).render()
assert_equal(want.strip, have.strip)
end

end

0 comments on commit 95b4ab8

Please sign in to comment.