supa 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 07a270a2a9086ea44a5c82efb0bdc8571ac219ba
4
+ data.tar.gz: 1a222147091f881a346fff4b6467a536b4e995ad
5
+ SHA512:
6
+ metadata.gz: 53e7f232f60a5ac4316a4c788802d564f5cbfdc46df0b80c59154fcd20affdd8f1ca18b85ec66084155ceb42dd39b73407f784094e09a361252de5b61f246421
7
+ data.tar.gz: db596cc0ca47081caa679d2f6689446004695b2ff4e9d92736cd144665069a21d8f5adc8ada0b4359c17c60e4724891bad2be0c11445152b208335f195eb02c5
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.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 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 andrey.duplischev@distribusion.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,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in supa.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Andrey D
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,209 @@
1
+ # Supa
2
+
3
+ Ruby object → JSON serialization.
4
+
5
+ [![Build Status](https://travis-ci.org/dasnotme/supa.svg?branch=master)](https://travis-ci.org/dasnotme/supa)
6
+
7
+ ## Introduction
8
+
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'supa'
16
+ ```
17
+
18
+ And then execute:
19
+ ```shell
20
+ bundle install
21
+ ```
22
+
23
+ Or install it yourself as:
24
+ ```
25
+ gem install supa
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ ### Example
31
+
32
+ ```ruby
33
+ class Article
34
+ attr_accessor :id, :title, :text, :author, :comments
35
+ end
36
+ ```
37
+ ```ruby
38
+ class Author
39
+ attr_accessor :id, :first_name, :last_name
40
+ end
41
+ ```
42
+ ```ruby
43
+ class Comment
44
+ attr_accessor :id, :text
45
+ end
46
+ ```
47
+
48
+ ```ruby
49
+ class ArticleRepresenter
50
+ include Supa::Representable
51
+
52
+ define do
53
+ namespace :jsonapi do
54
+ attribute :version, getter: proc { 1.1 }
55
+ end
56
+
57
+ namespace :data do
58
+ attribute :id
59
+ attribute :type, getter: proc { 'articles' }
60
+
61
+ namespace :attributes do
62
+ attribute :title
63
+ attribute :text
64
+ end
65
+
66
+ namespace :relationships do
67
+ object :author do
68
+ namespace :data do
69
+ attribute :id
70
+ attribute :type, getter: proc { 'authors' }
71
+ end
72
+ end
73
+
74
+ namespace :comments do
75
+ collection :data, getter: proc { self.comments } do
76
+ attribute :id
77
+ attribute :type, getter: proc { 'comments' }
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ polymorphic :included, getter: proc { [self.author] } do
84
+ attribute :id
85
+ attribute :type, getter: proc { 'authors' }
86
+
87
+ namespace :attributes do
88
+ attribute :first_name
89
+ attribute :last_name
90
+ end
91
+ end
92
+
93
+ polymorphic :included, getter: proc { self.comments } do
94
+ attribute :id
95
+ attribute :type, getter: proc { 'comments' }
96
+
97
+ namespace :attributes do
98
+ attribute :text
99
+ end
100
+ end
101
+ end
102
+ end
103
+ ```
104
+
105
+ ```ruby
106
+ ArticleRepresenter.new(Article.new).to_json
107
+ ```
108
+
109
+ ```json
110
+ {
111
+ "jsonapi": {
112
+ "version": 1.1
113
+ },
114
+ "data": {
115
+ "id": "7aa15512-1f9d-4a86-98ad-4bb0aae487a2",
116
+ "type": "articles",
117
+ "attributes": {
118
+ "title": "Pilot wave theory",
119
+ "text": "In theoretical physics, the pilot wave theory was the first known example of a hidden variable theory, presented by Louis de Broglie in 1927. Its more modern version, the de Broglie–Bohm theory, remains a non-mainstream attempt to interpret quantum mechanics as a deterministic theory, avoiding troublesome notions such as wave–particle duality, instantaneous wave function collapse and the paradox of Schrödinger's cat."
120
+ },
121
+ "relationships": {
122
+ "author": {
123
+ "data": {
124
+ "id": "52139b0b-bd22-4fc7-adc8-593f16ae034f",
125
+ "type": "authors"
126
+ }
127
+ },
128
+ "comments": {
129
+ "data": [
130
+ {
131
+ "id": "35a88ca5-80ec-4e49-9357-d8a16b8873f8",
132
+ "type": "comments"
133
+ },
134
+ {
135
+ "id": "0e02b198-299a-4e6b-99a0-8f2c33c15b1d",
136
+ "type": "comments"
137
+ }
138
+ ]
139
+ }
140
+ }
141
+ },
142
+ "included": [
143
+ {
144
+ "id": "52139b0b-bd22-4fc7-adc8-593f16ae034f",
145
+ "type": "authors",
146
+ "attributes": {
147
+ "first_name": "Louis",
148
+ "last_name": "de Broglie"
149
+ }
150
+ },
151
+ {
152
+ "id": "35a88ca5-80ec-4e49-9357-d8a16b8873f8",
153
+ "type": "comments",
154
+ "attributes": {
155
+ "text": "There can exist empty waves, represented by wave functions propagating in space and time but not carrying energy or momentum, and not associated with a particle."
156
+ }
157
+ },
158
+ {
159
+ "id": "0e02b198-299a-4e6b-99a0-8f2c33c15b1d",
160
+ "type": "comments",
161
+ "attributes": {
162
+ "text": "Let's call the concept ghost waves."
163
+ }
164
+ }
165
+ ]
166
+ }
167
+ ```
168
+
169
+ ### `attribute`
170
+
171
+ ### `namespace`
172
+
173
+ ### `object`
174
+
175
+ ### `collection`
176
+
177
+ ### `polymorphic`
178
+
179
+ ## Development
180
+
181
+ To install dependencies
182
+ ```shell
183
+ bin/setup
184
+ ```
185
+
186
+ To run tests
187
+ ```shell
188
+ bundle exec rake test
189
+ ```
190
+
191
+ To run benchmarks
192
+ ```shell
193
+ bundle exec rake bench
194
+ ```
195
+
196
+ To spin up an interactive prompt that will allow you to experiment
197
+ ```shell
198
+ bin/console
199
+ ```
200
+
201
+ ## Contributing
202
+
203
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dasnotme/supa. 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.
204
+
205
+
206
+ ## License
207
+
208
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
209
+
@@ -0,0 +1,14 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << %w(spec lib)
6
+ t.test_files = FileList['spec/**/*_spec.rb']
7
+ end
8
+
9
+ Rake::TestTask.new(:bench) do |t|
10
+ t.libs << %w(spec lib)
11
+ t.test_files = FileList['spec/**/*_bench.rb']
12
+ end
13
+
14
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "supa"
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
@@ -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,6 @@
1
+ require 'supa/version'
2
+ require 'supa/builder'
3
+ require 'supa/representable'
4
+
5
+ module Supa
6
+ end
@@ -0,0 +1,39 @@
1
+ require 'supa/commands/attribute'
2
+ require 'supa/commands/object'
3
+ require 'supa/commands/namespace'
4
+ require 'supa/commands/collection'
5
+ require 'supa/commands/polymorphic'
6
+
7
+ module Supa
8
+ class Builder
9
+ COMMANDS = %w(attribute object namespace collection polymorphic).freeze
10
+
11
+ COMMANDS.each do |command|
12
+ klass = Supa::Commands.const_get(command.capitalize)
13
+
14
+ define_method command do |name, options = {}, &block|
15
+ klass.new(context: context,
16
+ tree: tree,
17
+ name: name,
18
+ options: options,
19
+ &block).represent
20
+ end
21
+ end
22
+
23
+ def initialize(context:, tree:)
24
+ @context = context
25
+ @tree = tree
26
+ end
27
+
28
+ def to_hash
29
+ tree.to_hash
30
+ end
31
+
32
+ def to_json
33
+ to_hash.to_json
34
+ end
35
+
36
+ private
37
+ attr_reader :context, :tree
38
+ end
39
+ end
@@ -0,0 +1,26 @@
1
+ module Supa
2
+ class Command
3
+ def initialize(context:, tree:, name:, options: {}, &block)
4
+ @context = context
5
+ @tree = tree
6
+ @name = name
7
+ @options = options
8
+ @block = block
9
+ end
10
+
11
+ def represent
12
+ raise NotImplementedError
13
+ end
14
+
15
+ private
16
+ attr_reader :context, :tree, :name, :options, :block
17
+
18
+ def with_getter?
19
+ options[:getter].is_a?(Proc)
20
+ end
21
+
22
+ def getter
23
+ options[:getter]
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ require 'supa/command'
2
+
3
+ module Supa
4
+ module Commands
5
+ class Attribute < Supa::Command
6
+ def represent
7
+ value = with_getter? ? context.instance_exec(&getter) : context.send(name)
8
+
9
+ tree[name] = value
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ require 'supa/command'
2
+
3
+ module Supa
4
+ module Commands
5
+ class Collection < Supa::Command
6
+ def represent
7
+ values = with_getter? ? context.instance_exec(&getter) : context.send(name)
8
+
9
+ tree[name] = []
10
+
11
+ Array(values).each do |value|
12
+ tree[name] << {}
13
+
14
+ Supa::Builder.new(context: value, tree: tree[name][-1]).instance_exec(&block)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ require 'supa/command'
2
+
3
+ module Supa
4
+ module Commands
5
+ class Namespace < Supa::Command
6
+ def represent
7
+ tree[name] = {}
8
+
9
+ Supa::Builder.new(context: context, tree: tree[name]).instance_exec(&block)
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+
@@ -0,0 +1,15 @@
1
+ require 'supa/command'
2
+
3
+ module Supa
4
+ module Commands
5
+ class Object < Supa::Command
6
+ def represent
7
+ value = with_getter? ? context.instance_exec(&getter) : context.send(name)
8
+
9
+ tree[name] = {}
10
+
11
+ Supa::Builder.new(context: value, tree: tree[name]).instance_exec(&block)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ require 'supa/command'
2
+
3
+ module Supa
4
+ module Commands
5
+ class Polymorphic < Supa::Command
6
+ def represent
7
+ values = with_getter? ? context.instance_exec(&getter) : context.send(name)
8
+
9
+ tree[name] ||= []
10
+
11
+ Array(values).each do |value|
12
+ tree[name] << {}
13
+
14
+ Supa::Builder.new(context: value, tree: tree[name][-1]).instance_exec(&block)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,35 @@
1
+ module Supa
2
+ module Representable
3
+ def self.included(base)
4
+ base.send :include, InstanceMethods
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module InstanceMethods
9
+ def initialize(object)
10
+ @object = object
11
+ end
12
+
13
+ def to_hash
14
+ Supa::Builder.new(context: object, tree: {}).tap do |builder|
15
+ builder.instance_exec(&self.class.definition)
16
+ end.to_hash
17
+ end
18
+
19
+ def to_json
20
+ to_hash.to_json
21
+ end
22
+
23
+ private
24
+ attr_reader :object
25
+ end
26
+
27
+ module ClassMethods
28
+ def define(&block)
29
+ @definition = block
30
+ end
31
+
32
+ attr_reader :definition
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module Supa
2
+ VERSION = "0.1.1"
3
+ end
Binary file
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'supa/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "supa"
8
+ spec.version = Supa::VERSION
9
+ spec.authors = ["Das"]
10
+ spec.email = [""]
11
+
12
+ spec.summary = "Ruby object → JSON serialization."
13
+ spec.description = "Ruby object → JSON serialization."
14
+ spec.homepage = "https://github.com/dasnotme/supa"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.13"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "minitest", "~> 5.0"
36
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: supa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Das
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-23 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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Ruby object → JSON serialization.
56
+ email:
57
+ - ''
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .travis.yml
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/supa.rb
72
+ - lib/supa/builder.rb
73
+ - lib/supa/command.rb
74
+ - lib/supa/commands/attribute.rb
75
+ - lib/supa/commands/collection.rb
76
+ - lib/supa/commands/namespace.rb
77
+ - lib/supa/commands/object.rb
78
+ - lib/supa/commands/polymorphic.rb
79
+ - lib/supa/representable.rb
80
+ - lib/supa/version.rb
81
+ - supa-0.1.0.gem
82
+ - supa.gemspec
83
+ homepage: https://github.com/dasnotme/supa
84
+ licenses:
85
+ - MIT
86
+ metadata:
87
+ allowed_push_host: 'TODO: Set to ''http://mygemserver.com'''
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.0.14.1
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Ruby object → JSON serialization.
108
+ test_files: []