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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a106e1faa458b8bcc191eee37a460eb213aabcf0
4
- data.tar.gz: 6d66a47727e9660b65e0421657f43ea3a335b87a
3
+ metadata.gz: 99fe951457197fe94430c5e51ee5da6c806f92fa
4
+ data.tar.gz: a02ecba7c1057cf6b969373eaa0b144a772a9c82
5
5
  SHA512:
6
- metadata.gz: 34a6c6c87c72fc5cc1a67ddb6d7f34dc049f999ff02631ca9b9c9134b4d477d07b781c4a96683450c66e87578bdfc89b192735a0346810c9675d54ad57d7b267
7
- data.tar.gz: 44db5cf6552c5883bb69db3999ecb4b72d0a3cc89f4ad31836e406d752fb6d9afb69b36ac6fe69e03913eb9d03ecab897ebea5b6cd6866bfdd6cb2bb8c4bce24
6
+ metadata.gz: a3c08ca211f0668c940fa71376132c57d402dad43a155791c0e2ba20331851c2527284459d7cd5e275f7a5b7a7afcbee92fb2a78df51b6a20c0760f91c337653
7
+ data.tar.gz: f9aef9d829a71b88a476cb1e6254314018bf2ce6b7a2d34450201d835154a33228464b03856ed68317d7434e6001677b733f2d9fb10a63cc5eae81ba832b6aa5
data/.gitignore CHANGED
@@ -7,3 +7,4 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ .ruby-version
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in hb.gemspec
4
4
  gemspec
5
+
6
+ gem 'pry'
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Hb
2
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/hb`. 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
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
- TODO: Write usage instructions here
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
- ## Development
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
- 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.
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
- 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).
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/[USERNAME]/hb.
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/shozawa/hb.
36
85
 
data/Rakefile CHANGED
@@ -1,2 +1,10 @@
1
1
  require "bundler/gem_tasks"
2
- task :default => :spec
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |test|
5
+ test.libs << 'test'
6
+ test.test_files = FileList[ 'test/**/*_test.rb' ]
7
+ test.verbose = true
8
+ end
9
+
10
+ task :default => [:test]
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
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start
9
+ require "pry"
10
+ Pry.start
data/lib/hb/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Hb
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
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.0
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-14 00:00:00.000000000 Z
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