activerepresenter 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: daccec770392a3e34c24787aab513afb0f7389ad58463e92712af7935f779f8c
4
- data.tar.gz: a24a0bdf8b0591e6d975272298226352983f67f46fde63681ead3df97a7b3f34
3
+ metadata.gz: 4086b34f08da5fed29e928c30285f740b381c0a5538eb1dacb97537d4770a021
4
+ data.tar.gz: 3d369422e3e828c8ae9b6956b00fd20fa31efe7b69ac328b83b7ef10b716044c
5
5
  SHA512:
6
- metadata.gz: a280eeda5b03f13b209fd72d9dd0c548c703de6935ab637d3a6fedd468fece28b8c43a697d3fddcb7339df7f04828d94a07a5b6df1c62338eb3af34b37b75528
7
- data.tar.gz: de17fab51af0588ffed1d626c84e9c27950ab0652c3af8082ab6218ece77282409e2bae03d6ed299bdd7fe90c5b69091468c1506da4160150cb0857397c65e26
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
- # attr_collection:
22
- # Declare sub (containing) object array like has many association.
23
- # If sub object's representer is found, sub objects will be wrapped by it.
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.attr_collection(name, **options)
62
- unless name.is_a?(Symbol) || name.is_a?(String)
63
- raise ArgumentError.new("collection's name must be a Symbol or a String")
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
- representer_name = \
67
- options[:representer_name] ? options[:representer_name] : guess_representrer_name(name.to_s)
68
- raise ArgumentError.new("representer_name must be a String") unless representer_name.is_a?(String)
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 => e
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
@@ -11,7 +11,7 @@ end
11
11
 
12
12
  class UserRepresenter < ActiveRepresenter::Base
13
13
  attr_collection :activities
14
- attr_collection :notifications
14
+ attr_many :notifications
15
15
 
16
16
  def full_name
17
17
  "#{first_name} #{last_name}"
@@ -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.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-08 00:00:00.000000000 Z
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