sparrow-entity 0.1.1 → 0.1.2

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: ba7f73f798ef8f46951bba6feb55e71b9809c4a4b54e3e1be7840c6b5f4193d4
4
- data.tar.gz: 2e7646185c12d2c10b407899f6d0c71d4a8260653821d38c1c4cf3a9849b2af8
3
+ metadata.gz: cd8e492cb64787e3312b6801d81a8faeb56f471ff3d499757b6897c259f7eff1
4
+ data.tar.gz: b0895dc4b2cdbb2a4b9757813304a35e1b404e39037cef9873a57cc300e2d958
5
5
  SHA512:
6
- metadata.gz: cc06044da994a650876616956624c65cfb454f632f748212172ddb9c7e5bb6dd6e55e310253315d0693c007128923500193028ec7bf37ea10ded236743f0f186
7
- data.tar.gz: 67cd395f4c53fae5d0c28576c2525d087a2e00cdc200272df7fa6167712c347361aa670f4e65d2770a189ad4f8cf59cdce0392711e509cea0756217117236a97
6
+ metadata.gz: 8e96039af0b9a4d5105642701fa8b1e345497f34eaa4f4d16348a478c8956de54f2dd9cab1763049ce672b011727212cdbc5112304e78d5a91c9551d762a9220
7
+ data.tar.gz: 17993065f31455e924e22957c1a0570b92fc3c0878d9b6de41c93edeec15b6dfbc49c40d3eaaee2ef48c193d798d85b12f7d798a0a263de7635c98be2876101b
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sparrow-entity (0.1.1)
4
+ sparrow-entity (0.1.2)
5
5
  activemodel
6
6
  activemodel_object_info (~> 0.2.0)
7
7
  activesupport
data/README.md ADDED
@@ -0,0 +1,126 @@
1
+ # Sparrow
2
+
3
+ Sparrow 用来快速实现在一个 Ruby 项目中引入实体处理非数据库可以映射的项目。特别是对于拆分为微服务的项目来说,由于大量数据基于其他服务获取到,而并不是直接来源于数据库,所以不能使用基于 **ActiveRecord::Base** 这样的类来处理具体数据实例。而 Sparrow 就提供了类似 ORM 式的使用方法来处理这些对象类。
4
+
5
+ ## 安装
6
+
7
+ 在你的项目中的 Gemfile 文件添加如下一行即可。建议保持最新版本以获取功能齐全的体验。
8
+
9
+ ```ruby
10
+ gem 'sparrow-entity', require: 'sparrow'
11
+ ```
12
+
13
+ 之后在你的项目文件夹下执行命令安装。
14
+
15
+ $ bundle
16
+
17
+ 或者也可以直接使用 gem 命令全局安装。
18
+
19
+ $ gem install sparrow-entity
20
+
21
+ ## 使用
22
+
23
+ ### 一般的使用方法
24
+
25
+ 安装之后尽可以在需要的时候随意创建自己的实体类,只要继承该 gem 提供的基础类即可。需要注意的是,如果你是在单纯的 Ruby 项目下使用,记得在使用前引入相关文件,比如:
26
+
27
+ ```ruby
28
+ # 使用本 gem 引入。如果是 Rails 项目则可省略。
29
+ require 'sparrow'
30
+
31
+ # 定义一个继承自 gem 基本实体类的自定义实体类
32
+ class MyEntity < Sparrow::Base
33
+ # 定义相关的属性和类型
34
+ field :id, Integer
35
+ field :family_name, String
36
+ field :given_name, String
37
+ field :gender, Integer
38
+ field :birthday, Date
39
+ field :nationality, String
40
+ # 也可以给与属性默认值,如果属性值本身无效时获取该属性会返回默认值
41
+ field :status, String, default: 'good'
42
+
43
+ # 定义具体的实例方法可以在之后使用
44
+ def full_name(last: :given, seperator: ' ')
45
+ val = [family_name, given_name]
46
+ val.reverse! if last == :family
47
+ val.join(seperator)
48
+ end
49
+ end
50
+ ```
51
+
52
+ 这样定义的一个类就具有了唯一标示 **id**,姓氏 **family_name**,名字 **given_name**,性别 **gender**,出生日期 **birthday** 和国籍 **nationality** 这几个属性,并且会在赋值和获取对应属性值时尝试自动将该属性设置转换为对应的数据类型。举例来说,比如给出一个可以被解析的日期字符串并赋值给出生日期属性,那么获取该属性值时会自动转化成定义属性时设定的 **Date** 类型的日期:
53
+
54
+ ```ruby
55
+ me = MyEntity.new(birthday: '2022-01-01')
56
+ me.birthday
57
+ # => <Date 2022年1月1日> 实例对象
58
+ ```
59
+
60
+ ### 赋值
61
+
62
+ 上例中也展示了为实体类的实例进行属性赋值的方式之一,即创建时指定属性名对应的值。当然也可以在创建后分别给属性赋值。
63
+
64
+ ```ruby
65
+ me.family_name = '张'
66
+ me.last_name = '三'
67
+ me.full_name(seperator: '')
68
+ # => "张三"
69
+ ```
70
+
71
+ ### 默认属性值
72
+
73
+ 在定义属性的时候可以跟着 `default` 命名参数给出默认值。当属性值为空或者无效时,获取属性的时候会返回默认值。
74
+
75
+ ```ruby
76
+ me.status
77
+ # => "good"
78
+ me.status = 'bad'
79
+ me.status
80
+ # => "bad"
81
+ ```
82
+
83
+ ### 基于 Rails 项目的使用方法
84
+
85
+ 如果你的项目是一个基于 Rails 框架的项目,那么在创建自己的实体时并不需要特别声明引入该 gem 对应的文件,因为 Rails 框架已经替你做好了一切。
86
+
87
+ 另外,使用 Rails 框架的项目还可以利用本 gem 提供的生成器(Generator)来方便快速生成一个新的实体类。
88
+
89
+ $ rails g sparrow:entity Post::Replay
90
+
91
+ 使用命令 Rails 生成器命令 `sparrow:entity` 会在你的项目根文件夹的 `app/entities` 路径下生成 `post/reply.rb` 的文件,文件也自动创建了好了继承类。
92
+
93
+ ```ruby
94
+ module Post
95
+ class Reply < Sparrow::Base
96
+ end
97
+ end
98
+ ```
99
+
100
+ 之后可以根据自己的需要修改生成的框架类的命名空间或者类的名称。
101
+
102
+ ### 内置的属性方法
103
+
104
+ 除了常规的 `attributes` 和 `attribute_names` 方法分别获取属性名和值的键值对以及属性名的数组外,本 gem 还内置了 [activemodel_object_info](https://rubygems.org/gems/activemodel_object_info) 这个 gem 辅助生成属性和值的键值对散列。具体用法请参考相关 gem 的说明文档。
105
+
106
+ ```ruby
107
+ OUTPUT = {
108
+ attributes: [
109
+ { name: :full_name, type: :method },
110
+ { name: :sex, as: :gender, type: :abstract, filter: ->(v) { v == 1 ? 'Male' : 'Female' } },
111
+ ],
112
+ }.freeze
113
+
114
+ me.instance_info(OUTPUT)
115
+ # => { full_name: "张三", sex: "Male" }
116
+ ```
117
+
118
+ ## 面向开发者
119
+
120
+ 内置 Rake 命令包括了控制台开启命令 `rake c` 或者 `rake console` 即可开启控制台模式。
121
+
122
+ 默认的 Rake 命令为单元测试命令,等同于 `bundle exec rspec` 命令的效果。
123
+
124
+ ## License
125
+
126
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -10,14 +10,73 @@ module Sparrow
10
10
  # 默认的引入应用的相对安装路径
11
11
  TARGET_RELATIVE_PATH = 'app/entities'
12
12
 
13
+ # 单模组模板
14
+ MODULE_TEMPLATE =<<~MODULE.strip
15
+ module %{module_name}
16
+ %{module_content}
17
+ end
18
+ MODULE
19
+
20
+ # 单类模板
21
+ CLASS_TEMPLATE = <<~KLASS.strip
22
+ class %{class_name} < Sparrow::Base
23
+ #
24
+ # Useage:
25
+ #
26
+ # use field DSL to define attribute.
27
+ # like: <tt>field :name, String</tt> to define an string attribute called +name+.
28
+ #
29
+ end
30
+ KLASS
31
+
13
32
  source_root File.expand_path('templates', __dir__)
14
33
 
15
34
  #
16
35
  # 复制模板文件到对应 Rails 项目中
17
36
  #
18
37
  def create_sparrow_class_file
38
+ # 如果存在命名空间,则分别生成对应模型
39
+ if class_path.present?
40
+ class_path.each_with_index do |sub, index|
41
+ @sub_paths = class_path[0..index]
42
+ parent_paths = @sub_paths.dup
43
+ parent_paths.pop
44
+ sub_path = ::File.join(TARGET_RELATIVE_PATH, parent_paths, "#{sub}.rb")
45
+ template 'module.rb.tt', sub_path
46
+ end
47
+ end
48
+
49
+ # 类文件最终路径
19
50
  sparrow_file_path = ::File.join(TARGET_RELATIVE_PATH, class_path, "#{file_name}.rb")
20
- template 'sparrow_entity.rb.erb', sparrow_file_path
51
+ # 类文件生成
52
+ template 'sparrow_entity.rb.tt', sparrow_file_path
53
+ end
54
+
55
+ private
56
+
57
+ def indent_level
58
+ @indent_level = @indent_level.blank? ? 0 : @indent_level + 1
59
+ end
60
+
61
+ def __module_content__(name, content)
62
+ return MODULE_TEMPLATE % { module_name: name.camelize, module_content: indent(content, 2) }
63
+ end
64
+
65
+ def __class_content__
66
+ return CLASS_TEMPLATE % { class_name: file_name.camelize }
67
+ end
68
+
69
+ def __class_body__
70
+ results = __class_content__
71
+ class_path.reverse.each { |sub| results = __module_content__(sub, results) } if class_path.present?
72
+ results
73
+ end
74
+
75
+ def __module_body__(paths = [])
76
+ current = paths.shift
77
+ content = paths.size.positive? ? __module_body__(paths) : ''
78
+ result = current ? __module_content__(current, content) : ''
79
+ result.each_line.select { |line| line.present? }.join
21
80
  end
22
81
  end
23
82
  end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ <%= __module_body__(@sub_paths) -%>
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ <%= __class_body__ -%>
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Sparrow
4
4
  # 当前版本号
5
- VERSION = '0.1.1'
5
+ VERSION = '0.1.2'
6
6
  # 当前综述
7
7
  SUMMARY = 'Provide an easy approach to build entity object for data that can not be reflected to database tables.'
8
8
  # 当前详述
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sparrow-entity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - shiner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-31 00:00:00.000000000 Z
11
+ date: 2022-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -170,7 +170,7 @@ files:
170
170
  - Gemfile
171
171
  - Gemfile.lock
172
172
  - LICENSE
173
- - README
173
+ - README.md
174
174
  - Rakefile
175
175
  - bin/console
176
176
  - bin/setup
@@ -178,7 +178,8 @@ files:
178
178
  - lib/config/locales/sparrow/base.zh-CN.yml
179
179
  - lib/generators/sparrow/entity/USAGE
180
180
  - lib/generators/sparrow/entity/entity_generator.rb
181
- - lib/generators/sparrow/entity/templates/sparrow_entity.rb.erb
181
+ - lib/generators/sparrow/entity/templates/module.rb.tt
182
+ - lib/generators/sparrow/entity/templates/sparrow_entity.rb.tt
182
183
  - lib/sparrow.rb
183
184
  - lib/sparrow/base.rb
184
185
  - lib/sparrow/class_methods.rb
data/README DELETED
@@ -1,43 +0,0 @@
1
- # Sparrow
2
-
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sparrow`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'sparrow'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install sparrow
22
-
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sparrow. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
-
37
- ## License
38
-
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
-
41
- ## Code of Conduct
42
-
43
- Everyone interacting in the Sparrow project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/sparrow/blob/master/CODE_OF_CONDUCT.md).
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SparrowEntity
4
- #
5
- # Create a sparrow entity class named <%= class_name %> by generator.
6
- #
7
- class <%= class_name %> < ::Sparrow::Base
8
- #
9
- # Useage:
10
- #
11
- # use field DSL to define attribute.
12
- # like: <tt>field :name, String</tt> to define an string attribute called +name+.
13
- #
14
- end
15
- end