meta-api 0.0.7 → 0.0.8

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c114ab0d95b902ecefcd5e720f0c38fbcc420b2839194842c2974d2d073505fd
4
- data.tar.gz: 994b7e975626f9907467edfe4283875aa113957edc860b74ed1b29e27560f196
3
+ metadata.gz: 6b35c78915baab28213ceb9f2b6184c8ae177742ce0524529c681132fa6e0390
4
+ data.tar.gz: bc4ca96b46e0284f10fa41360ef4308aa7378121ead9f0cd1a536ac027cd590e
5
5
  SHA512:
6
- metadata.gz: 6b62b24879f309c6cc67e45bc9bfd250b47ec8ad6c25f5fe1e9f5098b970ff8466f239b8799b4e653ebdc5568cbbb87fdd7c086703a99ef8525945ce24c44617
7
- data.tar.gz: edec25f4ea37588611af7dceca41a443c5cb6d83586096f51f9f27ce75fdf290ec7085e4f27bf87043779e1ed289f7cbaa0a7e9568839e2ed206eef1d7d4f4b7
6
+ metadata.gz: c5cf6b6c1ee9ba5af2826b6682e57a357bbd74caf7807a5f3f9cbd124b99bdce4bcc7cba7040b45b2c1d62b9abfda1e4ee6c47853c2ebcb57bfd2d8a224db622
7
+ data.tar.gz: 5043cc4c8516d7c6bc827d32889e26a2164299ba0a71b1439bd1594aabdd1975bbd1994139fed2ffca93409e9c1f2118157301b3cef502767750c52b061c7367
data/Gemfile CHANGED
@@ -5,6 +5,7 @@ gemspec
5
5
 
6
6
  gem "rake", "~> 12.0"
7
7
  gem 'i18n'
8
+ gem 'hash_to_struct'
8
9
 
9
10
  group :test do
10
11
  gem 'rspec'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- meta-api (0.0.7)
4
+ meta-api (0.0.8)
5
5
 
6
6
  GEM
7
7
  remote: https://gems.ruby-china.com/
@@ -18,6 +18,7 @@ GEM
18
18
  grape-entity (0.10.1)
19
19
  activesupport (>= 3.0.0)
20
20
  multi_json (>= 1.3.2)
21
+ hash_to_struct (1.0.0)
21
22
  i18n (1.8.11)
22
23
  concurrent-ruby (~> 1.0)
23
24
  method_source (1.0.0)
@@ -54,6 +55,7 @@ PLATFORMS
54
55
 
55
56
  DEPENDENCIES
56
57
  grape-entity
58
+ hash_to_struct
57
59
  i18n
58
60
  meta-api!
59
61
  pry-byebug
data/README.md CHANGED
@@ -17,7 +17,7 @@ $ git clone https://github.com/yetrun/web-frame-example.git
17
17
  在 Gemfile 中添加:
18
18
 
19
19
  ```ruby
20
- gem 'meta-api', '~> 0.0.7' # Meta 框架处于快速开发阶段,引入时应尽量固定版本
20
+ gem 'meta-api', '~> 0.0.8' # Meta 框架处于快速开发阶段,引入时应尽量固定版本
21
21
  ```
22
22
 
23
23
  然后在 Ruby 代码中引用:
@@ -1645,3 +1645,64 @@ class DemoApp < Meta::Application
1645
1645
  end
1646
1646
  end
1647
1647
  ```
1648
+
1649
+ ### 实体内部引入另一个实体的字段(轨迹)
1650
+
1651
+ 我们有时候遇到这样一个需求:需要把另一个实体的字段合并到当前的实体。在其他框架里,你会发现诸如继承(inherited)、轨迹(trait)这样的术语;但是 Meta 框架没有。事实上,我们可以通过 Ruby 语言本身的表达做到这样的效果,因此对于该特性 Meta 框架目前采取谨慎加入的态度。
1652
+
1653
+ 举一个表单控件实体和控件值实体的例子。下面是控件实体:
1654
+
1655
+ ```ruby
1656
+ class ControlEntity < Meta::Entity
1657
+ property :id, type: 'integer', description: '控件 ID'
1658
+ property :label, type: 'string', description: '标签名称,显示在控件的左边'
1659
+ property :style, type: 'string', description: '控件类型,如文本框、下拉框、单选框等'
1660
+ property :required, type: boolean, description: '是否必选'
1661
+ property :multiple, type: boolean, description: '是否多项值,当用在下拉框时可选中多个值'
1662
+ end
1663
+ ```
1664
+
1665
+ 另有一个控件值实体:
1666
+
1667
+ ```ruby
1668
+ class ControlValueEntity < Meta::Entity
1669
+ property :id, type: 'integer', description: '控件 ID'
1670
+ property :label, type: 'string', description: '标签名称,显示在控件的左边'
1671
+ # 包含控件的其他字段……
1672
+
1673
+ property :value, type: 'object', description: '控件填入的值'
1674
+ end
1675
+ ```
1676
+
1677
+ 以上显然有重复。我们可以将 `ControlEntity` 的代码放到一个 `Proc` 中,两个实体内都执行一次:
1678
+
1679
+ ```ruby
1680
+ ControlEntityProc = Proc.new do
1681
+ property :id, type: 'integer', description: '控件 ID'
1682
+ property :label, type: 'string', description: '标签名称,显示在控件的左边'
1683
+ property :style, type: 'string', description: '控件类型,如文本框、下拉框、单选框等'
1684
+ property :required, type: boolean, description: '是否必选'
1685
+ property :multiple, type: boolean, description: '是否多项值,当用在下拉框时可选中多个值'
1686
+ end
1687
+
1688
+ class ControlEntity < Meta::Entity
1689
+ instance_exec &ControlEntityProc
1690
+ end
1691
+
1692
+ class ControlValueEntity < Meta::Entity
1693
+ instance_exec &ControlEntityProc
1694
+
1695
+ property :value, type: 'object', description: '控件填入的值'
1696
+ end
1697
+ ```
1698
+
1699
+ Meta 框架还提供了一个 `use` 方法,它可以代替 `instance_exec` 方法。没什么特别的意思,只是简化了一丢丢代码而已:
1700
+
1701
+ ```ruby
1702
+ class ControlValueEntity < Meta::Entity
1703
+ use ControlEntityProc
1704
+
1705
+ property :value, type: 'object', description: '控件填入的值'
1706
+ end
1707
+ ```
1708
+
data/lib/meta/config.rb CHANGED
@@ -1,22 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Meta
4
- class Config
5
- attr_accessor :default_locked_scope,
6
- :json_schema_user_options,
7
- :json_schema_param_stage_options,
8
- :json_schema_render_stage_options
3
+ require 'hash_to_struct'
9
4
 
10
- def initialize
11
- @default_locked_scope = nil
12
- @json_schema_user_options = {}
13
- @json_schema_param_stage_options = {}
14
- @json_schema_render_stage_options = {}
15
- end
16
- end
5
+ module Meta
6
+ DEFAULT_OPTIONS = {
7
+ default_locked_scope: nil,
8
+ json_schema_user_options: {},
9
+ json_schema_param_stage_options: {},
10
+ json_schema_render_stage_options: {}
11
+ }
17
12
 
18
- @config = Config.new
19
13
  class << self
20
14
  attr_reader :config
15
+
16
+ def initialize_configuration(*options_list)
17
+ final_options = options_list.reduce(DEFAULT_OPTIONS, :deep_merge)
18
+ @config = HashToStruct.struct(final_options)
19
+ end
21
20
  end
22
21
  end
22
+ Meta.initialize_configuration
@@ -15,16 +15,16 @@ module Meta
15
15
 
16
16
  def param(name, options = {}, &block)
17
17
  options = (options || {}).dup
18
- in_op = options.delete(:in) || \
19
18
  if path_param_names.include?(name)
20
- 'path'
19
+ options = Utils::KeywordArgs::Checker.fix!(options, in: 'path', required: true)
21
20
  elsif @route_method == :get
22
- 'query'
21
+ options = Utils::KeywordArgs::Checker.merge_defaults!(options, in: 'query')
23
22
  else
24
- 'body'
23
+ options = Utils::KeywordArgs::Checker.merge_defaults!(options, in: 'body')
25
24
  end
26
25
 
27
- if in_op == 'body'
26
+ if options[:in] == 'body'
27
+ options.delete(:in)
28
28
  property name, options, &block
29
29
  else
30
30
  @parameters_builder.param name, options
data/meta-api.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "meta-api"
3
- spec.version = "0.0.7"
3
+ spec.version = "0.0.8"
4
4
  spec.authors = ["yetrun"]
5
5
  spec.email = ["yetrun@foxmail.com"]
6
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: meta-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - yetrun
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-07-14 00:00:00.000000000 Z
11
+ date: 2023-07-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 一个 Web API 框架,该框架采用定义元信息的方式编写 API,并同步生成 API 文档
14
14
  email: