ginny 0.5.4 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -1
- data/Gemfile.lock +1 -1
- data/README.md +22 -9
- data/lib/ginny/models/class.rb +35 -1
- data/lib/ginny/models/param.rb +2 -2
- data/lib/ginny/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5954674ccc9c88509fe6c907bfe4f23d13c6da96131a135c34374e7f6050f619
|
4
|
+
data.tar.gz: 31d45ba5e68259fd278950701c8caefc2799159f7dcaa265a25b2c3f19312af0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 454f12a2ebe56984619972409e81074799d2c94df3de76ce86ee2a13f105858c77c13797f9fc024e07b5c7f29e1df6e7d73f2a0f38f51c4a86474369a65724f8
|
7
|
+
data.tar.gz: 21d370801870b1223e3b470ded669a7149361228393917d1009a01166358c35aaaa19ce0b95564dd82413b4c39fd223cf6a08d165c30342d5b8fd683d3714b73
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,14 @@
|
|
2
2
|
|
3
3
|
<!-- ## master (unreleased) -->
|
4
4
|
|
5
|
+
## 0.6.0 (2019-12-14)
|
6
|
+
|
7
|
+
### Added
|
8
|
+
|
9
|
+
- Add `default_constructor` option for `Ginny::Class`, which will generate a method similar to [ActiveRecord::Base.create][create_method_link] for the class.
|
10
|
+
|
11
|
+
[create_method_link]: https://apidock.com/rails/ActiveRecord/Persistence/ClassMethods/create
|
12
|
+
|
5
13
|
## 0.5.4 (2019-12-09)
|
6
14
|
|
7
15
|
### Changed
|
@@ -42,7 +50,7 @@
|
|
42
50
|
### Added
|
43
51
|
|
44
52
|
- Add [dry-rb/dry-inflector](https://github.com/dry-rb/dry-inflector) dependency.
|
45
|
-
- Add `file_prefix`
|
53
|
+
- Add `file_prefix` option to `Ginny::Class` for a string to prepend to the name of the generated file.
|
46
54
|
|
47
55
|
### Changed
|
48
56
|
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -44,6 +44,7 @@ name: Human
|
|
44
44
|
description: This class models a person.
|
45
45
|
modules: [MilkyWay, Earth]
|
46
46
|
parent: Mammal
|
47
|
+
default_constructor: true
|
47
48
|
attrs:
|
48
49
|
- name: Name
|
49
50
|
type: String
|
@@ -51,7 +52,6 @@ attrs:
|
|
51
52
|
description: Number of years the human has been alive.
|
52
53
|
type: Integer
|
53
54
|
read_only: true
|
54
|
-
|
55
55
|
```
|
56
56
|
|
57
57
|
```shell
|
@@ -68,6 +68,7 @@ data = {
|
|
68
68
|
description: "This class models a person.",
|
69
69
|
modules: ["MilkyWay", "Earth"],
|
70
70
|
parent: "Mammal",
|
71
|
+
default_constructor: true,
|
71
72
|
attrs: [
|
72
73
|
{ name: "name", type: "String" },
|
73
74
|
{ name: "age", type: "Integer" read_only: true, description: "Number of years the human has been alive." },
|
@@ -90,6 +91,15 @@ module MilkyWay
|
|
90
91
|
# Number of years the human has been alive.
|
91
92
|
# @return [Integer]
|
92
93
|
attr_reader :age
|
94
|
+
|
95
|
+
# @param params [Hash]
|
96
|
+
# @return [self]
|
97
|
+
def self.create(params = {})
|
98
|
+
h = Human.new
|
99
|
+
h.name = params[:name]
|
100
|
+
h.age = params[:age]
|
101
|
+
return h
|
102
|
+
end
|
93
103
|
end
|
94
104
|
end
|
95
105
|
end
|
@@ -99,14 +109,17 @@ end
|
|
99
109
|
|
100
110
|
### `Ginny::Class`
|
101
111
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
| name (required)
|
105
|
-
| description
|
106
|
-
| body
|
107
|
-
| parent
|
108
|
-
| modules
|
109
|
-
|
|
112
|
+
| Name | Type | Description |
|
113
|
+
| ------------------- | -------------------- | --------------------------------------------------------------------------------------------------------------- |
|
114
|
+
| name (required) | `String` | Name of the class. |
|
115
|
+
| description | `String` | Description of the class. [Markdown][markdown] is supported. |
|
116
|
+
| body | `String` | String to write into the body of the class. |
|
117
|
+
| parent | `String` | Name of a class to inherit from. (Ex: `YourNewClass < Parent`) |
|
118
|
+
| modules | `Array<String>` | List of modules to declare the class inside of |
|
119
|
+
| default_constructor | `Boolean` | If `true`, a method similar to [ActiveRecord::Base.create][create_method_link] will be generated for the class. |
|
120
|
+
| attrs | `Array<Ginny::Attr>` | An array of Attrs. |
|
121
|
+
|
122
|
+
[create_method_link]: https://apidock.com/rails/ActiveRecord/Persistence/ClassMethods/create
|
110
123
|
|
111
124
|
### `Ginny::Attr`
|
112
125
|
|
data/lib/ginny/models/class.rb
CHANGED
@@ -22,6 +22,9 @@ module Ginny
|
|
22
22
|
# String to write into the body of the class.
|
23
23
|
# @return [String]
|
24
24
|
attr_accessor :body
|
25
|
+
# If `true`, a method similar to [ActiveRecord::Base.create](https://apidock.com/rails/ActiveRecord/Persistence/ClassMethods/create) will be generated for the class.
|
26
|
+
# @return [Boolean]
|
27
|
+
attr_accessor :default_constructor
|
25
28
|
# String to prepend to the name of the generated file.
|
26
29
|
# @return [String]
|
27
30
|
attr_accessor :file_prefix
|
@@ -31,6 +34,8 @@ module Ginny
|
|
31
34
|
self.attrs = []
|
32
35
|
self.modules = []
|
33
36
|
self.file_prefix = ""
|
37
|
+
self.body = ""
|
38
|
+
self.default_constructor = false
|
34
39
|
end
|
35
40
|
|
36
41
|
# Constructor for a Class. Use `create`, not `new`.
|
@@ -46,6 +51,7 @@ module Ginny
|
|
46
51
|
c.attrs = Ginny::Attr.from_array(args[:attrs]) if args[:attrs]&.is_a?(Array)
|
47
52
|
c.body = args[:body] unless args[:body].nil?
|
48
53
|
c.file_prefix = args[:file_prefix] || ""
|
54
|
+
c.default_constructor = args[:default_constructor]
|
49
55
|
return c
|
50
56
|
end
|
51
57
|
|
@@ -67,7 +73,8 @@ module Ginny
|
|
67
73
|
parts << (self.description&.length&.positive? ? self.description.comment.strip : nil)
|
68
74
|
parts << (self.parent.nil? ? "class #{self.class_name()}" : "class #{self.class_name()} < #{self.parent}")
|
69
75
|
parts << self.render_attributes()
|
70
|
-
parts <<
|
76
|
+
parts << self.render_body()
|
77
|
+
# parts << (self.body&.length&.positive? ? self.body.indent(2) : nil)
|
71
78
|
parts << "end"
|
72
79
|
if self.modules.length > 0
|
73
80
|
body = parts.compact.join("\n").gsub(/([[:blank:]]+)$/, "")
|
@@ -76,6 +83,19 @@ module Ginny
|
|
76
83
|
return parts.compact.join("\n").gsub(/([[:blank:]]+)$/, "")
|
77
84
|
end
|
78
85
|
|
86
|
+
# @return [String,nil]
|
87
|
+
def render_body()
|
88
|
+
if self.body.length > 0
|
89
|
+
if self.default_constructor
|
90
|
+
return ("\n" + self.constructor() + "\n\n" + self.body).indent(2)
|
91
|
+
end
|
92
|
+
return self.body.indent(2)
|
93
|
+
end
|
94
|
+
# binding.pry
|
95
|
+
return "\n" + self.constructor().indent(2) if self.default_constructor
|
96
|
+
return nil
|
97
|
+
end
|
98
|
+
|
79
99
|
# @return [String]
|
80
100
|
def render_attributes()
|
81
101
|
return nil unless self.attrs.length > 0
|
@@ -100,5 +120,19 @@ module Ginny
|
|
100
120
|
return self.file_prefix + inflector.underscore(self.name) + ".rb"
|
101
121
|
end
|
102
122
|
|
123
|
+
# @return [String]
|
124
|
+
def constructor()
|
125
|
+
char = self.name.chr.downcase
|
126
|
+
body = "#{char} = #{self.class_name}.new\n"
|
127
|
+
body << self.attrs.map { |a| "#{char}.#{a.name} = params[:#{a.name}]\n" }.join()
|
128
|
+
body << "return #{char}\n"
|
129
|
+
Ginny::Func.create({
|
130
|
+
name: "self.create",
|
131
|
+
return_type: "self",
|
132
|
+
params: [{ name: "params", type: "Hash", default: {} }],
|
133
|
+
body: body,
|
134
|
+
}).render()
|
135
|
+
end
|
136
|
+
|
103
137
|
end
|
104
138
|
end
|
data/lib/ginny/models/param.rb
CHANGED
@@ -20,10 +20,10 @@ module Ginny
|
|
20
20
|
attr_accessor :default
|
21
21
|
# If `true`, the default value will be `nil`.
|
22
22
|
# FIXME: This is a workaround for the fact that that passing `nil` to `default` messes with conditionals. Not sure of a simpler way to do this.
|
23
|
-
# @return [Boolean]
|
23
|
+
# @return [Boolean] (false)
|
24
24
|
attr_accessor :optional
|
25
25
|
# If `true`, the param will be generated as a [keyword argument](https://bugs.ruby-lang.org/issues/14183).
|
26
|
-
# @return [Boolean]
|
26
|
+
# @return [Boolean] (false)
|
27
27
|
attr_accessor :keyword
|
28
28
|
|
29
29
|
# Constructor for a Param. Use `create`, not `new`.
|
data/lib/ginny/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ginny
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Clay Dunston
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-12-
|
11
|
+
date: 2019-12-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -191,7 +191,7 @@ licenses:
|
|
191
191
|
metadata:
|
192
192
|
homepage_uri: https://github.com/tcd/ginny
|
193
193
|
source_code_uri: https://github.com/tcd/ginny
|
194
|
-
documentation_uri: https://www.rubydoc.info/gems/ginny/0.
|
194
|
+
documentation_uri: https://www.rubydoc.info/gems/ginny/0.6.0
|
195
195
|
changelog_uri: https://github.com/tcd/ginny/blob/master/CHANGELOG.md
|
196
196
|
yard.run: yri
|
197
197
|
post_install_message:
|