mimi-struct 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/examples/customer.rb +1 -1
- data/lib/mimi/struct/version.rb +1 -3
- data/lib/mimi/struct.rb +19 -16
- data/mimi-struct.gemspec +3 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a37be40de19ddfe65927cd3bef957db71979360d
|
4
|
+
data.tar.gz: 1e724f9d02e10c4fc59353af4f5e6e9121d79e7f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0302621a9b95b236a61c578533de6db60386a3d4e96445b1a6af8291fe0aa2658cf37895acf9429f2b50221922c5c4e32ed3f8c2f3bf95a9872aa13235d65f0b
|
7
|
+
data.tar.gz: 8bf489c4861b644fb9b00f0a64ecb797b86967a32a8a6f2121f26bed3a5b39ad588583af41e6ef1faafbfb30c18893caa3ac445ff1e58bd449842d0e3d21868b
|
data/examples/customer.rb
CHANGED
data/lib/mimi/struct/version.rb
CHANGED
data/lib/mimi/struct.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "mimi/core"
|
4
|
+
require "mimi/struct/version"
|
4
5
|
|
5
6
|
module Mimi
|
6
7
|
#
|
@@ -10,6 +11,8 @@ module Mimi
|
|
10
11
|
# are mapped from input data.
|
11
12
|
#
|
12
13
|
class Struct < Mimi::Core::Struct
|
14
|
+
VERSION = Mimi::Struct_VERSION
|
15
|
+
|
13
16
|
#
|
14
17
|
# Default attribute mapper
|
15
18
|
#
|
@@ -17,10 +20,16 @@ module Mimi
|
|
17
20
|
# Calculates a default value if the source attribute is not set.
|
18
21
|
#
|
19
22
|
DEFAULT_ATTRIBUTE_MAPPER = -> (o, params) do
|
20
|
-
|
21
|
-
|
22
|
-
else
|
23
|
+
source_attr_set = o.respond_to?(params[:from]) && !o.send(params[:from]).nil?
|
24
|
+
if source_attr_set || !params.key?(:default)
|
23
25
|
o.send(params[:from])
|
26
|
+
else
|
27
|
+
# source attr is not set AND there is :default
|
28
|
+
if params[:default].is_a?(Proc)
|
29
|
+
call_as_proc(params[:default], o, params) # default as proc
|
30
|
+
else
|
31
|
+
params[:default] # default as literal value
|
32
|
+
end
|
24
33
|
end
|
25
34
|
end
|
26
35
|
|
@@ -32,11 +41,15 @@ module Mimi
|
|
32
41
|
o.respond_to?(params[:from])
|
33
42
|
end
|
34
43
|
|
44
|
+
# General Mimi::Struct error
|
45
|
+
#
|
46
|
+
class Error < StandardError; end
|
47
|
+
|
35
48
|
# Creates a mapped Struct object from another object
|
36
49
|
#
|
37
50
|
# @param source [Hash,Object]
|
38
51
|
#
|
39
|
-
def initialize(source)
|
52
|
+
def initialize(source = {})
|
40
53
|
source = Mimi::Core::Struct.new(source) if source.is_a?(Hash)
|
41
54
|
attributes = self.class.transform_attributes(source)
|
42
55
|
super(attributes)
|
@@ -44,14 +57,6 @@ module Mimi
|
|
44
57
|
raise e.class, "Failed to construct #{self.class}: #{e}", e.backtrace
|
45
58
|
end
|
46
59
|
|
47
|
-
# Fetches an attribute with given name
|
48
|
-
#
|
49
|
-
# @param name [Symbol]
|
50
|
-
#
|
51
|
-
def [](name)
|
52
|
-
@attributes[name.to_sym]
|
53
|
-
end
|
54
|
-
|
55
60
|
# Presents this Struct as a Hash, deeply converting nested Structs
|
56
61
|
#
|
57
62
|
# @return [Hash]
|
@@ -84,7 +89,7 @@ module Mimi
|
|
84
89
|
#
|
85
90
|
def self.attribute(name, params = {})
|
86
91
|
name = name.to_sym
|
87
|
-
raise "Attribute '#{name}' is already declared" if attribute_definitions.key?(name)
|
92
|
+
raise ArgumentError, "Attribute '#{name}' is already declared" if attribute_definitions.key?(name)
|
88
93
|
defaults = group_params.reduce(:merge).merge(
|
89
94
|
from: name,
|
90
95
|
using: DEFAULT_ATTRIBUTE_MAPPER
|
@@ -192,7 +197,7 @@ module Mimi
|
|
192
197
|
end
|
193
198
|
raise "unexpected :using type: #{params[:using].class}"
|
194
199
|
rescue StandardError => e
|
195
|
-
raise "Failed to transform attribute :#{key} : #{e}"
|
200
|
+
raise Error, "Failed to transform attribute :#{key} : #{e}"
|
196
201
|
end
|
197
202
|
|
198
203
|
# Calls a lambda as a proc, not caring about the number of arguments
|
@@ -225,5 +230,3 @@ module Mimi
|
|
225
230
|
end
|
226
231
|
end # class Struct
|
227
232
|
end # module Mimi
|
228
|
-
|
229
|
-
require "mimi/struct/version"
|
data/mimi-struct.gemspec
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
lib = File.expand_path("lib", __dir__)
|
2
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
-
|
3
|
+
|
4
|
+
require "mimi/struct/version"
|
4
5
|
|
5
6
|
Gem::Specification.new do |spec|
|
6
7
|
spec.name = "mimi-struct"
|
7
|
-
spec.version = Mimi::
|
8
|
+
spec.version = Mimi::Struct_VERSION
|
8
9
|
spec.authors = ["Alex Kukushkin"]
|
9
10
|
spec.email = ["alex@kukushk.in"]
|
10
11
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mimi-struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Kukushkin
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-06-
|
11
|
+
date: 2019-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mimi-core
|