@@ -53,157 +53,51 @@ module Hanami
5353 #
5454 # @see Hanami::Repository
5555 class Entity < ROM ::Struct
56- def hash
57- [ self . class , id ] . hash
58- end
59-
60- def ==( other )
61- self . class . to_s == other . class . to_s && id == other . id
62- end
63- end
64-
65- class OldEntity < Dry ::Struct
66- require "hanami/entity/strict"
67- require "hanami/entity/schemaless"
68-
69- DEFAULT = schema . dup . freeze
70-
71- # Syntactic shortcut to reference types in custom schema DSL
56+ # Note: This is keeping with the previous "Schemaless" interface that we had.
57+ # def self.load(attributes = {})
58+ # return attributes if attributes.is_a?(self)
7259 #
73- # @since 0.7.0
74- module Types
75- include Hanami ::Model ::Types
76- end
77-
78- def self . inherited ( entity )
79- super
60+ # super(Utils::Hash.deep_symbolize(attributes.to_hash)).freeze
61+ # end
8062
81- schema_policy . call ( entity )
82- entity . class_eval do
83- @_mutex = Mutex . new
84- end
85- end
86-
87- def self . new ( attributes = default_attributes , safe = false )
88- return if attributes . nil?
89-
90- super ( Utils ::Hash . deep_symbolize ( attributes . to_hash ) , safe ) . freeze
91- rescue Dry ::Struct ::Error => exception
92- raise Hanami ::Model ::Error . new ( exception . message )
93- end
94-
95- def self . []( type )
96- case type
97- when :struct
98- Schemaless
99- when :strict
100- Strict
101- else
102- raise Hanami ::Model ::Error . new ( "Unknown schema type: `#{ type . inspect } '" )
103- end
104- end
105-
106- def self . schema = ( attrs )
107- return if schema?
108-
109- attrs . each do |name , type |
110- attribute ( name , type )
111- end
112- end
113-
114- def self . schema?
115- @_mutex . synchronize do
116- defined? ( @_schema )
117- end
118- end
119-
120- def self . schema_policy
121- lambda do |entity |
122- entity . transform_types ( &:omittable )
123- end
124- end
63+ # class << self
64+ # alias new load
65+ # alias call load
66+ # alias call_unsafe load
67+ # end
12568
126- def self . attribute ( name , type = Undefined , &blk )
127- @_mutex . synchronize do
128- @_schema = true
129- end
130-
131- super ( name , type , &blk )
132- end
133-
134- # Entity ID
135- #
136- # @return [Object,NilClass] the ID, if present
137- #
138- # @since 0.7.0
13969 def id
140- attributes . fetch ( :id , nil )
70+ attributes . fetch ( :id ) { nil }
14171 end
14272
143- # Implement generic equality for entities
144- #
145- # Two entities are equal if they are instances of the same class and they
146- # have the same id.
147- #
148- # @param other [Object] the object of comparison
149- #
150- # @return [FalseClass,TrueClass] the result of the check
151- #
152- # @since 0.1.0
153- def ==( other )
154- self . class == other . class &&
155- id == other . id
156- end
157-
158- # Implement predictable hashing for hash equality
159- #
160- # @return [Integer] the object hash
161- #
162- # @since 0.7.0
16373 def hash
16474 [ self . class , id ] . hash
16575 end
16676
167- # Freeze the entity
168- #
169- # @since 0.7.0
170- def freeze
171- attributes . freeze
172- super
77+ def ==( other )
78+ self . class . to_s == other . class . to_s && id == other . id
17379 end
17480
175- # Serialize entity to a Hash
176- #
177- # @return [Hash] the result of serialization
178- #
179- # @since 0.1.0
180- def to_h
181- Utils ::Hash . deep_dup ( attributes )
182- end
81+ # def to_h
82+ # Utils::Hash.deep_dup(attributes)
83+ # end
84+ # alias to_hash to_h
18385
184- # @since 0.7.0
185- alias to_hash to_h
86+ # def inspect
87+ # "#<#{self.class.name} #{attributes.map { |k, v| "#{k}=#{v.inspect}" }.join(' ')}>"
88+ # end
89+ # alias to_s inspect
18690
187- protected
91+ def method_missing ( method_name , *args )
92+ # return attributes[method_name] if args.empty? && attributes.key?(method_name)
18893
189- # Check if the attribute is allowed to be read
190- #
191- # @since 0.7.0
192- # @api private
193- def attribute? ( name )
194- self . class . has_attribute? ( name )
94+ super
95+ rescue => exception
96+ raise Hanami ::Model ::Error . for ( exception )
19597 end
19698
197- private
198-
199- # @since 0.1.0
200- # @api private
201- attr_reader :attributes
202-
203- # @since 0.7.0
204- # @api private
205- def respond_to_missing? ( name , _include_all )
206- attribute? ( name )
207- end
99+ # def respond_to_missing?(method_name, include_all)
100+ # super || attributes.key?(method_name)
101+ # end
208102 end
209103end
0 commit comments