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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8064ab3ebc35af445e5be33a322db6d14a31693e
4
- data.tar.gz: f8d1a850531925e12fe0c473647dc519538072f0
3
+ metadata.gz: a37be40de19ddfe65927cd3bef957db71979360d
4
+ data.tar.gz: 1e724f9d02e10c4fc59353af4f5e6e9121d79e7f
5
5
  SHA512:
6
- metadata.gz: 3d62b706cf2bffafb74bd09ead748382b95e1a4d297c6976832968065f5ecc7c9a6ca65b9daa7baed14f741c46719891ceaa23fcd7fdc921b8a73b0344e90fea
7
- data.tar.gz: ad312292e8c75438467baa6754658f26e101040dd9ee1121dc6a4ddf349975c09d72f5ebe72e91566ddc384a33a8e3bd6fe835d982373de8dce817ddc9a8c506
6
+ metadata.gz: 0302621a9b95b236a61c578533de6db60386a3d4e96445b1a6af8291fe0aa2658cf37895acf9429f2b50221922c5c4e32ed3f8c2f3bf95a9872aa13235d65f0b
7
+ data.tar.gz: 8bf489c4861b644fb9b00f0a64ecb797b86967a32a8a6f2121f26bed3a5b39ad588583af41e6ef1faafbfb30c18893caa3ac445ff1e58bd449842d0e3d21868b
data/examples/customer.rb CHANGED
@@ -19,4 +19,4 @@ end
19
19
 
20
20
  customer = Customer << { id: "person1", type: :person, firstName: "John", lastName: "Smith" }
21
21
 
22
- puts customer.to_h
22
+ puts customer.to_h
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mimi
4
- class Struct
5
- VERSION = "1.0.0"
6
- end
4
+ Struct_VERSION = "1.1.0"
7
5
  end
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
- if params.key?(:default)
21
- o.respond_to?(params[:from]) || call_as_proc(params[:default], o, params)
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
- require "mimi/struct"
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::Struct::VERSION
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.0.0
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-28 00:00:00.000000000 Z
11
+ date: 2019-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mimi-core