ftl-serializer 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 194b0f0936760e826edd971a6d2c6f302aff9b34b11e6903bfd425fe34f9f11d
4
+ data.tar.gz: 3404de0c181d182e56d9ad90684e81791cd40323f0c1bf854ccd75e9a33d26dc
5
+ SHA512:
6
+ metadata.gz: c0de22d43bdfd73eda9ab2e2c9c4bca7c2968db009a2f574ec8e7a8b921188423d5b3ef34b451167d087f653765e47cbcb3dd4b83f6aae53931ec53733e8c8ba
7
+ data.tar.gz: 0dd3387b068787d20e3a6d5cd0e9e5c6b555f6b4c7f14a6c45d46cd6971ef44a19a801d4fc4e86de3c1666077e823fbcec4a66699b74f7bf17c414a8bb79574c
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ TAGS
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .byebug_history
14
+ *.gem
15
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.3.8
7
+ before_install: gem install bundler -v 1.17.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to make participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at andrew.markle@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in ftl.gemspec
8
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Fullscript
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,237 @@
1
+ # FTL (Faster Than Light) Serializer 🚀
2
+
3
+ A ruby serializer that can make the kessel run in less than 12 parsecs.
4
+
5
+ ## Why FTL?
6
+ This library is an extraction from Fullscript. We originally wrote this at a time when there weren't many options out there. Serializers were mostly slow, with over-complicated DSLs, or they weren't flexible enough for our needs (for example only supporting a specific spec like JSON:API).
7
+
8
+ Our main design decisions centered around 3 principles.
9
+
10
+ - Speed. Slow stuff happens at boot time rather than at runtime.
11
+ - Simple DSL. We've opted for a very simple DSL. Mostly to avoid any meta-programming slowness (no has_many, belongs_to, etc. that you see in most serializers) but we also wanted an early-career developer to pick up FTL without much effort.
12
+ - Flexibility. You should be able to serialize data to an existing spec (like JSON:API) or come up with your own.
13
+
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'ftl-serializer'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install ftl-serializer
30
+
31
+ Then in an initializer you just need to point to the path(s) where you're FTL serializers live.
32
+
33
+ ```ruby
34
+ # app/initializers/ftl.rb
35
+
36
+ FTL::Configuration.serializer_paths = ["#{Rails.root.join}/app/serializers"]
37
+ ```
38
+
39
+ ## Usage
40
+
41
+ ### Example Rails Model
42
+
43
+ For our examples here's a simple Rails Model. (Note that FTL can accept any data structure and isn't limited to models.)
44
+
45
+ ```ruby
46
+ class Ship
47
+ belongs_to :classification
48
+
49
+ attr_accessor :id, :name, :special_modifications
50
+ end
51
+ ```
52
+
53
+ ```ruby
54
+ ship = Ship.new(id: 10, name: "Millenium Falcon", special_modifications: true, classification_id: 20)
55
+ ```
56
+
57
+
58
+ ### Serializer Definition
59
+
60
+ We define our serializer by inheriting from FTL::Serializer::Base
61
+
62
+ ```ruby
63
+ class FastestHunkOfJunkInTheGalaxy < FTL::Serializer::Base
64
+ attributes :name, :special_modifications, :type
65
+
66
+ def type
67
+ obj.classification.name
68
+ end
69
+ end
70
+ ```
71
+
72
+ #### .to_h
73
+
74
+ ```ruby
75
+ hash = FastestHunkOfJunkInTheGalaxy.new(ship).to_h
76
+ ```
77
+
78
+ returns:
79
+
80
+ ```ruby
81
+ {
82
+ id: "10",
83
+ name: "Millenium Falcon",
84
+ special_modifications: true,
85
+ type: "YT-1300 Corellian light freighter"
86
+ }
87
+ ```
88
+
89
+
90
+ #### .to_json
91
+
92
+ ```ruby
93
+ json_string = FastestHunkOfJunkInTheGalaxy.new(ship).to_json
94
+ ```
95
+
96
+ returns:
97
+
98
+ ```json
99
+ {
100
+ "id": "10",
101
+ "name": "Millenium Falcon",
102
+ "special_modifications": true,
103
+ "type": "YT-1300 Corellian light freighter"
104
+ }
105
+ ```
106
+
107
+ ### Options
108
+
109
+ #### format
110
+
111
+ By default FTL underscores the key names but it also supports camel case.
112
+
113
+ ```ruby
114
+ class FastestHunkOfJunkInTheGalaxy < FTL::Serializer::Base
115
+ # Available options :camel, :underscore (default)
116
+ format :camel
117
+ end
118
+ ```
119
+
120
+ Examples:
121
+
122
+ ```ruby
123
+ keys :camel # "some_key" => "someKey"
124
+ keys :underscore # "some_key" => "some_key"
125
+ ```
126
+
127
+ #### root
128
+
129
+ FTL can also support a root key.
130
+
131
+ ```ruby
132
+ class FastestHunkOfJunkInTheGalaxy < FTL::Serializer::Base
133
+ root "starship"
134
+ end
135
+ ```
136
+
137
+ Returns:
138
+
139
+ ```json
140
+ {
141
+ "starship": {
142
+ "id": "10",
143
+ "name": "Millenium Falcon",
144
+ "special_modifications": true,
145
+ "type": "YT-1300 Corellian light freighter"
146
+ }
147
+ }
148
+ ```
149
+
150
+ Roots can also be disabled when you are initializing your serializer. (Occasionally helpful when calling other serializers from within another serializer.)
151
+
152
+ ```ruby
153
+ FastestHunkOfJunkInTheGalaxy.new(obj).root(:disabled)
154
+ ```
155
+
156
+ ### Attributes
157
+
158
+ Attributes are defined using the `attributes` keyword.
159
+
160
+ ```ruby
161
+ class FastestHunkOfJunkInTheGalaxy < FTL::Serializer::Base
162
+ attributes :name
163
+ end
164
+ ```
165
+
166
+ Custom attributes can be overridden by defining a method.
167
+
168
+ The object (that is passed into the serializer) is referrenced to as `obj`.
169
+
170
+ ```ruby
171
+ class FastestHunkOfJunkInTheGalaxy < FTL::Serializer::Base
172
+ attributes :name
173
+
174
+ def name
175
+ obj.first_name
176
+ end
177
+ end
178
+ ```
179
+
180
+ ### Locals
181
+
182
+ In some cases, you might want to use some ancillary data that's not necessarily available on your objects. For example, `current_user` or `current_account` are examples of a dependency that you may want to inject into your serializer.
183
+
184
+ To do this you can just pass a `locals` hash into the serializer.
185
+
186
+ ```ruby
187
+ class FastestHunkOfJunkInTheGalaxy < FTL::Serializer::Base
188
+ attributes :name, :current_pilot
189
+
190
+ def current_pilot
191
+ locals.current_pilot.full_name
192
+ end
193
+ end
194
+
195
+ # ...
196
+ lando = User.find_by(first_name: "Lando", last_name: "Calrissian")
197
+ serializer = FastestHunkOfJunkInTheGalaxy.new(ship, { locals: { current_pilot: lando } })
198
+ serializer.to_h
199
+ ```
200
+
201
+ Locals can be in a hash format or it can be chained as a method.
202
+
203
+ ```ruby
204
+ # This is the same:
205
+ FastestHunkOfJunkInTheGalaxy.new(ship, { locals: { current_pilot: lando } })
206
+
207
+ # as this:
208
+ FastestHunkOfJunkInTheGalaxy.new(ship).locals(current_pilot: lando)
209
+ ```
210
+
211
+ ### Loading
212
+
213
+ It's also worth mentioning how serializers are loaded. They're hooked into a `Rails::Railtie` that loads up the serializers and sets all the attributes during the Rails boot time. We did this for speed so that everything is ready to go and we don't need to do any expensive meta-programming when you call your serializer. All the attributes are set and you just need to pass it some data to serialize.
214
+
215
+ If you ever need to manually load up a serializer it's just:
216
+
217
+ ```ruby
218
+ FTL::Serializer.bootstrap!
219
+ ```
220
+
221
+ ## Development
222
+
223
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
224
+
225
+ 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).
226
+
227
+ ## Contributing
228
+
229
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fullscript/ftl-serializer. 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.
230
+
231
+ ## License
232
+
233
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
234
+
235
+ ## Code of Conduct
236
+
237
+ Everyone interacting in the Ftl project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ftl/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ftl"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'bundler/setup'
3
+ load Gem.bin_path('rspec-core', 'rspec')
@@ -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
@@ -0,0 +1,44 @@
1
+
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path("../lib", __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require "ftl/version"
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = "ftl-serializer"
10
+ spec.version = FTL::VERSION
11
+ spec.authors = ["Fullscript"]
12
+ spec.email = ["devops@fullscript.com", ]
13
+
14
+ spec.summary = 'A ruby serializer that can make the kessel run in less than 12 parsecs.'
15
+ spec.description = 'FTL (Faster Than Light) is a ruby serializer that is optimized for simplicity and speed.'
16
+ spec.homepage = "https://fullscript.com"
17
+ spec.license = "MIT"
18
+
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = "https://github.com/fullscript/ftl-serializer"
22
+ # spec.metadata["changelog_uri"] = "https://github.com/fullscript/ftl-serializer/changelog.md"
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ spec.required_ruby_version = '>= 2.3'
29
+
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+
34
+ spec.require_paths = ["lib"]
35
+
36
+ spec.add_dependency "railties", ">= 5"
37
+ spec.add_dependency "activesupport", ">= 5"
38
+ spec.add_dependency "oj", '>= 2'
39
+
40
+ spec.add_development_dependency "rake", "~> 10.0"
41
+ spec.add_development_dependency "rspec", "~> 3.0"
42
+ spec.add_development_dependency "pry"
43
+ spec.add_development_dependency "pry-byebug"
44
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ftl/version"
4
+ require "ftl/serializer"
5
+ require "ftl/railtie"
6
+ require "ftl/errors"
7
+ require "ftl/configuration"
8
+ require "ftl/serializer/base"
9
+ require "ftl/serializer/dsl"
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/configurable"
4
+
5
+ module FTL
6
+ class Configuration
7
+ include ActiveSupport::Configurable
8
+
9
+ config_accessor(:serializer_paths) { [] }
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FTL
4
+ module Errors
5
+ class LocalsError < StandardError
6
+ def initialize(serializer)
7
+ super("#{serializer} is expecting your locals as a hash."\
8
+ " You can do this by passing in a locals hash to your serializer like this:"\
9
+ " #{serializer}.new(your_object, locals: { your_locals_object: obj })"\
10
+ " or like this: #{serializer}.new(your_object).locals(your_locals_object: obj).")
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/railtie"
4
+
5
+ module FTL
6
+ class Railtie < Rails::Railtie
7
+ # Watch serializer files for changes in dev, so we can reload them with any new code.
8
+ initializer "ftl.add_watchable_files" do |app|
9
+ reload_paths = FTL::Configuration.serializer_paths.reduce({}) do |memo, dir|
10
+ app_dir = File.join(app.root, dir)
11
+ memo[app_dir] = ['rb']
12
+ memo
13
+ end
14
+
15
+ ftl_reloader = app.config.file_watcher.new([], reload_paths) do
16
+ FTL::Configuration.serializer_paths.map do |path|
17
+ Dir.glob("#{path}/**/*.rb").each do |file|
18
+ begin
19
+ if Rails.env.development? || Rails.env.test?
20
+ load file
21
+ else
22
+ require file
23
+ end
24
+ rescue => e
25
+ warn "can't load '#{file}' file (#{e.message})!"
26
+ end
27
+ end
28
+ end
29
+
30
+ FTL::Serializer.bootstrap!
31
+ end
32
+
33
+ app.reloaders << ftl_reloader
34
+
35
+ # Reloads serializers on boot / when they change
36
+ config.to_prepare do
37
+ ftl_reloader.execute
38
+ end
39
+
40
+ config.after_initialize do
41
+ if defined?(Spring)
42
+ Spring.after_fork do
43
+ ftl_reloader.execute
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FTL
4
+ module Serializer
5
+ def self.register_serializer(klass)
6
+ @serializers ||= []
7
+ return @serializers if @serializers.include?(klass)
8
+ @serializers.push(klass)
9
+ end
10
+
11
+ def self.registered_serializers
12
+ @serializers
13
+ end
14
+
15
+ def self.bootstrap!
16
+ return nil unless @serializers
17
+ @serializers.each do |klass|
18
+ klass.define_to_h
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ftl/serializer/dsl'
4
+ require 'oj'
5
+
6
+ module FTL::Serializer
7
+ class Base
8
+ extend FTL::Serializer::DSL
9
+
10
+ attr_accessor :obj
11
+ attr_reader :collection
12
+
13
+ def initialize(collection, args = {})
14
+ @collection = collection
15
+ @locals = args.dig(:locals)
16
+ end
17
+
18
+ def to_h
19
+ if singular_object?
20
+ rootify(singular_to_h)
21
+ else
22
+ rootify(multi_to_h)
23
+ end
24
+ end
25
+ alias_method :to_hash, :to_h
26
+
27
+ def to_json
28
+ Oj.dump(to_h)
29
+ end
30
+
31
+ def meta(hash)
32
+ self.tap { @meta = hash }
33
+ end
34
+
35
+ def links(hash)
36
+ self.tap { @links = hash }
37
+ end
38
+
39
+ def root(name)
40
+ self.tap { @root = name }
41
+ end
42
+
43
+ def locals(*args)
44
+ if args.size == 0
45
+ return @_locals if defined? @_locals
46
+ return nil if @locals.blank?
47
+ @_locals = begin
48
+ local_methods = @locals.keys.map(&:to_sym)
49
+ values = @locals.values
50
+ if local_methods.any?
51
+ Struct.new(*local_methods).new(*values)
52
+ end
53
+ rescue
54
+ raise FTL::Errors::LocalsError.new(self.class.name)
55
+ end
56
+ else
57
+ self.tap { @locals = args[0] }
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ def singular_object?
64
+ collection.is_a?(Hash) || collection.is_a?(Struct) || !collection.respond_to?(:map)
65
+ end
66
+
67
+ def singular_to_h
68
+ self.obj = collection
69
+ hashify
70
+ end
71
+
72
+ def multi_to_h
73
+ collection.map do |object|
74
+ self.obj = object
75
+ hashify
76
+ end
77
+ end
78
+
79
+ def rootify(hash)
80
+ root_name = @root || self.class.root_name
81
+ return hash if root_name == :disabled || root_name.nil?
82
+
83
+ root_name = format_root_name(root_name)
84
+ if singular_object?
85
+ { root_name => hash }
86
+ else
87
+ { root_name.pluralize => hash }.tap do |h|
88
+ meta_hash.map { |meta| h.merge!(meta) if meta }
89
+ end
90
+ end
91
+ end
92
+
93
+ def format_root_name(root_name)
94
+ if self.class.camel_case?
95
+ root_name.to_s.camelize(:lower)
96
+ else
97
+ root_name.to_s
98
+ end
99
+ end
100
+
101
+ def meta_hash
102
+ [{ "meta" => @meta, "links" => @links }.select { |_, value| !value.nil? }]
103
+ end
104
+
105
+ def merge!(hash)
106
+ return hash if self.class.object_merge.empty?
107
+ self.class.object_merge.reduce(hash) do |complete_hash, merge_object_method|
108
+ new_hash = self.send(merge_object_method)
109
+ complete_hash.merge(new_hash)
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ftl/serializer/dsl/format'
4
+
5
+ module FTL::Serializer
6
+ module DSL
7
+ include FTL::Serializer::DSL::Format
8
+
9
+ attr_reader :attribute_list, :object_merge
10
+
11
+ def inherited(child)
12
+ child.attributes(attribute_list) unless attribute_list.nil?
13
+ child.merge_with(object_merge) unless object_merge.nil?
14
+ child.root(root_name)
15
+
16
+ FTL::Serializer.register_serializer(child)
17
+ end
18
+
19
+ def attribute_list
20
+ @attribute_list ||= []
21
+ end
22
+
23
+ def object_merge
24
+ @object_merge ||= []
25
+ end
26
+
27
+ def attributes(*attrs)
28
+ attribute_list.push(attrs).flatten!.uniq!
29
+ end
30
+
31
+ def merge_with(*attrs)
32
+ object_merge.push(attrs).flatten!.uniq!
33
+ end
34
+
35
+ def define_to_h
36
+ class_eval <<-METHOD, __FILE__, __LINE__ + 1
37
+ def hashify
38
+ hash = {
39
+ #{
40
+ attribute_list.reduce('') do |method_str, attr|
41
+ if self.camel_case?
42
+ attr_name = attr.to_s.camelize(:lower)
43
+ else
44
+ attr_name = attr
45
+ end
46
+
47
+ if instance_methods.include?(attr)
48
+ method_str + "\"#{attr_name}\" => #{attr},"
49
+ else
50
+ method_str + "\"#{attr_name}\" => obj.#{attr},"
51
+ end
52
+ end
53
+ }
54
+ }
55
+
56
+ merge!(hash)
57
+ end
58
+ METHOD
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FTL::Serializer::DSL
4
+ module Format
5
+ attr_reader :root_name
6
+
7
+ def root(root_name)
8
+ @root_name = root_name
9
+ end
10
+
11
+ def format(format_type)
12
+ @hash_format = format_type
13
+ end
14
+
15
+ def hash_format
16
+ @hash_format ||= :underscore
17
+ end
18
+
19
+ def camel_case?
20
+ hash_format == :camel
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FTL
4
+ VERSION = "0.2.0"
5
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ftl-serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Fullscript
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: oj
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry-byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: FTL (Faster Than Light) is a ruby serializer that is optimized for simplicity
112
+ and speed.
113
+ email:
114
+ - devops@fullscript.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - bin/console
128
+ - bin/rspec
129
+ - bin/setup
130
+ - ftl-serializer.gemspec
131
+ - lib/ftl-serializer.rb
132
+ - lib/ftl/configuration.rb
133
+ - lib/ftl/errors.rb
134
+ - lib/ftl/railtie.rb
135
+ - lib/ftl/serializer.rb
136
+ - lib/ftl/serializer/base.rb
137
+ - lib/ftl/serializer/dsl.rb
138
+ - lib/ftl/serializer/dsl/format.rb
139
+ - lib/ftl/version.rb
140
+ homepage: https://fullscript.com
141
+ licenses:
142
+ - MIT
143
+ metadata:
144
+ homepage_uri: https://fullscript.com
145
+ source_code_uri: https://github.com/fullscript/ftl-serializer
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '2.3'
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubygems_version: 3.0.3
162
+ signing_key:
163
+ specification_version: 4
164
+ summary: A ruby serializer that can make the kessel run in less than 12 parsecs.
165
+ test_files: []