hb 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a106e1faa458b8bcc191eee37a460eb213aabcf0
4
+ data.tar.gz: 6d66a47727e9660b65e0421657f43ea3a335b87a
5
+ SHA512:
6
+ metadata.gz: 34a6c6c87c72fc5cc1a67ddb6d7f34dc049f999ff02631ca9b9c9134b4d477d07b781c4a96683450c66e87578bdfc89b192735a0346810c9675d54ad57d7b267
7
+ data.tar.gz: 44db5cf6552c5883bb69db3999ecb4b72d0a3cc89f4ad31836e406d752fb6d9afb69b36ac6fe69e03913eb9d03ecab897ebea5b6cd6866bfdd6cb2bb8c4bce24
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hb.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Hb
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
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'hb'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install hb
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]/hb.
36
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hb"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
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
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/hb.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hb"
8
+ spec.version = Hb::VERSION
9
+ spec.authors = ["shozawa"]
10
+ spec.email = ["t.shozawa@mugenup.com"]
11
+
12
+ spec.summary = "Simple Hash Builder (=Presenter) for Rails API"
13
+ spec.homepage = "https://github.com/shozawa/hb"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.13"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+
25
+ spec.add_dependency "activesupport", ">= 3.0.0"
26
+ end
@@ -0,0 +1,28 @@
1
+ module Hb
2
+ class Base
3
+ class KeyFormatter
4
+ def initialize(*args)
5
+ @format = {}
6
+
7
+ options = args.extract_options!
8
+ args.each do |name|
9
+ @format[name] = []
10
+ end
11
+ options.each do |name, parameters|
12
+ @format[name] = parameters
13
+ end
14
+ end
15
+
16
+ def format(key)
17
+ @format.reduce(key.to_s) do |result, args|
18
+ func, args = args
19
+ if ::Proc === func
20
+ func.call result, *args
21
+ else
22
+ result.send func, *args
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
data/lib/hb/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Hb
2
+ VERSION = "0.1.0"
3
+ end
data/lib/hb.rb ADDED
@@ -0,0 +1,60 @@
1
+ require "hb/version"
2
+ require "hb/key_formatter"
3
+ require "active_support/core_ext/class"
4
+ require "active_support/core_ext/array"
5
+
6
+ module Hb
7
+ class Base
8
+ attr_reader :object, :instance_options
9
+ class_attribute :_attributes_data, :key_formatter
10
+ self._attributes_data = {}
11
+ self.key_formatter = nil
12
+
13
+ def initialize(object, options = {})
14
+ @object = object
15
+ @instance_options = options
16
+ end
17
+
18
+ class << self
19
+ def attributes(*args)
20
+ args.each do |attr|
21
+ attribute(attr)
22
+ end
23
+ end
24
+
25
+ def attribute(attr, options = {})
26
+ key = options.fetch(:key, attr)
27
+ # https://apidock.com/rails/Class/class_attribute
28
+ self._attributes_data = _attributes_data.merge({ key => attr })
29
+ end
30
+
31
+ def key_format(*args)
32
+ self.key_formatter = KeyFormatter.new(*args)
33
+ end
34
+ end
35
+
36
+ def to_hash
37
+ hash = self.class._attributes_data.each_with_object({}) do |(key, attr), result|
38
+ if respond_to?(attr)
39
+ result[key] = send(attr)
40
+ else
41
+ result[key] = object.send(attr)
42
+ end
43
+ end
44
+
45
+ format(hash)
46
+ end
47
+
48
+ alias :to_h :to_hash
49
+
50
+ private
51
+
52
+ def format(hash)
53
+ return hash unless self.class.key_formatter
54
+
55
+ hash.deep_transform_keys do |k|
56
+ self.class.key_formatter.format(k)
57
+ end
58
+ end
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - shozawa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0
55
+ description:
56
+ email:
57
+ - t.shozawa@mugenup.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".ruby-version"
64
+ - Gemfile
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - hb.gemspec
70
+ - lib/hb.rb
71
+ - lib/hb/key_formatter.rb
72
+ - lib/hb/version.rb
73
+ homepage: https://github.com/shozawa/hb
74
+ licenses: []
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.6.12
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Simple Hash Builder (=Presenter) for Rails API
96
+ test_files: []