excel_serializer 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0ea8bac11e5e6a57e556ee0e7d25c61f6ca45d84780c03c89cfecca2f5c81a3b
4
+ data.tar.gz: 89ab8cfb455f2b493f74c56b3556471f9a9486e15614525909c97f5a5bc5f92b
5
+ SHA512:
6
+ metadata.gz: a31f41444348e1961308b0758c834039ce38476383fb0faf366ec0973419d7515970ce95cea8b28310b019275bd09d45cc138bb13fc5591e5e4a060a97b952c5
7
+ data.tar.gz: ad4d6f94bef0842c2c93874231b093b336f8764937e881f036b9d6f62d39a31886839a7dcc3a9bd3d0ca2fecd5c8c63cba7ff89411e639a964ac775614322887
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.4
@@ -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 making 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 storresy@innventto.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 [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in excel_serializer.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Sebastian Torres
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,67 @@
1
+ # ExcelSerializer
2
+
3
+ This gem allows you to easily export ruby objects to excel files by defining a serializer for each object. ExcelSerializer can work with multiple excel writting gems and allows you to translate the document headers using I18n.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'excel_serializer'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install excel_serializer
20
+
21
+ ## Usage
22
+
23
+ ### Serializer definition
24
+
25
+ ```ruby
26
+ class AttendeeExcelSerializer
27
+ include ExcelSerializer::ObjectSerializer
28
+
29
+ attributes :uuid, :first_name, :last_name, :created_at
30
+ belongs_to :event
31
+
32
+ excel_adapter :write_excel # optional
33
+ translation_adapter :i18n # optional
34
+
35
+ def created_at
36
+ object.created_at&.strftime("%Y-%m-%d")
37
+ end
38
+ end
39
+ ```
40
+
41
+ ### Excel file generation
42
+
43
+ ```ruby
44
+ AttendeeExcelSerializer.new(attendees).excel_file
45
+ ```
46
+
47
+ excel_file method receives an optional string parameter path, which will be used as the generated file path. If this param is not present the file will be generated in /tmp folder with a random string name.
48
+
49
+
50
+ ## Development
51
+
52
+ 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.
53
+
54
+ 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).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/storresy/excel_serializer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/storresy/excel_serializer/blob/master/CODE_OF_CONDUCT.md).
59
+
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
64
+
65
+ ## Code of Conduct
66
+
67
+ Everyone interacting in the ExcelSerializer project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/storresy/excel_serializer/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "excel_serializer"
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(__FILE__)
@@ -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,28 @@
1
+ require_relative 'lib/excel_serializer/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "excel_serializer"
5
+ spec.version = ExcelSerializer::VERSION
6
+ spec.authors = ["Sebastian Torres"]
7
+
8
+ spec.summary = %q{An excel serializer for Ruby objects}
9
+ spec.description = %q{This gem allows you to easily export ruby objects to excel files by defining a serializer for each object. ExcelSerializer can work with multiple excel writting gems and allows you to translate the document headers using I18n.}
10
+ spec.homepage = "https://github.com/storresy/excel_serializer"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/storresy/excel_serializer"
18
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+ end
@@ -0,0 +1,28 @@
1
+ require "excel_serializer/version"
2
+ require "excel_serializer/object_serializer"
3
+
4
+ module ExcelSerializer
5
+ class ExcelAdapterNotFoundError < StandardError
6
+ def initialize(class_name, gem_name)
7
+ msg = "#{class_name} could not be found. Please ensure you have '#{gem_name}' gem installed or setup a different excel adapter"
8
+ super(msg)
9
+ end
10
+ end
11
+ class Config
12
+ attr_accessor :excel_adapter, :translation_adapter
13
+
14
+ def initialize(excel_adapter: :write_excel,
15
+ translation_adapter: :i18n)
16
+ self.excel_adapter = excel_adapter
17
+ self.translation_adapter = translation_adapter
18
+ end
19
+ end
20
+
21
+ def self.config
22
+ @@config ||= Config.new
23
+ end
24
+
25
+ def self.configure
26
+ yield(self.config)
27
+ end
28
+ end
@@ -0,0 +1,37 @@
1
+ module ExcelSerializer
2
+ module Adapters
3
+ class WriteExcel
4
+ # This is the adapter for WriteExcel gem
5
+
6
+ def initialize(file_path)
7
+ @write_excel = ::WriteExcel.new(file_path)
8
+ rescue NameError => e
9
+ raise ExcelAdapterNotFoundError.new('WriteExcel', 'writeexcel')
10
+ end
11
+
12
+ def add_worksheet(sheet_name)
13
+ WorkSheet.new(@write_excel, sheet_name)
14
+ end
15
+
16
+ def save
17
+ @write_excel.close
18
+ end
19
+
20
+ def self.file_extension
21
+ '.xls'
22
+ end
23
+
24
+ class WorkSheet
25
+ def initialize(write_excel, sheet_name)
26
+ @write_excel = write_excel
27
+ @worksheet = @write_excel.add_worksheet(sheet_name)
28
+ end
29
+
30
+ def write(row, column, value)
31
+ @worksheet.write(row, column, value)
32
+ end
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,58 @@
1
+ module ExcelSerializer
2
+ module HeadersTranslator
3
+ extend ActiveSupport::Concern
4
+
5
+ class_methods do
6
+ def compute_headers
7
+ if computed_headers.blank?
8
+ self.computed_headers ||= []
9
+ self.attributes_to_serialize.each do |attribute|
10
+ self.computed_headers << translate_attribute(attribute)
11
+ end
12
+ end
13
+ computed_headers
14
+ end
15
+
16
+ def translation_key_base(translation_key_base)
17
+ @translation_key_base = translation_key_base
18
+ end
19
+
20
+ def translate_attribute(attribute)
21
+ case self.current_config.translation_adapter
22
+ when :i18n
23
+ i18n_translation(attribute)
24
+ when :humanize
25
+ humanize(attribute)
26
+ else
27
+ raise "Invalid translations adapter"
28
+ end
29
+ end
30
+
31
+ def i18n_translation(attribute)
32
+ I18n.t("#{translation_base}.#{attribute}")
33
+ end
34
+
35
+ def humanize(attribute)
36
+ attribute.to_s.humanize
37
+ end
38
+
39
+ def translation_base
40
+ return @translation_key_base if @translation_key_base.present?
41
+ obj_name = self.name.sub('ExcelSerializer','').underscore
42
+ @translation_key_base = "activerecord.attributes.#{obj_name}"
43
+ end
44
+ end
45
+
46
+ def headers
47
+ @headers ||= compute_headers
48
+ end
49
+
50
+ def compute_headers
51
+ @headers = self.class.compute_headers
52
+ self.class.included_relations.each do |key, hash|
53
+ @headers += hash[:serializer].constantize.compute_headers
54
+ end
55
+ @headers
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,106 @@
1
+ require "excel_serializer/writer"
2
+ require "excel_serializer/headers_translator"
3
+
4
+ module ExcelSerializer
5
+ module ObjectSerializer
6
+ extend ActiveSupport::Concern
7
+ include ::ExcelSerializer::Writer
8
+ include ::ExcelSerializer::HeadersTranslator
9
+
10
+ included do
11
+ attr_accessor :resources
12
+
13
+ class << self
14
+ attr_accessor :attributes_to_serialize
15
+ attr_accessor :included_relations
16
+ attr_accessor :computed_headers
17
+ attr_accessor :config
18
+ end
19
+ end
20
+
21
+ class_methods do
22
+ def attributes(*attributes_list)
23
+ self.attributes_to_serialize ||= []
24
+ self.attributes_to_serialize += attributes_list
25
+ end
26
+
27
+ def belongs_to(relation, serializer: nil)
28
+ self.included_relations ||= {}
29
+ self.included_relations[relation.to_s] = {
30
+ method: relation,
31
+ serializer: (serializer || relation_serializer(relation))
32
+ }
33
+ end
34
+
35
+ def excel_adapter(adapter)
36
+ current_config.excel_adapter = adapter
37
+ end
38
+
39
+ def translation_adapter(adapter)
40
+ current_config.translation_adapter = adapter
41
+ end
42
+
43
+ def current_config
44
+ self.config ||= ExcelSerializer.config.dup
45
+ end
46
+
47
+ private
48
+
49
+ def relation_serializer(relation)
50
+ "#{relation.to_s.singularize.camelcase}ExcelSerializer"
51
+ end
52
+ end
53
+
54
+ def initialize(resources, file_path: nil)
55
+ self.file_path = file_path
56
+ if resources.is_a?(ActiveRecord::Relation) or resources.is_a?(Array)
57
+ self.resources = resources
58
+ else
59
+ self.resources = [resources]
60
+ end
61
+ end
62
+
63
+ def excel_file
64
+ add_headers('Main')
65
+ method_name = 'each'
66
+ if resources.is_a?(ActiveRecord::Relation)
67
+ method_name = 'find_each'
68
+ end
69
+ resources.send(method_name) do |resource|
70
+ write('Main', single_resource_all_info(resource))
71
+ end
72
+ close_workbook
73
+ end
74
+
75
+ def object
76
+ @object
77
+ end
78
+
79
+ def single_resource_all_info(resource)
80
+ all_info = single_resource_info(resource)
81
+ self.class.included_relations.each do |key, hash|
82
+ all_info += hash[:serializer].constantize.new(nil).single_resource_info(
83
+ resource.send(hash[:method])
84
+ )
85
+ end
86
+ all_info
87
+ end
88
+
89
+ def single_resource_info(resource)
90
+ self.object= resource
91
+ self.class.attributes_to_serialize.map do |attribute|
92
+ if self.respond_to?(attribute)
93
+ self
94
+ else
95
+ resource
96
+ end.send(attribute)
97
+ end
98
+ end
99
+
100
+ private
101
+
102
+ def object=(object)
103
+ @object = object
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,3 @@
1
+ module ExcelSerializer
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ module ExcelSerializer
2
+ class Worksheet
3
+ attr_accessor :sheet, :row_counter
4
+
5
+ def initialize(current_excel, sheet_name)
6
+ @sheet = current_excel.add_worksheet(sheet_name)
7
+ @row_counter = 0
8
+ end
9
+
10
+ def write_row(row)
11
+ row_index = @row_counter += 1
12
+ row.each_with_index do |value, current_column|
13
+ @sheet.write(row_index, current_column, value)
14
+ end
15
+ end
16
+
17
+ def write_headers(headers)
18
+ headers.each_with_index do |value, current_column|
19
+ @sheet.write(0, current_column, value)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,59 @@
1
+ require "excel_serializer/worksheet"
2
+ require "excel_serializer/adapters/write_excel"
3
+
4
+ module ExcelSerializer
5
+ module Writer
6
+ extend ActiveSupport::Concern
7
+
8
+ private
9
+
10
+ def add_headers(sheet_name)
11
+ sheet(sheet_name).write_headers(headers)
12
+ end
13
+
14
+ def current_excel
15
+ return @current_excel if @current_excel.present?
16
+ @current_excel = adapter.new(file_path)
17
+ end
18
+
19
+ def write(sheet_name, row)
20
+ sheet(sheet_name).write_row(row)
21
+ end
22
+
23
+ def sheet(sheet_name)
24
+ @sheets ||= {}
25
+ @sheets[sheet_name]= worksheet(sheet_name) if @sheets[sheet_name].blank?
26
+ @sheets[sheet_name]
27
+ end
28
+
29
+ def worksheet(sheet_name)
30
+ ::ExcelSerializer::Worksheet.new(current_excel, sheet_name)
31
+ end
32
+
33
+ def file_path=(file_path)
34
+ if file_path.blank?
35
+ @file_path = "/tmp/#{SecureRandom.uuid}#{adapter.file_extension}"
36
+ else
37
+ @file_path= file_path
38
+ end
39
+ end
40
+
41
+ def file_path
42
+ @file_path
43
+ end
44
+
45
+ def close_workbook
46
+ current_excel.save
47
+ return file_path
48
+ end
49
+
50
+ def adapter
51
+ case self.class.current_config.excel_adapter
52
+ when :write_excel
53
+ ::ExcelSerializer::Adapters::WriteExcel
54
+ else
55
+ raise "Invalid excel writer adapter"
56
+ end
57
+ end
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: excel_serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sebastian Torres
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This gem allows you to easily export ruby objects to excel files by defining
14
+ a serializer for each object. ExcelSerializer can work with multiple excel writting
15
+ gems and allows you to translate the document headers using I18n.
16
+ email:
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".travis.yml"
24
+ - CODE_OF_CONDUCT.md
25
+ - Gemfile
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/setup
31
+ - excel_serializer.gemspec
32
+ - lib/excel_serializer.rb
33
+ - lib/excel_serializer/adapters/write_excel.rb
34
+ - lib/excel_serializer/headers_translator.rb
35
+ - lib/excel_serializer/object_serializer.rb
36
+ - lib/excel_serializer/version.rb
37
+ - lib/excel_serializer/worksheet.rb
38
+ - lib/excel_serializer/writer.rb
39
+ homepage: https://github.com/storresy/excel_serializer
40
+ licenses:
41
+ - MIT
42
+ metadata:
43
+ homepage_uri: https://github.com/storresy/excel_serializer
44
+ source_code_uri: https://github.com/storresy/excel_serializer
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.3.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.1.2
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: An excel serializer for Ruby objects
64
+ test_files: []