phlexi-field 0.0.5 → 0.0.7

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: a2ff865f069b2361127071190b6f17cd58a553d9eadcb3084acce2e3cefbe6df
4
- data.tar.gz: 6af6e7c11737780a668ea39f86a6436540afa31e2f76a14d0201177e336ff649
3
+ metadata.gz: b81988b45868d71c491619e29435eeae401a9ce46d7e1f2e646d5fd2a0adc8af
4
+ data.tar.gz: 63c2472d49cb30609fabf6190c26e25801de083b9fece0918aac83bf761cb840
5
5
  SHA512:
6
- metadata.gz: 617357cb149cc34431f4615ecc041207565a00e3855400fe804bd18c162af9c4e7b182a49d73c8d01deb122a087969531883316e3b1571595d72a7dbab56e84e
7
- data.tar.gz: afbfa1cefd2b4a31898e9aa90636f642395fc4d33d57b58989d3aaa8977232ce9d4d1b91602f1f63b4375854f2961de82935b88a5b11c5291fe90a0b55336907
6
+ metadata.gz: 43654f76cfb84a8625b19a6147aa2bbb9bec4e01431be33f7ac6a27c9d288622d92a529214500ff412e08cb692e060d46443447d3066a7238a1a26a03a662052
7
+ data.tar.gz: 9ace64b95a34ebb011ed0bb2b273238f1e33f58373a90bf0bd5bfbc62068fcc86c6fba722bebe2323be25b2bc8a73915ad1153da772f7c9ebf7bb73cc5d4f75b
@@ -67,7 +67,7 @@ module Phlexi
67
67
  end
68
68
 
69
69
  def determine_value_from_object
70
- object.respond_to?(key) ? object.public_send(key) : nil
70
+ Phlexi::Field::Support::Value.from(object, key)
71
71
  end
72
72
  end
73
73
  end
@@ -46,17 +46,9 @@ module Phlexi
46
46
  return custom_type.type if custom_type&.type
47
47
  end
48
48
 
49
- # Check if object responds to the key
50
- if object.respond_to?(key)
51
- # Fallback to inferring type from the value
52
- return infer_field_type_from_value(object.send(key))
53
- end
54
-
55
- # Check if object is a has that contains key
56
- if object.respond_to?(:fetch)
57
- # Fallback to inferring type from the value
58
- return infer_field_type_from_value(object[key])
59
- end
49
+ # Fallback to inferring type from the value
50
+ value = Phlexi::Field::Support::Value.from(object, key)
51
+ return infer_field_type_from_value(value) unless value.nil?
60
52
 
61
53
  # Default to string if we can't determine the type
62
54
  :string
@@ -22,7 +22,11 @@ module Phlexi
22
22
  def id
23
23
  @id ||= begin
24
24
  root, *rest = lineage
25
- root_key = root.respond_to?(:dom_id) ? root.dom_id : root.key
25
+ root_key = if root.respond_to?(:dom_id)
26
+ root.dom_id
27
+ else
28
+ root.key
29
+ end
26
30
  rest.map(&:key).unshift(root_key).join("_")
27
31
  end
28
32
  end
@@ -22,10 +22,11 @@ module Phlexi
22
22
 
23
23
  attr_reader :builder_klass, :object
24
24
 
25
- def initialize(key, parent:, builder_klass:, object: nil)
25
+ def initialize(key, parent:, builder_klass:, object: nil, dom_id: nil)
26
26
  super(key, parent: parent)
27
27
  @builder_klass = builder_klass
28
28
  @object = object
29
+ @dom_id = dom_id
29
30
  @children = {}
30
31
  yield self if block_given?
31
32
  end
@@ -50,8 +51,9 @@ module Phlexi
50
51
  # end
51
52
  # end
52
53
  # ```
53
- def nest_one(key, object: nil, &)
54
+ def nest_one(key, object: nil, as: nil, &)
54
55
  object ||= object_value_for(key: key)
56
+ key = as || key
55
57
  create_child(key, self.class, object:, builder_klass:, &)
56
58
  end
57
59
 
@@ -71,8 +73,9 @@ module Phlexi
71
73
  # ```
72
74
  # The object within the block is a `Namespace` object that maps each object within the enumerable
73
75
  # to another `Namespace` or `Field`.
74
- def nest_many(key, collection: nil, &)
76
+ def nest_many(key, collection: nil, as: nil, &)
75
77
  collection ||= Array(object_value_for(key: key))
78
+ key = as || key
76
79
  create_child(key, self.class::NamespaceCollection, collection:, &)
77
80
  end
78
81
 
@@ -84,12 +87,12 @@ module Phlexi
84
87
 
85
88
  def dom_id
86
89
  @dom_id ||= begin
87
- id = if object.nil?
90
+ object_id = if object.nil?
88
91
  nil
89
92
  elsif (primary_key = Phlexi::Field.object_primary_key(object))
90
93
  primary_key&.to_s || :new
91
94
  end
92
- [key, id].compact.join("_").underscore
95
+ [key, object_id].compact.join("_").underscore
93
96
  end
94
97
  end
95
98
 
@@ -100,15 +103,8 @@ module Phlexi
100
103
 
101
104
  protected
102
105
 
103
- # Calls the corresponding method on the object for the `key` name, if it exists. For example
104
- # if the `key` is `email` on `User`, this method would call `User#email` if the method is
105
- # present.
106
- #
107
- # This method could be overwritten if the mapping between the `@object` and `key` name is not
108
- # a method call. For example, a `Hash` would be accessed via `user[:email]` instead of `user.send(:email)`
109
106
  def object_value_for(key:)
110
- return @object.send(key) if @object.respond_to?(key)
111
- @object[key] if @object.is_a?(Hash)
107
+ Phlexi::Field::Support::Value.from(@object, key)
112
108
  end
113
109
 
114
110
  private
@@ -20,7 +20,7 @@ module Phlexi
20
20
  end
21
21
 
22
22
  def inspect
23
- "<#{self.class.name} key=#{key.inspect} parent=#{parent.inspect} />"
23
+ "<#{self.class.name} key=#{key.inspect} object=#{object.inspect} parent=#{parent.inspect} />"
24
24
  end
25
25
  end
26
26
  end
@@ -0,0 +1,12 @@
1
+ module Phlexi
2
+ module Field
3
+ module Support
4
+ module Value
5
+ def self.from(object, key)
6
+ return object[key] if object.is_a?(Hash)
7
+ object.public_send(key) if object.respond_to?(key)
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Phlexi
4
4
  module Field
5
- VERSION = "0.0.5"
5
+ VERSION = "0.0.7"
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.5
4
+ version: 0.0.7
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-18 00:00:00.000000000 Z
11
+ date: 2024-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: phlex
@@ -216,6 +216,7 @@ files:
216
216
  - lib/phlexi/field/structure/namespace.rb
217
217
  - lib/phlexi/field/structure/namespace_collection.rb
218
218
  - lib/phlexi/field/structure/node.rb
219
+ - lib/phlexi/field/support/value.rb
219
220
  - lib/phlexi/field/theme.rb
220
221
  - lib/phlexi/field/version.rb
221
222
  - sig/phlexi/field.rbs