hb 0.1.0 → 0.1.1
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/.gitignore +1 -0
- data/Gemfile +2 -0
- data/README.md +57 -8
- data/Rakefile +9 -1
- data/bin/console +2 -6
- data/lib/hb/version.rb +1 -1
- metadata +2 -3
- data/.ruby-version +0 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 99fe951457197fe94430c5e51ee5da6c806f92fa
|
|
4
|
+
data.tar.gz: a02ecba7c1057cf6b969373eaa0b144a772a9c82
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a3c08ca211f0668c940fa71376132c57d402dad43a155791c0e2ba20331851c2527284459d7cd5e275f7a5b7a7afcbee92fb2a78df51b6a20c0760f91c337653
|
|
7
|
+
data.tar.gz: f9aef9d829a71b88a476cb1e6254314018bf2ce6b7a2d34450201d835154a33228464b03856ed68317d7434e6001677b733f2d9fb10a63cc5eae81ba832b6aa5
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
# Hb
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
|
3
|
+
Simple Hash Builder which has ActiveModelSerializers like interface.
|
|
6
4
|
|
|
7
5
|
## Installation
|
|
8
6
|
|
|
@@ -22,15 +20,66 @@ Or install it yourself as:
|
|
|
22
20
|
|
|
23
21
|
## Usage
|
|
24
22
|
|
|
25
|
-
|
|
23
|
+
### Creating Presenter
|
|
24
|
+
Subclass Hb::Base and declare attributes.
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
class PersonPresenter < Hb::Base
|
|
28
|
+
attributes :id, :email
|
|
29
|
+
attribute :last_name, key: :family_name
|
|
30
|
+
end
|
|
31
|
+
```
|
|
32
|
+
The attributes names are whitelist of attributes to be exposed. You can change key name by using :key option.
|
|
33
|
+
|
|
34
|
+
### Buildeing Hash
|
|
26
35
|
|
|
27
|
-
|
|
36
|
+
```ruby
|
|
37
|
+
Person = Struct.new(:id, :email, :last_name, :first_name, :age)
|
|
38
|
+
|
|
39
|
+
person = Person.new(1, 'shozawa@sample.com', 'shozawa', 'tomohiro', 28)
|
|
40
|
+
|
|
41
|
+
PersonPresenter.new(person).to_h
|
|
28
42
|
|
|
29
|
-
|
|
43
|
+
# => { id: 1, email: 'shozawa@sample.com', family_name: 'shozawa' }
|
|
44
|
+
|
|
45
|
+
# you can pass ActiveModel object like this
|
|
46
|
+
PersonPresenter.new(User.find(1)).to_h
|
|
47
|
+
```
|
|
30
48
|
|
|
31
|
-
|
|
49
|
+
### Computed Attributes
|
|
50
|
+
```ruby
|
|
51
|
+
class PersonPresenter < Hb::Base
|
|
52
|
+
attributes :id, :email, :full_name
|
|
53
|
+
def full_name
|
|
54
|
+
"#{object.first_name} #{object.last_name}"
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# => { id: 1, email: 'shozawa@sample.com', full_name: 'tomohiro shozawa' }
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
`object` is the object to be wrapped.
|
|
62
|
+
|
|
63
|
+
### Transforming Keys
|
|
64
|
+
```ruby
|
|
65
|
+
# pass method_name
|
|
66
|
+
Hb::Base.key_format :upcase
|
|
67
|
+
PersonPresenter.new(person).to_h
|
|
68
|
+
# => { "ID" => 1, "EMAIL" => 'shozawa@sample.com', "FULL_NAME": ... }
|
|
69
|
+
|
|
70
|
+
# with options
|
|
71
|
+
Hb::Base.key_format camelize: :lower
|
|
72
|
+
PersonPresenter.new(person).to_h
|
|
73
|
+
# => { "id" => 1, "email" => 'shozawa@sample.com', "fullName": ... }
|
|
74
|
+
|
|
75
|
+
# you can pass proc
|
|
76
|
+
Hb::Base.key_format -> (k) { '_' + k }
|
|
77
|
+
PersonPresenter.new(person).to_h
|
|
78
|
+
# => { "_id" => 1, "_email" => 'shozawa@sample.com', "_full_name": ... }
|
|
79
|
+
|
|
80
|
+
```
|
|
32
81
|
|
|
33
82
|
## Contributing
|
|
34
83
|
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
|
84
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/shozawa/hb.
|
|
36
85
|
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
|
@@ -6,9 +6,5 @@ require "hb"
|
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
# Pry.start
|
|
12
|
-
|
|
13
|
-
require "irb"
|
|
14
|
-
IRB.start
|
|
9
|
+
require "pry"
|
|
10
|
+
Pry.start
|
data/lib/hb/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: hb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- shozawa
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2017-05-
|
|
11
|
+
date: 2017-05-17 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -60,7 +60,6 @@ extensions: []
|
|
|
60
60
|
extra_rdoc_files: []
|
|
61
61
|
files:
|
|
62
62
|
- ".gitignore"
|
|
63
|
-
- ".ruby-version"
|
|
64
63
|
- Gemfile
|
|
65
64
|
- README.md
|
|
66
65
|
- Rakefile
|
data/.ruby-version
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
2.3.3
|