rbs_active_hash 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '08c6d368bbd5d2a88c358679a5fe2d4e0e6330602cad2a593d38d0b66174356f'
4
- data.tar.gz: e18518e269dcad30a34795519bb745206eee48083d6bc3d7d3c35326d7946f76
3
+ metadata.gz: a40c37520928e6b4e5f6049311691c16966c1819008648702b9fdfa7abb0a156
4
+ data.tar.gz: c2d4212d760b5970ae09919641a88cdca6bd4469e12271a545d7226692281b99
5
5
  SHA512:
6
- metadata.gz: c380fd3d043cadebb606d6e2acfe47399c49d663392bba6fcd2f2c1b58277363039f7449fa848deb299bfca35b104a62ab4318b7114e863810e38518ed6bdef5
7
- data.tar.gz: 722aabe39f2f61c67562476aa6dd4af2a3acb908797ab1a8edc1913b6f8234761aaa23475c1a77e7395c7f03b01256a592892200a6c8c994c0bc895ddee71113
6
+ metadata.gz: cf19e092d7dac5139dd6ba69bc136e692efc0b4dd6624f084e24de9a57327ec32bb4dcd777c7da6b25586925c4103738bf2796f962db200f5077ad7356f9ff59
7
+ data.tar.gz: c9a1fc5bcf3c671f2cc4954b0639ea13a461318583d55434e3bbe58f7036594b215d587b59eddedb89f5db6702cb5451c64ec0f45bcaa0cb262bf2b8b79e8941
data/.rubocop.yml CHANGED
@@ -18,3 +18,9 @@ Layout/LineLength:
18
18
  Metrics/BlockLength:
19
19
  Exclude:
20
20
  - spec/**/*_spec.rb
21
+
22
+ Metrics/MethodLength:
23
+ Max: 20
24
+
25
+ Metrics/PerceivedComplexity:
26
+ Max: 10
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rbs_active_hash (0.1.1)
4
+ rbs_active_hash (1.0.0)
5
5
  active_hash
6
6
  rbs_rails
7
7
 
@@ -85,12 +85,13 @@ module RbsActiveHash
85
85
 
86
86
  def method_decls
87
87
  method_names.map do |method|
88
+ method_type = stringify_type(method_types.fetch(method, "untyped"))
88
89
  <<~RBS
89
- def #{method}: () -> untyped
90
- def #{method}=: (untyped value) -> void
90
+ def #{method}: () -> #{method_type}
91
+ def #{method}=: (#{method_type} value) -> void
91
92
  def #{method}?: () -> bool
92
- def self.find_by_#{method}: (untyped value) -> self?
93
- def self.find_all_by_#{method}: (untyped value) -> Array[self]
93
+ def self.find_by_#{method}: (#{method_type} value) -> self?
94
+ def self.find_all_by_#{method}: (#{method_type} value) -> Array[self]
94
95
  RBS
95
96
  end.join("\n")
96
97
  end
@@ -102,6 +103,16 @@ module RbsActiveHash
102
103
  method_names.uniq.select { |k| k != :id && valid_field_name?(k) }
103
104
  end
104
105
 
106
+ def method_types
107
+ method_types = Hash.new { |hash, key| hash[key] = [] }
108
+ (klass.data || []).each do |record|
109
+ record.symbolize_keys.each do |key, value|
110
+ method_types[key] << value.class
111
+ end
112
+ end
113
+ method_types.transform_values(&:uniq)
114
+ end
115
+
105
116
  def valid_field_name?(name)
106
117
  name.to_s =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/
107
118
  end
@@ -110,6 +121,25 @@ module RbsActiveHash
110
121
  "end\n" * klass.module_parents.size
111
122
  end
112
123
 
124
+ def stringify_type(type)
125
+ if [TrueClass, FalseClass].include?(type)
126
+ "bool"
127
+ elsif type == NilClass
128
+ "nil"
129
+ elsif type.is_a? Class
130
+ type.name
131
+ elsif type.is_a? Array
132
+ types = type.map { |t| stringify_type(t) }.uniq.sort
133
+ if types.delete("nil")
134
+ "(#{types.join(" | ")})?"
135
+ else
136
+ "(#{types.join(" | ")})"
137
+ end
138
+ else
139
+ type.to_s
140
+ end
141
+ end
142
+
113
143
  attr_reader :klass
114
144
  end
115
145
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RbsActiveHash
4
- VERSION = "0.1.1"
4
+ VERSION = "1.0.0"
5
5
  end
@@ -16,10 +16,13 @@ module RbsActiveHash
16
16
  def enum_decls: () -> String?
17
17
  def constants: () -> Array[String]
18
18
  def method_decls: () -> String
19
- def method_names: () -> Array[String]
19
+ def method_names: () -> Array[Symbol]
20
+ def method_types: () -> Hash[Symbol, untyped]
20
21
  def valid_field_name?: (String) -> boolish
21
22
  def footer: () -> String
22
23
 
24
+ def stringify_type: (untyped type) -> String
25
+
23
26
  attr_reader klass: Class
24
27
  end
25
28
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbs_active_hash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takeshi KOMIYA
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-11 00:00:00.000000000 Z
11
+ date: 2023-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: active_hash