activeentity 0.0.1.beta18 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ed20caf9aace0c0f40b0ab08e03fa599e36a0833d55bd29748b93f2141c178a5
4
- data.tar.gz: '0068522aa324603aa8e26fd629d0e8ca574eb3187ef16100d146289a508bf4a1'
3
+ metadata.gz: 1a443ac8ce930488a0c53dab9543f9f0c7969c3692111451fecb633f462480f0
4
+ data.tar.gz: 99551ed1ea119f9d9643cb77d7ac7afc8aeea0d08cbaf7ec8a1b16e9c7a5a89e
5
5
  SHA512:
6
- metadata.gz: 715297effdacc19ba9b63aac1ff2164670578d5f284f55b2b86dc622676fee480ae601a5dedc2f8d5260e816c2e6105851c8c43a15500396721063f5faa7ba89
7
- data.tar.gz: c9bc14d8c43c524f21048c02203c600d0b67f7d203d77547057228319a91ac14e8632c414e65fa7f3626d3953cc7b63e21183214be49fe54764670c24e8f3018
6
+ metadata.gz: fa0b0819cf677b6972a951935d8f0aa7038dc5da8043ba369f8dbf19eaccf56e21ee2aea8352ab1ac9c1ccdb6f7cc2108f5fdd62edd7b47ddedb1059d52a174f
7
+ data.tar.gz: 12ae33174d9fdc60139f150d01462fd9d3c07b3a12b033e4e349e57f8081140965890c989279b98069aa9afab68730f9b80278e47e3fef648515633b578e77e6
@@ -27,6 +27,17 @@ module ActiveEntity
27
27
  include Mutex_m
28
28
  end
29
29
 
30
+ class << self
31
+ def dangerous_attribute_methods # :nodoc:
32
+ @dangerous_attribute_methods ||= (
33
+ Base.instance_methods +
34
+ Base.private_instance_methods -
35
+ Base.superclass.instance_methods -
36
+ Base.superclass.private_instance_methods
37
+ ).map { |m| -m.to_s }.to_set.freeze
38
+ end
39
+ end
40
+
30
41
  module ClassMethods
31
42
  def inherited(child_class) #:nodoc:
32
43
  child_class.initialize_generated_modules
@@ -96,7 +107,7 @@ module ActiveEntity
96
107
  # A method name is 'dangerous' if it is already (re)defined by Active Entity, but
97
108
  # not by any ancestors. (So 'puts' is not dangerous but 'save' is.)
98
109
  def dangerous_attribute_method?(name) # :nodoc:
99
- method_defined_within?(name, Base)
110
+ ::ActiveEntity::AttributeMethods.dangerous_attribute_methods.include?(name.to_s)
100
111
  end
101
112
 
102
113
  def method_defined_within?(name, klass, superklass = klass.superclass) # :nodoc:
@@ -148,25 +159,40 @@ module ActiveEntity
148
159
  # class Person < ActiveEntity::Base
149
160
  # end
150
161
  #
151
- # Person.has_attribute?('name') # => true
152
- # Person.has_attribute?(:age) # => true
153
- # Person.has_attribute?(:nothing) # => false
162
+ # Person.has_attribute?('name') # => true
163
+ # Person.has_attribute?('new_name') # => true
164
+ # Person.has_attribute?(:age) # => true
165
+ # Person.has_attribute?(:nothing) # => false
154
166
  def has_attribute?(attr_name)
155
- attribute_types.key?(attr_name.to_s)
167
+ attr_name = attr_name.to_s
168
+ attr_name = attribute_aliases[attr_name] || attr_name
169
+ attribute_types.key?(attr_name)
170
+ end
171
+
172
+ def _has_attribute?(attr_name) # :nodoc:
173
+ attribute_types.key?(attr_name)
156
174
  end
157
175
  end
158
176
 
159
177
  # Returns +true+ if the given attribute is in the attributes hash, otherwise +false+.
160
178
  #
161
179
  # class Person < ActiveEntity::Base
180
+ # alias_attribute :new_name, :name
162
181
  # end
163
182
  #
164
183
  # person = Person.new
165
- # person.has_attribute?(:name) # => true
166
- # person.has_attribute?('age') # => true
167
- # person.has_attribute?(:nothing) # => false
184
+ # person.has_attribute?(:name) # => true
185
+ # person.has_attribute?(:new_name) # => true
186
+ # person.has_attribute?('age') # => true
187
+ # person.has_attribute?(:nothing) # => false
168
188
  def has_attribute?(attr_name)
169
- @attributes.key?(attr_name.to_s)
189
+ attr_name = attr_name.to_s
190
+ attr_name = self.class.attribute_aliases[attr_name] || attr_name
191
+ @attributes.key?(attr_name)
192
+ end
193
+
194
+ def _has_attribute?(attr_name) # :nodoc:
195
+ @attributes.key?(attr_name)
170
196
  end
171
197
 
172
198
  # Returns an array of names for the attributes available on this object.
@@ -210,6 +236,7 @@ module ActiveEntity
210
236
  # person.attribute_for_inspect(:tag_ids)
211
237
  # # => "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]"
212
238
  def attribute_for_inspect(attr_name)
239
+ attr_name = attr_name.to_s
213
240
  value = _read_attribute(attr_name)
214
241
  format_for_inspect(value)
215
242
  end
@@ -229,8 +256,9 @@ module ActiveEntity
229
256
  # task.is_done = true
230
257
  # task.attribute_present?(:title) # => true
231
258
  # task.attribute_present?(:is_done) # => true
232
- def attribute_present?(attribute)
233
- value = _read_attribute(attribute)
259
+ def attribute_present?(attr_name)
260
+ attr_name = attr_name.to_s
261
+ value = _read_attribute(attr_name)
234
262
  !value.nil? && !(value.respond_to?(:empty?) && value.empty?)
235
263
  end
236
264
 
@@ -46,7 +46,7 @@ module ActiveEntity
46
46
  # task.read_attribute_before_type_cast('completed_on') # => "2012-10-21"
47
47
  # task.read_attribute_before_type_cast(:completed_on) # => "2012-10-21"
48
48
  def read_attribute_before_type_cast(attr_name)
49
- @attributes[attr_name.to_s].value_before_type_cast
49
+ attribute_before_type_cast(attr_name.to_s)
50
50
  end
51
51
 
52
52
  # Returns a hash of attributes before typecasting and deserialization.
@@ -66,12 +66,12 @@ module ActiveEntity
66
66
  private
67
67
 
68
68
  # Dispatch target for <tt>*_before_type_cast</tt> attribute methods.
69
- def attribute_before_type_cast(attribute_name)
70
- read_attribute_before_type_cast(attribute_name)
69
+ def attribute_before_type_cast(attr_name)
70
+ @attributes[attr_name].value_before_type_cast
71
71
  end
72
72
 
73
- def attribute_came_from_user?(attribute_name)
74
- @attributes[attribute_name].came_from_user?
73
+ def attribute_came_from_user?(attr_name)
74
+ @attributes[attr_name].came_from_user?
75
75
  end
76
76
  end
77
77
  end
@@ -16,29 +16,27 @@ module ActiveEntity
16
16
 
17
17
  # Returns the primary key column's value.
18
18
  def id
19
- primary_key = self.class.primary_key
20
- _read_attribute(primary_key) if primary_key
19
+ _read_attribute(@primary_key)
21
20
  end
22
21
 
23
22
  # Sets the primary key column's value.
24
23
  def id=(value)
25
- primary_key = self.class.primary_key
26
- _write_attribute(primary_key, value) if primary_key
24
+ _write_attribute(@primary_key, value)
27
25
  end
28
26
 
29
27
  # Queries the primary key column's value.
30
28
  def id?
31
- query_attribute(self.class.primary_key)
29
+ query_attribute(@primary_key)
32
30
  end
33
31
 
34
32
  # Returns the primary key column's value before type cast.
35
33
  def id_before_type_cast
36
- read_attribute_before_type_cast(self.class.primary_key)
34
+ read_attribute_before_type_cast(@primary_key)
37
35
  end
38
36
 
39
37
  # Returns the primary key column's previous value.
40
38
  def id_was
41
- attribute_was(self.class.primary_key)
39
+ attribute_was(@primary_key)
42
40
  end
43
41
 
44
42
  private
@@ -31,12 +31,8 @@ module ActiveEntity
31
31
  end
32
32
  end
33
33
 
34
- private
35
-
36
- # Dispatch target for <tt>*?</tt> attribute methods.
37
- def attribute?(attribute_name)
38
- query_attribute(attribute_name)
39
- end
34
+ alias :attribute? :query_attribute
35
+ private :attribute?
40
36
  end
41
37
  end
42
38
  end
@@ -8,17 +8,14 @@ module ActiveEntity
8
8
  module ClassMethods # :nodoc:
9
9
  private
10
10
 
11
- def define_method_attribute(name)
11
+ def define_method_attribute(name, owner:)
12
12
  ActiveModel::AttributeMethods::AttrNames.define_attribute_accessor_method(
13
- generated_attribute_methods, name
13
+ owner, name
14
14
  ) do |temp_method_name, attr_name_expr|
15
- generated_attribute_methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1
16
- # frozen_string_literal: true
17
- def #{temp_method_name}
18
- name = #{attr_name_expr}
19
- _read_attribute(name) { |n| missing_attribute(n, caller) }
20
- end
21
- RUBY
15
+ owner <<
16
+ "def #{temp_method_name}" <<
17
+ " _read_attribute(#{attr_name_expr}) { |n| missing_attribute(n, caller) }" <<
18
+ "end"
22
19
  end
23
20
  end
24
21
  end
@@ -30,13 +27,14 @@ module ActiveEntity
30
27
  name = attr_name.to_s
31
28
  name = self.class.attribute_aliases[name] || name
32
29
 
33
- _read_attribute(name, &block)
30
+ name = @primary_key if name == "id" && @primary_key
31
+ @attributes.fetch_value(name, &block)
34
32
  end
35
33
 
36
34
  # This method exists to avoid the expensive primary_key check internally, without
37
35
  # breaking compatibility with the read_attribute API
38
36
  def _read_attribute(attr_name, &block) # :nodoc
39
- @attributes.fetch_value(attr_name.to_s, &block)
37
+ @attributes.fetch_value(attr_name, &block)
40
38
  end
41
39
 
42
40
  alias :attribute :_read_attribute
@@ -12,17 +12,14 @@ module ActiveEntity
12
12
  module ClassMethods # :nodoc:
13
13
  private
14
14
 
15
- def define_method_attribute=(name)
15
+ def define_method_attribute=(name, owner:)
16
16
  ActiveModel::AttributeMethods::AttrNames.define_attribute_accessor_method(
17
- generated_attribute_methods, name, writer: true,
17
+ owner, name, writer: true,
18
18
  ) do |temp_method_name, attr_name_expr|
19
- generated_attribute_methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1
20
- # frozen_string_literal: true
21
- def #{temp_method_name}(value)
22
- name = #{attr_name_expr}
23
- _write_attribute(name, value)
24
- end
25
- RUBY
19
+ owner <<
20
+ "def #{temp_method_name}(value)" <<
21
+ " _write_attribute(#{attr_name_expr}, value)" <<
22
+ "end"
26
23
  end
27
24
  end
28
25
  end
@@ -34,26 +31,23 @@ module ActiveEntity
34
31
  name = attr_name.to_s
35
32
  name = self.class.attribute_aliases[name] || name
36
33
 
37
- _write_attribute(name, value)
34
+ name = @primary_key if name == "id" && @primary_key
35
+ @attributes.write_from_user(name, value)
38
36
  end
39
37
 
40
38
  # This method exists to avoid the expensive primary_key check internally, without
41
39
  # breaking compatibility with the write_attribute API
42
40
  def _write_attribute(attr_name, value) # :nodoc:
43
- @attributes.write_from_user(attr_name.to_s, value)
44
- value
41
+ @attributes.write_from_user(attr_name, value)
45
42
  end
46
43
 
44
+ alias :attribute= :_write_attribute
45
+ private :attribute=
46
+
47
47
  private
48
48
 
49
49
  def write_attribute_without_type_cast(attr_name, value)
50
- @attributes.write_cast_value(attr_name.to_s, value)
51
- value
52
- end
53
-
54
- # Dispatch target for <tt>*=</tt> attribute methods.
55
- def attribute=(attribute_name, value)
56
- _write_attribute(attribute_name, value)
50
+ @attributes.write_cast_value(attr_name, value)
57
51
  end
58
52
  end
59
53
  end
@@ -7,10 +7,10 @@ module ActiveEntity
7
7
  end
8
8
 
9
9
  module VERSION
10
- MAJOR = 0
11
- MINOR = 0
12
- TINY = 1
13
- PRE = "beta18"
10
+ MAJOR = 6
11
+ MINOR = 1
12
+ TINY = 0
13
+ PRE = nil
14
14
 
15
15
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
16
16
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeentity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.beta18
4
+ version: 6.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jasl
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-18 00:00:00.000000000 Z
11
+ date: 2021-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -135,7 +135,7 @@ homepage: https://github.com/jasl/activeentity
135
135
  licenses:
136
136
  - MIT
137
137
  metadata: {}
138
- post_install_message:
138
+ post_install_message:
139
139
  rdoc_options: []
140
140
  require_paths:
141
141
  - lib
@@ -146,12 +146,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
146
146
  version: 2.5.0
147
147
  required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  requirements:
149
- - - ">"
149
+ - - ">="
150
150
  - !ruby/object:Gem::Version
151
- version: 1.3.1
151
+ version: '0'
152
152
  requirements: []
153
- rubygems_version: 3.1.2
154
- signing_key:
153
+ rubygems_version: 3.1.4
154
+ signing_key:
155
155
  specification_version: 4
156
156
  summary: Rails virtual model solution based on ActiveModel.
157
157
  test_files: []