activerepresenter 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/active_representer/base.rb +66 -44
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fdd0c7efa14f672dba1fd48fb7e930fb382628cb552e7695601e26cd1e799f82
4
- data.tar.gz: 7bf5638cfd3896aa45f7746154e8b6d41d796d152233b33d9e3506682cf75eb8
3
+ metadata.gz: d2747744f8459f07ebdfe15c26a152ea8da162d1f969734f4e57d881bdd4fafa
4
+ data.tar.gz: e8f959c5390fb1ea77ffec78ace4b69d874ad8fd2ef673f95bd93a27182c66ec
5
5
  SHA512:
6
- metadata.gz: 6d354a1299513afbfc555bd93f9d5a212d038a047f3da5d6c0a77061b2249ba64f130e623e533de6bc7546c1c01a6a1af76a8a098a939381878a0bbb79249342
7
- data.tar.gz: '048eb40b6d05b93227d5c318cdb01594b2df688cb68dfde1c76e8019090281b12fc30b080e3c406a7355197e0d1b0d34e51dc07485efa174727753e39ce53f28'
6
+ metadata.gz: 8c53e4051cd07c2c7e18b1aa37578f3c6d366050a45abbca576d7a784209d482bb2c5c2f752e8e95f7158888edb9aa37a425db84af2c72e2c6326226e82ad47f
7
+ data.tar.gz: b0771c5c2363e650012862099931c2e15071cd98409017f3874f0abfc4a1000ba2462fc1069411da35c7a10da8891a46448032fd6cbc5bbacc90a6a119a5d9f5
@@ -4,6 +4,24 @@ require "active_model"
4
4
  require "active_support"
5
5
 
6
6
  module ActiveRepresenter
7
+ # == Action \Representer \Base
8
+ #
9
+ # This is a base class for a representer (wrapped object).
10
+ # It wraps the original object as `wrapped` attribute and
11
+ # you can add custom methods to the class (using the decorator pattern).
12
+ #
13
+ # In addition, `attr_field` / `attr_collection` can be used for attributes.
14
+ #
15
+ # attr_field:
16
+ # Declare additional field and type to the objects.
17
+ # You can get / set field's value (converted to corresponding).
18
+ # It uses ActiveModel::Attributes internally.
19
+ # See examples: AttrFieldTest in test/attr_field_test.
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.
24
+ # See examples: AttrCollectionTest in test/attr_collection_test.
7
25
  class Base
8
26
  include ActiveModel::Model
9
27
  include ActiveModel::Attributes
@@ -13,60 +31,64 @@ module ActiveRepresenter
13
31
 
14
32
  delegate_missing_to :wrapped
15
33
 
16
- class << self
17
- def wrap(wrapped)
18
- instance = new
19
- instance.wrapped = wrapped
20
- collection_names.each do |collection_name|
21
- next if wrapped[collection_name].nil?
22
- representer_klass = collections[collection_name]
23
- collection_value = \
24
- if representer_klass
25
- wrapped[collection_name].map { |item| representer_klass.wrap(item) }
26
- else
27
- wrapped[collection_name]
28
- end
29
- instance.instance_variable_set("@#{collection_name}", collection_value)
30
- end
31
- attribute_names.each do |attribute_name|
32
- instance.send("#{attribute_name}=", wrapped.send(attribute_name))
33
- end
34
- instance
35
- end
34
+ def self.wrap(wrapped)
35
+ instance = new
36
+ instance.wrapped = wrapped
36
37
 
37
- def attr_field(name, type = Type::Value.new, **options)
38
- attribute(name, type, **options)
38
+ collection_names.each do |collection_name|
39
+ next if wrapped[collection_name].nil?
40
+ representer_klass = collections[collection_name]
41
+ collection_value = \
42
+ if representer_klass
43
+ wrapped[collection_name].map { |item| representer_klass.wrap(item) }
44
+ else
45
+ wrapped[collection_name]
46
+ end
47
+ instance.instance_variable_set("@#{collection_name}", collection_value)
39
48
  end
40
49
 
41
- def attr_collection(name, **options)
42
- unless name.is_a?(Symbol) || name.is_a?(String)
43
- raise ArgumentError.new("collection's name must be a Symbol or a String")
44
- end
45
- representer_name = \
46
- options[:representer_name] ? options[:representer_name] : guess_representrer_name(name.to_s)
47
- raise ArgumentError.new("representer_name must be a String") unless representer_name.is_a?(String)
48
- begin
49
- representer = representer_name.constantize
50
- collections[name.to_sym] = representer
51
- rescue NameError => e
52
- collections[name.to_sym] = nil
53
- end
54
- class_eval do
55
- attr_reader name.to_sym
56
- end
50
+ attribute_names.each do |attribute_name|
51
+ instance.send("#{attribute_name}=", wrapped.send(attribute_name))
57
52
  end
58
53
 
59
- def collection_names
60
- collections.keys
54
+ instance
55
+ end
56
+
57
+ def self.attr_field(name, type = Type::Value.new, **options)
58
+ attribute(name, type, **options)
59
+ end
60
+
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")
61
64
  end
62
65
 
63
- def attribute_names
64
- attribute_types.keys - ["wrapped"]
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)
69
+
70
+ begin
71
+ representer = representer_name.constantize
72
+ collections[name.to_sym] = representer
73
+ rescue NameError => e
74
+ collections[name.to_sym] = nil
65
75
  end
66
76
 
67
- def guess_representrer_name(name)
68
- "#{name.singularize.camelize}Representer"
77
+ class_eval do
78
+ attr_reader name.to_sym
69
79
  end
70
80
  end
81
+
82
+ def self.collection_names
83
+ collections.keys
84
+ end
85
+
86
+ def self.attribute_names
87
+ attribute_types.keys - ["wrapped"]
88
+ end
89
+
90
+ def self.guess_representrer_name(name)
91
+ "#{name.singularize.camelize}Representer"
92
+ end
71
93
  end
72
94
  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.0
4
+ version: 0.2.1
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-04 00:00:00.000000000 Z
11
+ date: 2020-03-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler