u-attributes 0.7.0 → 0.8.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: 17e36e9053fd612a0fdec9ff0d9dd65ffa5b6b5c42cca0cff4855e3547171738
4
- data.tar.gz: 35f893fa0563d725d386ba98fb65506effaef632ce68a60e144a26671bbd979e
3
+ metadata.gz: e6458bbe7cd9f3f3ce857b69273aec70bf9697594c4f2ee0ca898c441d0d7f1f
4
+ data.tar.gz: f71576f85de2c6a03ea50401a6b2e791ed79ec393d061c5653e52076bcaee23a
5
5
  SHA512:
6
- metadata.gz: 69293580d1bde6527d6861cac123adbfe7ad4ace46dc57830a3e4dc980b8052f551c077a73851e1fae08c363915ae2b0544f5dd89ec11913f4ab2734c93b3e06
7
- data.tar.gz: 8f6c9176e8b2b2f6371a4ec2ccf3b8e7475832b16c1050ee908304e3f3e4502492818bfbc1faa8cd0ffcec8dd26a158a72063fbe8ee128a9ffc1d1db205d11e7
6
+ metadata.gz: c26e67d45e58f7b381e4b1a250a1d284eb05dc6455baae21cecff5ca54b5de864360c2d4f889ecab318d3b5d406ee39a1011608a1e3983dddf0106f5d849c10e
7
+ data.tar.gz: 9ff97e16e741127e9311ce0baf74deac64a24bc9d964e5f32f17d3a03e27fcbc32ded8efa3ce367caaec2032adc5327ea13bb341e7be3c8939f542283cd7725c
data/Gemfile CHANGED
@@ -2,5 +2,5 @@ source "https://rubygems.org"
2
2
 
3
3
  gem 'simplecov', require: false, group: :test
4
4
 
5
- # Specify your gem's dependencies in micro-attributes.gemspec
5
+ # Specify your gem's dependencies in u-attributes.gemspec
6
6
  gemspec
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- u-attributes (0.7.0)
4
+ u-attributes (0.8.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -80,6 +80,32 @@ person = Person.new('name' => 'John', age: 20)
80
80
 
81
81
  puts person.name # John
82
82
  puts person.age # 20
83
+
84
+ #--------------#
85
+ # #attribute() #
86
+ #--------------#
87
+ #
88
+ # Use the #attribute() method with a valid attribute name to get its value
89
+
90
+ puts person.attribute(:name) # John
91
+ puts person.attribute('age') # 20
92
+ puts person.attribute('foo') # nil
93
+
94
+ #
95
+ # If you pass a block, it will be executed only if the attribute is valid.
96
+
97
+ person.attribute(:name) { |value| puts value } # John
98
+ person.attribute('age') { |value| puts value } # 20
99
+ person.attribute('foo') { |value| puts value } # !! Nothing happened, because of the attribute not exists.
100
+
101
+ #--------------#
102
+ # #attribute() #
103
+ #--------------#
104
+ #
105
+ # Works like the #attribute() method, but will raise an exception when the attribute not exist.
106
+
107
+ puts person.attribute!('foo') # NameError (undefined attribute `foo)
108
+ person.attribute!('foo') { |value| puts value } # NameError (undefined attribute `foo)
83
109
  ```
84
110
 
85
111
  ### How to define multiple attributes?
@@ -249,7 +275,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
249
275
 
250
276
  ## Contributing
251
277
 
252
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/micro-attributes. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
278
+ Bug reports and pull requests are welcome on GitHub at https://github.com/serradura/u-attributes. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
253
279
 
254
280
  ## License
255
281
 
@@ -257,4 +283,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
257
283
 
258
284
  ## Code of Conduct
259
285
 
260
- Everyone interacting in the Micro::Attributes project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/micro-attributes/blob/master/CODE_OF_CONDUCT.md).
286
+ Everyone interacting in the Micro::Attributes project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/serradura/u-attributes/blob/master/CODE_OF_CONDUCT.md).
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Micro
4
4
  module Attributes
5
- module Utils
5
+ module AttributesUtils
6
6
  ARGUMENT_ERROR_MSG = 'argument must be a Hash'
7
7
 
8
8
  def self.hash_argument!(arg)
@@ -44,8 +44,8 @@ module Micro
44
44
 
45
45
  def attributes_data(arg)
46
46
  __attributes_data.merge(
47
- Utils.hash_argument!(arg)
48
- .each_with_object({}) { |(key, val), memo| memo[key.to_s] = val }
47
+ AttributesUtils.hash_argument!(arg)
48
+ .each_with_object({}) { |(key, val), memo| memo[key.to_s] = val }
49
49
  )
50
50
  end
51
51
 
@@ -59,8 +59,8 @@ module Micro
59
59
  raise ArgumentError, 'wrong number of arguments (given 0, expected 1 or more)'
60
60
  end
61
61
  end
62
-
63
62
  private_constant :ForSubclasses
64
63
  end
64
+ private_constant :Macros
65
65
  end
66
66
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Micro
4
+ module Attributes
5
+ module ToInitialize
6
+ def self.included(base)
7
+ base.send(:include, ::Micro::Attributes)
8
+ end
9
+
10
+ def initialize(arg)
11
+ self.attributes = arg
12
+ end
13
+
14
+ def with_attribute(key, val)
15
+ self.class.new(attributes.merge(key => val))
16
+ end
17
+
18
+ def with_attributes(arg)
19
+ self.class.new(attributes.merge(arg))
20
+ end
21
+ end
22
+
23
+ private_constant :ToInitialize
24
+ end
25
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Micro
4
4
  module Attributes
5
- VERSION = "0.7.0"
5
+ VERSION = "0.8.0"
6
6
  end
7
7
  end
@@ -1,13 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "micro/attributes/version"
4
- require "micro/attributes/utils"
4
+ require "micro/attributes/attributes_utils"
5
5
  require "micro/attributes/macros"
6
+ require "micro/attributes/to_initialize"
6
7
 
7
8
  module Micro
8
9
  module Attributes
9
10
  def self.included(base)
10
- base.extend Macros
11
+ base.extend(::Micro::Attributes.const_get(:Macros))
11
12
 
12
13
  base.class_eval do
13
14
  private_class_method :__attribute
@@ -22,41 +23,20 @@ module Micro
22
23
  subclass.attribute(value.nil? ? name : {name => value})
23
24
  end
24
25
 
25
- subclass.extend Macros.const_get(:ForSubclasses)
26
+ subclass.extend ::Micro::Attributes.const_get('Macros::ForSubclasses')
26
27
  end
27
28
  end
28
29
 
29
30
  def self.to_initialize
30
- @to_initialize ||= Module.new do
31
- def self.included(base)
32
- base.send(:include, Micro::Attributes)
33
-
34
- base.class_eval(<<-RUBY)
35
- def initialize(arg)
36
- self.attributes = arg
37
- end
38
-
39
- def with_attribute(key, val)
40
- self.class.new(attributes.merge(key => val))
41
- end
42
-
43
- def with_attributes(arg)
44
- self.class.new(attributes.merge(arg))
45
- end
46
- RUBY
47
- end
48
- end
49
- end
50
-
51
- def attribute?(name)
52
- self.class.attribute?(name)
31
+ @to_initialize ||= ::Micro::Attributes.const_get(:ToInitialize)
53
32
  end
54
33
 
55
34
  def attributes=(arg)
56
- self.class.attributes_data(Utils.hash_argument!(arg)).each do |name, value|
35
+ self.class.attributes_data(AttributesUtils.hash_argument!(arg)).each do |name, value|
57
36
  instance_variable_set("@#{name}", value) if attribute?(name)
58
37
  end
59
38
  end
39
+ protected :attributes=
60
40
 
61
41
  def attributes
62
42
  state = self.class.attributes.each_with_object({}) do |name, memo|
@@ -66,6 +46,22 @@ module Micro
66
46
  self.class.attributes_data(state)
67
47
  end
68
48
 
69
- protected :attributes=
49
+ def attribute?(name)
50
+ self.class.attribute?(name)
51
+ end
52
+
53
+ def attribute(name)
54
+ return unless attribute?(name)
55
+
56
+ value = public_send(name)
57
+
58
+ block_given? ? yield(value) : value
59
+ end
60
+
61
+ def attribute!(name, &block)
62
+ attribute(name) { |name| return block ? block[name] : name }
63
+
64
+ raise NameError, "undefined attribute `#{name}"
65
+ end
70
66
  end
71
67
  end
File without changes
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: u-attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura
@@ -57,11 +57,12 @@ files:
57
57
  - bin/console
58
58
  - bin/setup
59
59
  - lib/micro/attributes.rb
60
+ - lib/micro/attributes/attributes_utils.rb
60
61
  - lib/micro/attributes/macros.rb
61
- - lib/micro/attributes/utils.rb
62
+ - lib/micro/attributes/to_initialize.rb
62
63
  - lib/micro/attributes/version.rb
63
64
  - lib/u-attributes.rb
64
- - micro-attributes.gemspec
65
+ - u-attributes.gemspec
65
66
  homepage: https://github.com/serradura/u-attributes
66
67
  licenses:
67
68
  - MIT