koine-attributes 0.1.3 → 0.1.4

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
  SHA1:
3
- metadata.gz: 9467d0514737ba4503f2b9f3fa95eb308c14c1cf
4
- data.tar.gz: 9c3b0f397e5eef27e60f7274b1e9c3cc55964e1c
3
+ metadata.gz: 2bc29763169638e976dbce7e8a8fd34e64fa1661
4
+ data.tar.gz: 8c92de7bb50fc754426854fdafca7e74a8820105
5
5
  SHA512:
6
- metadata.gz: 707a937b70778bbb5bd9532ed1a6e46d850f0e4e48fe7cb725657fa6e85d46431c7df914004b08d59945f95e32eca0adfe5b1918c4270e23250b4795585324e2
7
- data.tar.gz: b7b90869b1e5a6caa2edd3fa2b381e01e8c57ce0e23878b4dd1f92fa67682432259519c5805bc59a82ba3c70d7614011c8c12a58b7bb4b20c25be706e68913c2
6
+ metadata.gz: 9142060ec750bffb182c31d71cde2983bf4507f4867f6dfd72491c165b15ac78d3ef15e7f1b9abb581717d3542a9ca422a378009d3ff224b3952f5da0a16506f
7
+ data.tar.gz: e37af654cd64875ab1b39b2197de56aff89870da607403df12e7676291b43a5ef1c4cafb53d8fb643bc9d7075f92cacb1bd6167e5c4b48e1084cc8e7283c15a2
data/README.md CHANGED
@@ -49,7 +49,12 @@ Or install it yourself as:
49
49
  person.birtday = '2001-02-31' # Date Object can also be given
50
50
 
51
51
  person.name # => 'John Doe'
52
- person.birtday # => #<Date 2001-02-31>
52
+ person.birthday # => #<Date 2001-02-31>
53
+
54
+ person.attributes.to_h # => {
55
+ # name: 'John Doe',
56
+ # birthday: #<Date 2001-02-31>
57
+ # }
53
58
  ```
54
59
 
55
60
  Options:
@@ -1,3 +1,5 @@
1
+ require 'time'
2
+
1
3
  module Koine
2
4
  module Attributes
3
5
  module Adapter
@@ -0,0 +1,18 @@
1
+ module Koine
2
+ module Attributes
3
+ class Attributes
4
+ def initialize(object, attributes:)
5
+ @object = object
6
+ @attributes = attributes
7
+ end
8
+
9
+ def to_h
10
+ {}.tap do |hash|
11
+ @attributes.each do |name|
12
+ hash[name.to_sym] = @object.send(name)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -13,6 +13,7 @@ module Koine
13
13
  @getter = GetterBuilder.new(attributes: attributes, target: target)
14
14
  @setter = SetterBuilder.new(attributes: attributes, target: target)
15
15
  @constructor = ConstructorBuilder.new(attributes: attributes, target: target)
16
+ @lazy_attributes = LazyAttributesBuilder.new(attributes: attributes, target: target)
16
17
  end
17
18
 
18
19
  def build(name, driver)
@@ -24,6 +25,10 @@ module Koine
24
25
  def build_constructor(strict: true, freeze: false)
25
26
  @constructor.build(strict: strict, freeze: freeze)
26
27
  end
28
+
29
+ def build_lazy_attributes
30
+ @lazy_attributes.build
31
+ end
27
32
  end
28
33
 
29
34
  class BaseBuilder
@@ -57,15 +62,32 @@ module Koine
57
62
  end
58
63
  end
59
64
 
65
+ class LazyAttributesBuilder < BaseBuilder
66
+ def build
67
+ valid_attributes = attributes
68
+
69
+ target.class_eval do
70
+ define_method :attributes do
71
+ @_koine_attributes ||= Koine::Attributes::Attributes.new(self, attributes: valid_attributes)
72
+ end
73
+ end
74
+ end
75
+ end
76
+
60
77
  class ConstructorBuilder < BaseBuilder
61
78
  def build(strict: true, freeze: false)
62
79
  valid_attributes = attributes
63
80
 
64
81
  target.class_eval do
65
- def initialize(args = {})
82
+ define_method :initialize do |args = {}|
83
+ @_koine_attributes ||= Koine::Attributes::Attributes.new(self, attributes: valid_attributes)
66
84
  initialize_attributes(args)
67
85
  end
68
86
 
87
+ def attributes
88
+ @_koine_attributes
89
+ end
90
+
69
91
  protected
70
92
 
71
93
  define_method(:initialize_attributes) do |constructor_args|
@@ -1,5 +1,5 @@
1
1
  module Koine
2
2
  module Attributes
3
- VERSION = "0.1.3"
3
+ VERSION = "0.1.4"
4
4
  end
5
5
  end
@@ -100,6 +100,8 @@ require 'koine/attributes/adapter/base'
100
100
  #
101
101
  module Koine
102
102
  module Attributes
103
+ autoload :Attributes, 'koine/attributes/attributes'
104
+
103
105
  module Adapter
104
106
  autoload :Boolean, 'koine/attributes/adapter/boolean'
105
107
  autoload :Date, 'koine/attributes/adapter/date'
@@ -126,6 +128,8 @@ module Koine
126
128
  initializer_options = {} unless initializer_options.is_a?(Hash)
127
129
 
128
130
  @builder.build_constructor(initializer_options)
131
+ else
132
+ @builder.build_lazy_attributes
129
133
  end
130
134
 
131
135
  @builder = nil
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: koine-attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Jacobus
@@ -106,6 +106,7 @@ files:
106
106
  - lib/koine/attributes/adapter/integer.rb
107
107
  - lib/koine/attributes/adapter/string.rb
108
108
  - lib/koine/attributes/adapter/time.rb
109
+ - lib/koine/attributes/attributes.rb
109
110
  - lib/koine/attributes/builder.rb
110
111
  - lib/koine/attributes/hash_helper.rb
111
112
  - lib/koine/attributes/version.rb
@@ -130,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
130
131
  version: '0'
131
132
  requirements: []
132
133
  rubyforge_project:
133
- rubygems_version: 2.6.11
134
+ rubygems_version: 2.5.2.1
134
135
  signing_key:
135
136
  specification_version: 4
136
137
  summary: Stronger setters and getters