u-attributes 0.8.0 → 0.9.0

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: e6458bbe7cd9f3f3ce857b69273aec70bf9697594c4f2ee0ca898c441d0d7f1f
4
- data.tar.gz: f71576f85de2c6a03ea50401a6b2e791ed79ec393d061c5653e52076bcaee23a
3
+ metadata.gz: 63911d6718fe30f139ee9acfb5b4a4577a71c3e01222087a17a4d0f349b31935
4
+ data.tar.gz: 8a3a4266c67d1e8eade0d742cf2d057c0bafb2f2e25c26c768f73cea4d40aa97
5
5
  SHA512:
6
- metadata.gz: c26e67d45e58f7b381e4b1a250a1d284eb05dc6455baae21cecff5ca54b5de864360c2d4f889ecab318d3b5d406ee39a1011608a1e3983dddf0106f5d849c10e
7
- data.tar.gz: 9ff97e16e741127e9311ce0baf74deac64a24bc9d964e5f32f17d3a03e27fcbc32ded8efa3ce367caaec2032adc5327ea13bb341e7be3c8939f542283cd7725c
6
+ metadata.gz: 927eb73d0b9cfce21e9d8abe11ab899296a6f45721d7821ea175e0bc58d144015ba22e488f1bdb6be563821a8b4e25a1082eba4c103ceedbed556ded654ab721
7
+ data.tar.gz: 85bf3683290458bfb863b215fe5eca4e57e041bb2dd167e1908a629c9081d9c8417584b847b9a775d46658f8069c5d960484a9c7a2be7ffb462eac1104f4b4ca
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- u-attributes (0.8.0)
4
+ u-attributes (0.9.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -68,7 +68,7 @@ puts person.age # 21
68
68
  class Person
69
69
  include Micro::Attributes
70
70
 
71
- attribute :name
71
+ attribute :name, 'John Doe' # .attribute() accepts a second arg as its default value
72
72
  attribute :age
73
73
 
74
74
  def initialize(options)
@@ -76,9 +76,9 @@ class Person
76
76
  end
77
77
  end
78
78
 
79
- person = Person.new('name' => 'John', age: 20)
79
+ person = Person.new(age: 20)
80
80
 
81
- puts person.name # John
81
+ puts person.name # John Doe
82
82
  puts person.age # 20
83
83
 
84
84
  #--------------#
@@ -87,14 +87,14 @@ puts person.age # 20
87
87
  #
88
88
  # Use the #attribute() method with a valid attribute name to get its value
89
89
 
90
- puts person.attribute(:name) # John
90
+ puts person.attribute(:name) # John Doe
91
91
  puts person.attribute('age') # 20
92
92
  puts person.attribute('foo') # nil
93
93
 
94
94
  #
95
95
  # If you pass a block, it will be executed only if the attribute is valid.
96
96
 
97
- person.attribute(:name) { |value| puts value } # John
97
+ person.attribute(:name) { |value| puts value } # John Doe
98
98
  person.attribute('age') { |value| puts value } # 20
99
99
  person.attribute('foo') { |value| puts value } # !! Nothing happened, because of the attribute not exists.
100
100
 
@@ -117,16 +117,16 @@ person.attribute!('foo') { |value| puts value } # NameError (undefined attribute
117
117
  class Person
118
118
  include Micro::Attributes
119
119
 
120
- attributes :name, :age
120
+ attributes :age, name: 'John Doe' # Use a hash to define attributes with default values
121
121
 
122
122
  def initialize(options)
123
123
  self.attributes = options
124
124
  end
125
125
  end
126
126
 
127
- person = Person.new('Serradura', 32)
127
+ person = Person.new(age: 32)
128
128
 
129
- puts person.name # Serradura
129
+ puts person.name # 'John Doe'
130
130
  puts person.age # 32
131
131
  ```
132
132
 
@@ -137,10 +137,7 @@ A: Use `Micro::Attributes.to_initialize`
137
137
  class Person
138
138
  include Micro::Attributes.to_initialize
139
139
 
140
- attributes :age, name: 'John Doe' # Use a hash to define a default value
141
-
142
- # attribute name: 'John Doe'
143
- # attribute :age
140
+ attributes :age, name: 'John Doe'
144
141
  end
145
142
 
146
143
  person = Person.new(age: 18)
@@ -200,7 +197,7 @@ puts instance.respond_to?(:foo) # true
200
197
  # The methods above allow redefining the attributes default data
201
198
 
202
199
  class AnotherSubclass < Person
203
- attribute! name: 'Alfa'
200
+ attribute! :name, 'Alfa'
204
201
  end
205
202
 
206
203
  alfa_person = AnotherSubclass.new({})
@@ -11,18 +11,13 @@ module Micro
11
11
  base.extend(::Micro::Attributes.const_get(:Macros))
12
12
 
13
13
  base.class_eval do
14
- private_class_method :__attribute
15
- private_class_method :__attributes
16
- private_class_method :__attribute_data
17
- private_class_method :__attribute_data!
18
- private_class_method :__attributes_data
14
+ private_class_method :__attributes_data, :__attributes
15
+ private_class_method :__attributes_def, :__attributes_set
16
+ private_class_method :__attribute_reader, :__attribute_set
19
17
  end
20
18
 
21
19
  def base.inherited(subclass)
22
- self.attributes_data({}).each do |name, value|
23
- subclass.attribute(value.nil? ? name : {name => value})
24
- end
25
-
20
+ subclass.attributes(self.attributes_data({}))
26
21
  subclass.extend ::Micro::Attributes.const_get('Macros::ForSubclasses')
27
22
  end
28
23
  end
@@ -10,6 +10,12 @@ module Micro
10
10
 
11
11
  raise ArgumentError, ARGUMENT_ERROR_MSG
12
12
  end
13
+
14
+ def self.stringify_hash_keys!(arg)
15
+ hash_argument!(arg).each_with_object({}) do |(key, val), memo|
16
+ memo[key.to_s] = val
17
+ end
18
+ end
13
19
  end
14
20
  end
15
21
  end
@@ -3,59 +3,59 @@
3
3
  module Micro
4
4
  module Attributes
5
5
  module Macros
6
- def __attributes
7
- @__attributes ||= Set.new
6
+ def __attributes_data
7
+ @__attributes_data ||= {}
8
8
  end
9
9
 
10
- def attribute?(name)
11
- __attributes.member?(name.to_s)
10
+ def __attributes
11
+ @__attributes ||= Set.new
12
12
  end
13
13
 
14
- def __attribute(name)
14
+ def __attribute_reader(name)
15
15
  __attributes.add(name)
16
16
  attr_reader(name)
17
17
  end
18
18
 
19
- def __attributes_data
20
- @__attributes_data ||= {}
19
+ def __attribute_set(key, value, can_overwrite)
20
+ name = key.to_s
21
+ has_attribute = attribute?(name)
22
+ __attribute_reader(name) unless has_attribute
23
+ __attributes_data[name] = value if can_overwrite || !has_attribute
21
24
  end
22
25
 
23
- def __attribute_data(name, value, allow_to_override)
24
- has_attribute = attribute?(name)
25
- __attribute(name) unless has_attribute
26
- __attributes_data[name] = value if allow_to_override || !has_attribute
26
+ def __attributes_def(arg, can_overwrite)
27
+ return __attribute_set(arg, nil, can_overwrite) unless arg.is_a?(::Hash)
28
+ arg.each { |key, val| __attribute_set(key, val, can_overwrite) }
27
29
  end
28
30
 
29
- def __attribute_data!(arg, allow_to_override:)
30
- return __attribute_data(arg.to_s, nil, allow_to_override) unless arg.is_a?(Hash)
31
+ def __attributes_set(args, can_overwrite)
32
+ args.flatten.each { |arg| __attributes_def(arg, can_overwrite) }
33
+ end
31
34
 
32
- arg.each { |key, value| __attribute_data(key.to_s, value, allow_to_override) }
35
+ def attribute?(name)
36
+ __attributes.member?(name.to_s)
33
37
  end
34
38
 
35
- def attribute(arg)
36
- __attribute_data!(arg, allow_to_override: false)
39
+ def attribute(name, value=nil)
40
+ __attribute_set(name, value, false)
37
41
  end
38
42
 
39
43
  def attributes(*args)
40
44
  return __attributes.to_a if args.empty?
41
-
42
- args.flatten.each { |arg| attribute(arg) }
45
+ __attributes_set(args, can_overwrite: false)
43
46
  end
44
47
 
45
48
  def attributes_data(arg)
46
- __attributes_data.merge(
47
- AttributesUtils.hash_argument!(arg)
48
- .each_with_object({}) { |(key, val), memo| memo[key.to_s] = val }
49
- )
49
+ __attributes_data.merge(AttributesUtils.stringify_hash_keys!(arg))
50
50
  end
51
51
 
52
52
  module ForSubclasses
53
- def attribute!(arg)
54
- __attribute_data!(arg, allow_to_override: true)
53
+ def attribute!(name, value=nil)
54
+ __attribute_set(name, value, true)
55
55
  end
56
56
 
57
57
  def attributes!(*args)
58
- return args.flatten.each { |arg| attribute!(arg) } unless args.empty?
58
+ return __attributes_set(args, can_overwrite: true) unless args.empty?
59
59
  raise ArgumentError, 'wrong number of arguments (given 0, expected 1 or more)'
60
60
  end
61
61
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Micro
4
4
  module Attributes
5
- VERSION = "0.8.0"
5
+ VERSION = "0.9.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: u-attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-07 00:00:00.000000000 Z
11
+ date: 2019-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake