activerepresenter 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/active_representer/base.rb +55 -10
- data/test/attr_collection_test.rb +1 -1
- data/test/attr_one_test.rb +34 -0
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4086b34f08da5fed29e928c30285f740b381c0a5538eb1dacb97537d4770a021
|
|
4
|
+
data.tar.gz: 3d369422e3e828c8ae9b6956b00fd20fa31efe7b69ac328b83b7ef10b716044c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1dbc03c982b23a4efae657a2f6e4ec0fee0a34aeec9a524079bb60e3cd8e1dff70d76d4b1547d19ab115c38a7289b46ccea6fccd94c63f304698b57da96a5b14
|
|
7
|
+
data.tar.gz: '018633f610e9280c9035c7a57c39bfd782bff62d7129755aa0f1870ea53090ab5b1e265d1b673d945bb80c73ed012ade53acc69cc50e9c7bfd5770eb4273953e'
|
|
@@ -18,15 +18,21 @@ module ActiveRepresenter
|
|
|
18
18
|
# It uses ActiveModel::Attributes internally.
|
|
19
19
|
# See examples: AttrFieldTest in test/attr_field_test.
|
|
20
20
|
#
|
|
21
|
-
#
|
|
22
|
-
# Declare
|
|
23
|
-
# If
|
|
21
|
+
# attr_one:
|
|
22
|
+
# Declare an associated object like has one association.
|
|
23
|
+
# If a representer for the object is found, the object will be wrapped by the representer.
|
|
24
|
+
# See examples: AttrOneTest in test/attr_one_test.
|
|
25
|
+
#
|
|
26
|
+
# attr_many:
|
|
27
|
+
# Declare associated objects like has many association.
|
|
28
|
+
# If a representer for the objects is found, the objects will be wrapped by the representer.
|
|
24
29
|
# See examples: AttrCollectionTest in test/attr_collection_test.
|
|
25
30
|
class Base
|
|
26
31
|
include ActiveModel::Model
|
|
27
32
|
include ActiveModel::Attributes
|
|
28
33
|
|
|
29
34
|
attribute :wrapped
|
|
35
|
+
class_attribute :ones, default: {}
|
|
30
36
|
class_attribute :collections, default: {}
|
|
31
37
|
|
|
32
38
|
delegate_missing_to :wrapped
|
|
@@ -35,6 +41,18 @@ module ActiveRepresenter
|
|
|
35
41
|
instance = new
|
|
36
42
|
instance.wrapped = wrapped
|
|
37
43
|
|
|
44
|
+
one_names.each do |one_name|
|
|
45
|
+
next if wrapped[one_name].nil?
|
|
46
|
+
representer_klass = ones[one_name]
|
|
47
|
+
one_value = \
|
|
48
|
+
if representer_klass
|
|
49
|
+
representer_klass.wrap(wrapped[one_name])
|
|
50
|
+
else
|
|
51
|
+
wrapped[one_name]
|
|
52
|
+
end
|
|
53
|
+
instance.instance_variable_set("@#{one_name}", one_value)
|
|
54
|
+
end
|
|
55
|
+
|
|
38
56
|
collection_names.each do |collection_name|
|
|
39
57
|
next if wrapped[collection_name].nil?
|
|
40
58
|
representer_klass = collections[collection_name]
|
|
@@ -58,19 +76,30 @@ module ActiveRepresenter
|
|
|
58
76
|
attribute(name, type, **options)
|
|
59
77
|
end
|
|
60
78
|
|
|
61
|
-
def self.
|
|
62
|
-
|
|
63
|
-
|
|
79
|
+
def self.attr_one(name, **options)
|
|
80
|
+
check_name_type(name)
|
|
81
|
+
representer_name = specify_or_guess_representer_name(options[:representer_name], name)
|
|
82
|
+
|
|
83
|
+
begin
|
|
84
|
+
representer = representer_name.constantize
|
|
85
|
+
ones[name.to_sym] = representer
|
|
86
|
+
rescue NameError
|
|
87
|
+
ones[name.to_sym] = nil
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
class_eval do
|
|
91
|
+
attr_reader name.to_sym
|
|
64
92
|
end
|
|
93
|
+
end
|
|
65
94
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
95
|
+
def self.attr_collection(name, **options)
|
|
96
|
+
check_name_type(name)
|
|
97
|
+
representer_name = specify_or_guess_representer_name(options[:representer_name], name)
|
|
69
98
|
|
|
70
99
|
begin
|
|
71
100
|
representer = representer_name.constantize
|
|
72
101
|
collections[name.to_sym] = representer
|
|
73
|
-
rescue NameError
|
|
102
|
+
rescue NameError
|
|
74
103
|
collections[name.to_sym] = nil
|
|
75
104
|
end
|
|
76
105
|
|
|
@@ -79,6 +108,12 @@ module ActiveRepresenter
|
|
|
79
108
|
end
|
|
80
109
|
end
|
|
81
110
|
|
|
111
|
+
singleton_class.send(:alias_method, :attr_many, :attr_collection)
|
|
112
|
+
|
|
113
|
+
def self.one_names
|
|
114
|
+
ones.keys
|
|
115
|
+
end
|
|
116
|
+
|
|
82
117
|
def self.collection_names
|
|
83
118
|
collections.keys
|
|
84
119
|
end
|
|
@@ -87,8 +122,18 @@ module ActiveRepresenter
|
|
|
87
122
|
attribute_types.keys - ["wrapped"]
|
|
88
123
|
end
|
|
89
124
|
|
|
125
|
+
def self.check_name_type(name)
|
|
126
|
+
unless name.is_a?(Symbol) || name.is_a?(String)
|
|
127
|
+
raise ArgumentError.new("collection's name must be a Symbol or a String")
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
90
131
|
def self.guess_representrer_name(name)
|
|
91
132
|
"#{name.singularize.camelize}Representer"
|
|
92
133
|
end
|
|
134
|
+
|
|
135
|
+
def self.specify_or_guess_representer_name(specified_name, wrapped_name)
|
|
136
|
+
specified_name ? specified_name.to_s : guess_representrer_name(wrapped_name.to_s)
|
|
137
|
+
end
|
|
93
138
|
end
|
|
94
139
|
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
require "test/unit"
|
|
2
|
+
require "ostruct"
|
|
3
|
+
require "active_support/time"
|
|
4
|
+
require_relative "../lib/active_representer/base.rb"
|
|
5
|
+
|
|
6
|
+
class ProfileRepresenter < ActiveRepresenter::Base
|
|
7
|
+
def email_sent_on
|
|
8
|
+
email_sent_at.to_date
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
class UserRepresenter < ActiveRepresenter::Base
|
|
13
|
+
attr_one :profile
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class AttrOneTest < Test::Unit::TestCase
|
|
17
|
+
test "profile is represented" do
|
|
18
|
+
user = OpenStruct.new(
|
|
19
|
+
profile: OpenStruct.new(email_sent_at: Time.now)
|
|
20
|
+
)
|
|
21
|
+
representer = UserRepresenter.wrap(user)
|
|
22
|
+
profile = representer.profile
|
|
23
|
+
assert_instance_of(ProfileRepresenter, profile)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
test "profile.email_sent_on is Date.today" do
|
|
27
|
+
user = OpenStruct.new(
|
|
28
|
+
profile: OpenStruct.new(email_sent_at: Time.now)
|
|
29
|
+
)
|
|
30
|
+
representer = UserRepresenter.wrap(user)
|
|
31
|
+
profile = representer.profile
|
|
32
|
+
assert_equal(profile.email_sent_on, Date.today)
|
|
33
|
+
end
|
|
34
|
+
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: activerepresenter
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ryo Hashimoto
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2020-03-
|
|
11
|
+
date: 2020-03-15 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -105,6 +105,7 @@ files:
|
|
|
105
105
|
- lib/activerepresenter.rb
|
|
106
106
|
- test/attr_collection_test.rb
|
|
107
107
|
- test/attr_field_test.rb
|
|
108
|
+
- test/attr_one_test.rb
|
|
108
109
|
- test/base_test.rb
|
|
109
110
|
- test/inheritance_test.rb
|
|
110
111
|
homepage: https://github.com/ryohashimoto/lightrails
|
|
@@ -134,5 +135,6 @@ summary: Active Representer provides model objects by decorating objects (part o
|
|
|
134
135
|
test_files:
|
|
135
136
|
- test/attr_collection_test.rb
|
|
136
137
|
- test/attr_field_test.rb
|
|
138
|
+
- test/attr_one_test.rb
|
|
137
139
|
- test/base_test.rb
|
|
138
140
|
- test/inheritance_test.rb
|