easy_serializer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 700ae3daae7ea9a9b78584ba113372d51de24f29
4
+ data.tar.gz: 1918451e17315912e938cb82c2759b2346e6d4f6
5
+ SHA512:
6
+ metadata.gz: e55d290b237a04be9fef19fc3ceea6266a76b06baabce9b171436ec019d42e6ec6ca534cf59dbc612d8c41897a5935ad33f4c8166043a8614eb43a5c126a1067
7
+ data.tar.gz: 33150c86072b21384f81a2185a1b6ac1c054be9dfdfc7e6ad786b57293537bdb4f4ed960af519ac4ff1ea9b89218e5b55ee90e237a839921633dbb63ba2ddd7c
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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.6
4
+ before_install: gem install bundler -v 1.10.4
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in easy_serializer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Artur Pañach
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.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # EasySerializer
2
+
3
+ Semantic serializer for making easy serializing objects.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'easy_serializer'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install easy_serializer
20
+
21
+ ## Usage
22
+
23
+ example:
24
+ _serializer:_
25
+ ```ruby
26
+ class PolymorphicSerializer < EasySerializer::Base
27
+ cache true
28
+ attribute :segment_type do |object|
29
+ object.subject.class.name.demodulize
30
+ end
31
+ attribute :segment_id do |object|
32
+ object.id
33
+ end
34
+ attributes :initial_date,
35
+ :end_date
36
+
37
+ attribute :subject,
38
+ key: false,
39
+ serializer: proc {|serializer| serializer.serializer_for_subject },
40
+ cache: true
41
+ collection :elements, serializer: ElementsSerializer, cache: true
42
+
43
+ def serializer_for_subject
44
+ namespace = self.class.name.gsub(self.class.name.demodulize, '')
45
+ object_name = klass_ins.subject_type.demodulize
46
+ "#{namespace}#{object_name}Serializer".constantize
47
+ end
48
+ end
49
+ ```
50
+
51
+ ```ruby
52
+ PolymorphicSerializer.new(Polymorphic.last).serialize
53
+ # => Hash with the object serialized
54
+ ```
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ 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).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/easy_serializer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
65
+
66
+
67
+ ## License
68
+
69
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "easy_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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -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 'easy_serializer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "easy_serializer"
8
+ spec.version = EasySerializer::VERSION
9
+ spec.authors = ["Artur Pañach"]
10
+ spec.email = ["arturictus@gmail.com"]
11
+
12
+ spec.summary = %q{Semantic serializer for making easy serializing objects.}
13
+ spec.description = %q{Semantic serializer for making easy serializing objects.}
14
+ spec.homepage = "https://github.com/arturictus/easy_serializer"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
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.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.3"
25
+ spec.add_dependency 'activesupport', '~> 4.2'
26
+ end
@@ -0,0 +1,15 @@
1
+ require "easy_serializer/version"
2
+ require 'active_support/all'
3
+
4
+ module EasySerializer
5
+ include ActiveSupport::Configurable
6
+
7
+ def self.setup
8
+ yield self
9
+ end
10
+
11
+ config_accessor(:perform_caching) { false }
12
+ config_accessor(:cache)
13
+ end
14
+
15
+ require 'easy_serializer/base'
@@ -0,0 +1,119 @@
1
+ module EasySerializer
2
+ class Base
3
+ delegate :to_json, :[], to: :serialize
4
+ attr_reader :klass_ins
5
+ def initialize(klass_ins)
6
+ @klass_ins = klass_ins
7
+ end
8
+
9
+ class << self
10
+ def attribute(name, opts = {}, &block)
11
+ @__serializable_attributes ||= []
12
+ @__serializable_attributes << { name: name, block: block }.merge(opts)
13
+ end
14
+
15
+ def cache(bool, opts = {}, &block)
16
+ if bool
17
+ @__cache = opts.merge(block: block)
18
+ end
19
+ end
20
+
21
+ def attributes(*args)
22
+ args.each do |input|
23
+ attribute(*input)
24
+ end
25
+ end
26
+
27
+ def collection(name, opts = {}, &block)
28
+ opts = opts.merge(collection: true)
29
+ attribute(name, opts, &block)
30
+ end
31
+ end
32
+
33
+ def serialize
34
+ @serialize ||= serialized_from_cache || _serialize
35
+ end
36
+ alias_method :to_h, :serialize
37
+ alias_method :to_hash, :serialize
38
+ alias_method :to_s, :to_json
39
+
40
+ private
41
+
42
+ def _serialize
43
+ __serializable_attributes.each_with_object(HashWithIndifferentAccess.new) do |setup, object|
44
+ if setup[:key] === false
45
+ object.merge!(attr_serializer(setup))
46
+ else
47
+ key = (setup[:key] ? setup[:key] : setup[:name])
48
+ object[key] = attr_serializer(setup)
49
+ end
50
+ end
51
+ end
52
+
53
+ def serialized_from_cache
54
+ return false unless EasySerializer.perform_caching
55
+ cache = __cache
56
+ return false unless cache
57
+ if cache[:block]
58
+ cache[:block].call(klass_ins, self)
59
+ else
60
+ key = if cache[:key]
61
+ cache[:key].call(klass_ins)
62
+ else
63
+ [klass_ins, 'EasySerialized']
64
+ end
65
+ fail "[Serializer] No key for cache" unless key
66
+ EasySerializer.cache.fetch(key) { _serialize }
67
+ end
68
+ end
69
+
70
+ def __cache
71
+ self.class.instance_variable_get(:@__cache)
72
+ end
73
+
74
+ def __serializable_attributes
75
+ self.class.instance_variable_get(:@__serializable_attributes) || []
76
+ end
77
+
78
+ def attr_serializer(setup)
79
+ return setup[:block].call(klass_ins) if setup[:block]
80
+ content = klass_ins.send(setup[:name])
81
+ return content unless serializer = setup[:serializer]
82
+ if setup[:collection]
83
+ content.map { |o| cache_or_serialize(serializer, o, setup) }
84
+ else
85
+ cache_or_serialize(serializer, content, setup)
86
+ end
87
+ end
88
+
89
+ def cache_or_serialize(serializer, content, opts)
90
+ return unless content
91
+ if EasySerializer.perform_caching && opts[:cache]
92
+ key = if opts[:cache_key]
93
+ opts[:cache_key].call(content)
94
+ else
95
+ [content, 'EasySerialized']
96
+ end
97
+ # Be Aware
98
+ # We are cahing the serialized object
99
+ EasySerializer.cache.fetch(key) { send_to_serializer(serializer, content) }
100
+ else
101
+ send_to_serializer(serializer, content)
102
+ end
103
+ end
104
+
105
+ def from_setup_serializer(serializer, content)
106
+ case serializer
107
+ when Proc
108
+ serializer.call(self, klass_ins, content)
109
+ else
110
+ serializer
111
+ end
112
+ end
113
+
114
+ def send_to_serializer(serializer, content)
115
+ return unless content
116
+ from_setup_serializer(serializer, content).new(content).serialize
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,3 @@
1
+ module EasySerializer
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Artur Pañach
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-13 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.2'
69
+ description: Semantic serializer for making easy serializing objects.
70
+ email:
71
+ - arturictus@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - easy_serializer.gemspec
87
+ - lib/easy_serializer.rb
88
+ - lib/easy_serializer/base.rb
89
+ - lib/easy_serializer/version.rb
90
+ homepage: https://github.com/arturictus/easy_serializer
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.2.3
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: Semantic serializer for making easy serializing objects.
114
+ test_files: []