-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathdsl.rb
More file actions
178 lines (168 loc) · 5.67 KB
/
dsl.rb
File metadata and controls
178 lines (168 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
module JSONAPI
module Serializable
class Resource
module DSL
def self.extended(klass)
class << klass
attr_accessor :id_block, :type_val, :type_block, :attribute_blocks,
:relationship_blocks, :relationship_options,
:link_blocks, :meta_val, :meta_block
end
klass.attribute_blocks = {}
klass.relationship_blocks = {}
klass.relationship_options = {}
klass.link_blocks = {}
end
# rubocop:disable Metrics/AbcSize
def inherited(klass)
klass.id_block = id_block
klass.type_val = type_val
klass.type_block = type_block
klass.attribute_blocks = attribute_blocks.dup
klass.relationship_blocks = relationship_blocks.dup
klass.relationship_options = relationship_options.dup
klass.link_blocks = link_blocks.dup
klass.meta_val = meta_val
klass.meta_block = meta_block
end
# rubocop:enable Metrics/AbcSize
# Declare the JSON API id of this resource.
#
# @yieldreturn [String] The id of the resource.
#
# @example
# id { @object.id.to_s }
def id(&block)
self.id_block = block
end
# @overload type(value)
# Declare the JSON API type of this resource.
# @param [String] value The value of the type.
#
# @example
# type 'users'
#
# @overload type(&block)
# Declare the JSON API type of this resource.
# @yieldreturn [String] The value of the type.
#
# @example
# type { @object.type }
def type(value = nil, &block)
self.type_val = value.to_sym if value
self.type_block = block
end
# Declare an attribute for this resource.
#
# @param [Symbol] name The key of the attribute.
# @yieldreturn [Hash, String, nil] The block to compute the value.
#
# @example
# attribute(:name) { @object.name }
def attribute(name, _options = {}, &block)
block ||= proc { @object.public_send(name) }
attribute_blocks[name.to_sym] = block
end
# Declare a list of attributes for this resource.
#
# @param [Array] *args The attributes keys.
#
# @example
# attributes :title, :body, :date
def attributes(*args)
args.each do |attr|
attribute(attr)
end
end
# Declare a link for this resource. The properties of the link are set
# by providing a block in which the DSL methods of
# +JSONAPI::Serializable::Link+ are called, or the value of the link
# is returned directly.
# @see JSONAPI::Serialiable::Link
#
# @param [Symbol] name The key of the link.
# @yieldreturn [Hash, String, nil] The block to compute the value, if
# any.
#
# @example
# link(:self) do
# "http://api.example.com/users/#{@object.id}"
# end
#
# @example
# link(:self) do
# href "http://api.example.com/users/#{@object.id}"
# meta is_self: true
# end
def link(name, &block)
link_blocks[name] = block
end
# @overload meta(value)
# Declare the meta information for this resource.
# @param [Hash] value The meta information hash.
#
# @example
# meta key: value
#
# @overload meta(&block)
# Declare the meta information for this resource.
# @yieldreturn [String] The meta information hash.
# @example
# meta do
# { key: value }
# end
def meta(value = nil, &block)
self.meta_val = value
self.meta_block = block
end
# Declare a relationship for this resource. The properties of the
# relationship are set by providing a block in which the DSL methods
# of +JSONAPI::Serializable::Relationship+ are called.
# @see JSONAPI::Serializable::Relationship
#
# @param [Symbol] name The key of the relationship.
# @param [Hash] options The options for the relationship.
#
# @example
# relationship :author do
# data do
# @object.author
# end
# linkage do
# { type: 'users', id: @object.author_id }
# end
# link(:self) do
# "http://api.example.com/posts/#{@object.id}/relationships/author"
# end
# link(:related) do
# "http://api.example.com/posts/#{@object.id}/author"
# end
# meta do
# { author_online: @object.author.online? }
# end
# end
#
# @example
# relationship :books do
# data do
# @object.books
# end
# linkage do
# @object.books.map { |book| { id: book.id.to_s, type: 'books', name: book.name } }
# end
# end
def relationship(name, options = {}, &block)
rel_block = proc do
data { @object.public_send(name) }
instance_eval(&block) unless block.nil?
end
relationship_blocks[name.to_sym] = rel_block
relationship_options[name.to_sym] = options
end
alias has_many relationship
alias has_one relationship
alias belongs_to relationship
end
end
end
end