halitosis 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +32 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +360 -0
- data/Rakefile +12 -0
- data/lib/halitosis/base.rb +123 -0
- data/lib/halitosis/collection/field.rb +8 -0
- data/lib/halitosis/collection.rb +99 -0
- data/lib/halitosis/configuration.rb +13 -0
- data/lib/halitosis/errors.rb +11 -0
- data/lib/halitosis/field.rb +73 -0
- data/lib/halitosis/fields.rb +18 -0
- data/lib/halitosis/hash_util.rb +41 -0
- data/lib/halitosis/links/field.rb +76 -0
- data/lib/halitosis/links.rb +43 -0
- data/lib/halitosis/meta/field.rb +8 -0
- data/lib/halitosis/meta.rb +43 -0
- data/lib/halitosis/permissions/field.rb +8 -0
- data/lib/halitosis/permissions.rb +43 -0
- data/lib/halitosis/properties/field.rb +8 -0
- data/lib/halitosis/properties.rb +40 -0
- data/lib/halitosis/railtie.rb +23 -0
- data/lib/halitosis/relationships/field.rb +37 -0
- data/lib/halitosis/relationships.rb +80 -0
- data/lib/halitosis/resource.rb +58 -0
- data/lib/halitosis/version.rb +5 -0
- data/lib/halitosis.rb +85 -0
- data/sig/halitosis.rbs +4 -0
- metadata +77 -0
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Halitosis
|
4
|
+
# Behavior for serializers with a single primary resource
|
5
|
+
#
|
6
|
+
module Resource
|
7
|
+
def self.included(base)
|
8
|
+
if base.included_modules.include?(Collection)
|
9
|
+
raise InvalidResource, "#{base.name} has already defined a collection"
|
10
|
+
end
|
11
|
+
|
12
|
+
base.extend ClassMethods
|
13
|
+
|
14
|
+
base.send :include, InstanceMethods
|
15
|
+
|
16
|
+
base.send :attr_reader, :resource
|
17
|
+
|
18
|
+
base.class.send :attr_accessor, :resource_name
|
19
|
+
end
|
20
|
+
|
21
|
+
module ClassMethods
|
22
|
+
# @param name [Symbol, String] name of the resource
|
23
|
+
#
|
24
|
+
# @return [Module] self
|
25
|
+
#
|
26
|
+
def define_resource(name)
|
27
|
+
self.resource_name = name.to_s
|
28
|
+
|
29
|
+
alias_method name, :resource
|
30
|
+
end
|
31
|
+
|
32
|
+
# Override standard property field for resource-based serializers
|
33
|
+
#
|
34
|
+
# @param name [Symbol, String] name of the property
|
35
|
+
# @param options [nil, Hash] property options for field
|
36
|
+
#
|
37
|
+
def property(name, options = {}, &procedure)
|
38
|
+
super.tap do |field|
|
39
|
+
unless field.procedure || field.options.key?(:value)
|
40
|
+
field.procedure = proc { resource.send(name) }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
module InstanceMethods
|
47
|
+
# Override standard initializer to assign primary resource
|
48
|
+
#
|
49
|
+
# @param resource [Object] the primary resource
|
50
|
+
#
|
51
|
+
def initialize(resource, **)
|
52
|
+
@resource = resource
|
53
|
+
|
54
|
+
super(**)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/halitosis.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "halitosis/version"
|
4
|
+
|
5
|
+
require "json"
|
6
|
+
|
7
|
+
# HAL-like JSON generator
|
8
|
+
#
|
9
|
+
# Provides an interface for serializing resources as JSON
|
10
|
+
# with HAL-like links and relationships.
|
11
|
+
#
|
12
|
+
# @example
|
13
|
+
# class ArticleSerializer
|
14
|
+
# include Halitosis
|
15
|
+
#
|
16
|
+
# resource :article
|
17
|
+
#
|
18
|
+
# property :id, required: true
|
19
|
+
#
|
20
|
+
# property :title
|
21
|
+
#
|
22
|
+
# link :self, -> { article_path(article) }
|
23
|
+
#
|
24
|
+
# link :external, optional: true, -> { "http://example.com/read/#{article.id}" }
|
25
|
+
#
|
26
|
+
# rel :author, -> { UserSerializer.new(article.author) }
|
27
|
+
# # or relatioinship :author, -> { UserSerializer.new(article.author) }
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# render json: ArticleSerializer.new(article, include: :author, fields: :title)
|
31
|
+
module Halitosis
|
32
|
+
def self.included(base)
|
33
|
+
base.extend ClassMethods
|
34
|
+
|
35
|
+
base.include Base
|
36
|
+
|
37
|
+
config.extensions.each { |extension| base.send :include, extension }
|
38
|
+
end
|
39
|
+
|
40
|
+
module ClassMethods
|
41
|
+
def resource(name)
|
42
|
+
include Halitosis::Resource
|
43
|
+
|
44
|
+
define_resource(name)
|
45
|
+
end
|
46
|
+
|
47
|
+
def collection(name, ...)
|
48
|
+
include Halitosis::Collection
|
49
|
+
|
50
|
+
define_collection(name, ...)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class << self
|
55
|
+
# @yield [Halitosis::Configuration] configuration instance for modification
|
56
|
+
#
|
57
|
+
def configure
|
58
|
+
yield config
|
59
|
+
end
|
60
|
+
|
61
|
+
# Configuration instance
|
62
|
+
#
|
63
|
+
# @return [Halitosis::Configuration]
|
64
|
+
#
|
65
|
+
def config
|
66
|
+
@config ||= Configuration.new
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
require_relative "halitosis/base"
|
72
|
+
require_relative "halitosis/errors"
|
73
|
+
require_relative "halitosis/field"
|
74
|
+
require_relative "halitosis/fields"
|
75
|
+
require_relative "halitosis/properties"
|
76
|
+
require_relative "halitosis/links"
|
77
|
+
require_relative "halitosis/meta"
|
78
|
+
require_relative "halitosis/permissions"
|
79
|
+
require_relative "halitosis/relationships"
|
80
|
+
require_relative "halitosis/resource"
|
81
|
+
require_relative "halitosis/collection"
|
82
|
+
require_relative "halitosis/hash_util"
|
83
|
+
require_relative "halitosis/configuration"
|
84
|
+
|
85
|
+
require "halitosis/railtie" if defined?(::Rails)
|
data/sig/halitosis.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: halitosis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ben Morrall
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-09-30 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Provides an interface for serializing resources as JSON with HAL-like
|
14
|
+
links and relationships.
|
15
|
+
email:
|
16
|
+
- bemo56@hotmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".rspec"
|
22
|
+
- ".rubocop.yml"
|
23
|
+
- CHANGELOG.md
|
24
|
+
- CODE_OF_CONDUCT.md
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- lib/halitosis.rb
|
29
|
+
- lib/halitosis/base.rb
|
30
|
+
- lib/halitosis/collection.rb
|
31
|
+
- lib/halitosis/collection/field.rb
|
32
|
+
- lib/halitosis/configuration.rb
|
33
|
+
- lib/halitosis/errors.rb
|
34
|
+
- lib/halitosis/field.rb
|
35
|
+
- lib/halitosis/fields.rb
|
36
|
+
- lib/halitosis/hash_util.rb
|
37
|
+
- lib/halitosis/links.rb
|
38
|
+
- lib/halitosis/links/field.rb
|
39
|
+
- lib/halitosis/meta.rb
|
40
|
+
- lib/halitosis/meta/field.rb
|
41
|
+
- lib/halitosis/permissions.rb
|
42
|
+
- lib/halitosis/permissions/field.rb
|
43
|
+
- lib/halitosis/properties.rb
|
44
|
+
- lib/halitosis/properties/field.rb
|
45
|
+
- lib/halitosis/railtie.rb
|
46
|
+
- lib/halitosis/relationships.rb
|
47
|
+
- lib/halitosis/relationships/field.rb
|
48
|
+
- lib/halitosis/resource.rb
|
49
|
+
- lib/halitosis/version.rb
|
50
|
+
- sig/halitosis.rbs
|
51
|
+
homepage: https://github.com/bmorrall/halitosis
|
52
|
+
licenses:
|
53
|
+
- MIT
|
54
|
+
metadata:
|
55
|
+
homepage_uri: https://github.com/bmorrall/halitosis
|
56
|
+
source_code_uri: https://github.com/bmorrall/halitosis
|
57
|
+
changelog_uri: https://github.com/bmorrall/halitosis/main/CHANGELOG.md
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: 3.2.0
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubygems_version: 3.4.19
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: HAL-like API client for Ruby
|
77
|
+
test_files: []
|