Skip to content

Commit 1663d48

Browse files
authored
Merge pull request #548 from euglena1215/active-model-serializer
Add active_model_serializers-0.10 gem signature
2 parents 455028e + c757609 commit 1663d48

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require "active_model_serializers"
2+
3+
module Test
4+
class Article < ActiveModelSerializers::Model
5+
# @dynamic title, title=, body, body=, tags, tags=, initialize
6+
attributes :title, :body, :tags
7+
end
8+
9+
class Tag < ActiveModelSerializers::Model
10+
# @dynamic name, name=, initialize
11+
attributes :name
12+
end
13+
14+
class ArticleSerializer < ActiveModel::Serializer
15+
attribute :title, key: :name
16+
attributes :body, :tag_names
17+
18+
def tag_names
19+
object.tags.map(&:name)
20+
end
21+
end
22+
23+
resource = Article.new(title: "Hello", body: "World", tags: [Tag.new(name: "foo"), Tag.new(name: "bar")])
24+
serialization = ActiveModelSerializers::SerializableResource.new(resource, serializer: ArticleSerializer)
25+
serialization.to_json
26+
serialization.as_json
27+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Test
2+
class Article < ActiveModelSerializers::Model
3+
attr_accessor title: String
4+
attr_accessor body: String
5+
attr_accessor tags: Array[Tag]
6+
7+
def initialize: (title: String, body: String, tags: Array[Tag]) -> void
8+
end
9+
10+
class Tag < ActiveModelSerializers::Model
11+
attr_accessor name: String
12+
13+
def initialize: (name: String) -> void
14+
end
15+
16+
class ArticleSerializer < ActiveModel::Serializer[Article]
17+
def tag_names: () -> Array[String]
18+
end
19+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module ActiveModel
2+
class Serializer[T]
3+
def self.attributes: (*Symbol attrs) -> void
4+
def self.attribute: (Symbol attr, Hash[Symbol, untyped] options) ?{ () -> untyped } -> void
5+
6+
attr_accessor object: T
7+
attr_accessor root: untyped
8+
attr_accessor scope: untyped
9+
10+
def initialize: (T object, Hash[Symbol, untyped] options) -> void
11+
end
12+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module ActiveModelSerializers
2+
class Model
3+
def self.attributes: (*Symbol) -> void
4+
5+
def initialize: (**untyped) -> void
6+
end
7+
8+
class SerializableResource
9+
def initialize: (untyped resource, Hash[Symbol, untyped] options) -> void
10+
def to_json: -> String
11+
def as_json: -> Hash[Symbol, untyped]
12+
end
13+
end

0 commit comments

Comments
 (0)