phlexi-field 0.0.7 → 0.0.9

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: b81988b45868d71c491619e29435eeae401a9ce46d7e1f2e646d5fd2a0adc8af
4
- data.tar.gz: 63c2472d49cb30609fabf6190c26e25801de083b9fece0918aac83bf761cb840
3
+ metadata.gz: 250be1049bb60d0ec4f8a97ed44aa2bac660f68f1c2761b2122bed9fa3942046
4
+ data.tar.gz: a630eb20c75e78690931486ad0ca2fcf246d3855ebe0acc82990cb59d02be193
5
5
  SHA512:
6
- metadata.gz: 43654f76cfb84a8625b19a6147aa2bbb9bec4e01431be33f7ac6a27c9d288622d92a529214500ff412e08cb692e060d46443447d3066a7238a1a26a03a662052
7
- data.tar.gz: 9ace64b95a34ebb011ed0bb2b273238f1e33f58373a90bf0bd5bfbc62068fcc86c6fba722bebe2323be25b2bc8a73915ad1153da772f7c9ebf7bb73cc5d4f75b
6
+ metadata.gz: 22b15190bcab618d0c1389f421a29e552e2196b8fa8fb85bd7a86489e0a5a6866fb0df78d8b1bef381d418525d26a24eeebdece30ab46c11f85de8c18d42bd67
7
+ data.tar.gz: c80e965b3e329bae6a0b63bc0ffbbbd530bd017f73d5b7152672b301c6f2e33de0cab5dc5affa7ed52b04daaf136dccea9ea4bb0689ee7bc2edee7f1728d9e95
@@ -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,10 +51,10 @@ module Phlexi
51
51
  # end
52
52
  # end
53
53
  # ```
54
- def nest_one(key, object: nil, as: nil, &)
55
- object ||= object_value_for(key: key)
54
+ def nest_one(key, as: nil, object: nil, default: nil, template: false, &)
55
+ object ||= object_value_for(key: key) || default
56
56
  key = as || key
57
- create_child(key, self.class, object:, builder_klass:, &)
57
+ create_child(key, self.class, object:, template:, builder_klass:, &)
58
58
  end
59
59
 
60
60
  # Wraps an array of objects in Namespace classes. For example, if `User#addresses` returns
@@ -73,10 +73,10 @@ module Phlexi
73
73
  # ```
74
74
  # The object within the block is a `Namespace` object that maps each object within the enumerable
75
75
  # to another `Namespace` or `Field`.
76
- def nest_many(key, collection: nil, as: nil, &)
77
- collection ||= Array(object_value_for(key: key))
76
+ def nest_many(key, as: nil, collection: nil, default: nil, template: false, &)
77
+ collection ||= Array(object_value_for(key: key)).presence || default
78
78
  key = as || key
79
- create_child(key, self.class::NamespaceCollection, collection:, &)
79
+ create_child(key, self.class::NamespaceCollection, collection:, template:, &)
80
80
  end
81
81
 
82
82
  # Iterates through the children of the current namespace, which could be `Namespace` or `Field`
@@ -111,8 +111,12 @@ module Phlexi
111
111
 
112
112
  # Checks if the child exists. If it does then it returns that. If it doesn't, it will
113
113
  # build the child.
114
- def create_child(key, child_class, **kwargs, &block)
115
- @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
116
120
  end
117
121
  end
118
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.7"
5
+ VERSION = "0.0.9"
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.7
4
+ version: 0.0.9
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-11-03 00:00:00.000000000 Z
11
+ date: 2024-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: phlex