phlexi-field 0.0.6 → 0.0.8

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: 5a93196112cf2988e3893ae65133271d65cfe8a3c118c274dd7170bd30735a67
4
- data.tar.gz: 9f8c007cbbe91c52d856485a30ad0db0f72a5d687e62c2202a4c1e6703a5bfd9
3
+ metadata.gz: 718d6322b8dd57d2ded2366a0d00ae5191305e98c178459a45660f9badcc73c6
4
+ data.tar.gz: cc8e6173c724aa41c00b102342d73b47029c6915d2226b3f0849a3262e929072
5
5
  SHA512:
6
- metadata.gz: 1937ee5a4b0d478a67a38b0d098e305ffcafb27556957dfca8bcc1b827688feabfc29fffd0bb7f91159b0519b017cb4da8fa2bb3e62c3651f53d5cf58ef6ca76
7
- data.tar.gz: 1b43ffa71ec09cf7a8c959b564485ed9d0486e031a4627c7c12f4b611d063ec19115128e9210e3e43981a887e71d11544169cd24395b793d990c13b0856329a2
6
+ metadata.gz: '0962dfaf9ffce3f6319becd919e3ceda58be30922341cf363803e0dec24a528bea3fa5526be4adb2a696e2f1a3cddc1d2824a1041dd17aa0232d33145761d413'
7
+ data.tar.gz: c1c1783cba57aa86d069e40e7637771d512fb3a04ccb0df19e6b1b8bb84b3287c58ae7cb3d66f7b0ac914e43bba27b659429c99db7c8f7113ad60120a1d39860
@@ -31,8 +31,8 @@ module Phlexi
31
31
  yield self if block_given?
32
32
  end
33
33
 
34
- def field(key, **attributes)
35
- create_child(key, attributes.delete(:builder_klass) || builder_klass, object: object, **attributes).tap do |field|
34
+ def field(key, template: false, **attributes)
35
+ create_child(key, attributes.delete(:builder_klass) || builder_klass, object: object, template:, **attributes).tap do |field|
36
36
  yield field if block_given?
37
37
  end
38
38
  end
@@ -51,9 +51,10 @@ module Phlexi
51
51
  # end
52
52
  # end
53
53
  # ```
54
- def nest_one(key, object: nil, &)
54
+ def nest_one(key, object: nil, as: nil, template: false, &)
55
55
  object ||= object_value_for(key: key)
56
- create_child(key, self.class, object:, builder_klass:, &)
56
+ key = as || key
57
+ create_child(key, self.class, object:, template:, builder_klass:, &)
57
58
  end
58
59
 
59
60
  # Wraps an array of objects in Namespace classes. For example, if `User#addresses` returns
@@ -72,9 +73,10 @@ module Phlexi
72
73
  # ```
73
74
  # The object within the block is a `Namespace` object that maps each object within the enumerable
74
75
  # to another `Namespace` or `Field`.
75
- def nest_many(key, collection: nil, &)
76
+ def nest_many(key, collection: nil, as: nil, template: false, &)
76
77
  collection ||= Array(object_value_for(key: key))
77
- create_child(key, self.class::NamespaceCollection, collection:, &)
78
+ key = as || key
79
+ create_child(key, self.class::NamespaceCollection, collection:, template:, &)
78
80
  end
79
81
 
80
82
  # Iterates through the children of the current namespace, which could be `Namespace` or `Field`
@@ -109,8 +111,12 @@ module Phlexi
109
111
 
110
112
  # Checks if the child exists. If it does then it returns that. If it doesn't, it will
111
113
  # build the child.
112
- def create_child(key, child_class, **kwargs, &block)
113
- @children.fetch(key) { @children[key] = child_class.new(key, parent: self, **kwargs, &block) }
114
+ def create_child(key, child_class, template: false, **, &)
115
+ if template
116
+ child_class.new(key, parent: self, **, &)
117
+ else
118
+ @children.fetch(key) { @children[key] = child_class.new(key, parent: self, **, &) }
119
+ end
114
120
  end
115
121
  end
116
122
  end
@@ -16,6 +16,10 @@ module Phlexi
16
16
  each(&block)
17
17
  end
18
18
 
19
+ def object
20
+ @collection
21
+ end
22
+
19
23
  private
20
24
 
21
25
  def each(&)
@@ -26,8 +30,15 @@ module Phlexi
26
30
  #
27
31
  # @return [Array<Namespace>] An array of namespace objects.
28
32
  def namespaces
29
- @namespaces ||= @collection.map.with_index do |object, key|
30
- build_namespace(key, object: object)
33
+ @namespaces ||= case @collection
34
+ when Hash
35
+ @collection.map do |key, object|
36
+ build_namespace(key, object: object)
37
+ end
38
+ when Array
39
+ @collection.map.with_index do |object, key|
40
+ build_namespace(key, object: object)
41
+ end
31
42
  end
32
43
  end
33
44
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Phlexi
4
4
  module Field
5
- VERSION = "0.0.6"
5
+ VERSION = "0.0.8"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phlexi-field
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Froelich
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-25 00:00:00.000000000 Z
11
+ date: 2024-11-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: phlex