valued 0.1.3 → 0.2.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 +4 -4
- data/Gemfile.lock +1 -1
- data/lib/valued.rb +16 -3
- data/lib/valued/mutable.rb +92 -0
- data/lib/valued/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f51821642a08c2513844a7c47456c28b6b7a547796574178b8da0c109ab82712
|
4
|
+
data.tar.gz: f54ccf4641b836351d2ee0d48b98f91284718786cd070596cb75768f43b552e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40d747b3afeaf897065822bf5346657405eaffe250147a8f2ae7ef24cbf1ad8fa172fcecc34b1d5af9d84fb8b8db27003aff43b43f37325bb0df35be42f226c0
|
7
|
+
data.tar.gz: 9d33834008135bfb88116bfa7d090613993a8f8645b715487a124bd61a13df974a82017616566e481df3727227e89b7d4c7f0d812f5dfbe49c81d314800447ad
|
data/Gemfile.lock
CHANGED
data/lib/valued.rb
CHANGED
@@ -2,8 +2,14 @@ require 'valued/version'
|
|
2
2
|
|
3
3
|
module Valued
|
4
4
|
module ClassMethods
|
5
|
-
def
|
6
|
-
attributes.
|
5
|
+
def self.normalized_attributes(attributes)
|
6
|
+
attributes.each_with_object(attributes) do |attr, result|
|
7
|
+
result << attr.to_s.chomp('?').to_sym if attr.to_s.end_with?('?')
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.define_method(attr, klass)
|
12
|
+
klass.class_eval do
|
7
13
|
if attr.to_s.end_with?('?')
|
8
14
|
define_method(attr) do
|
9
15
|
instance_variable_get("@#{attr.to_s.chomp('?')}")
|
@@ -12,7 +18,14 @@ module Valued
|
|
12
18
|
attr_reader attr
|
13
19
|
end
|
14
20
|
end
|
15
|
-
|
21
|
+
end
|
22
|
+
|
23
|
+
def attributes(*attributes)
|
24
|
+
attributes.each { |attr| Valued::ClassMethods.define_method(attr, self) }
|
25
|
+
define_method('_attributes') do
|
26
|
+
Valued::ClassMethods.normalized_attributes(attributes)
|
27
|
+
end
|
28
|
+
private :_attributes
|
16
29
|
end
|
17
30
|
end
|
18
31
|
|
@@ -0,0 +1,92 @@
|
|
1
|
+
module Valued
|
2
|
+
module Mutable
|
3
|
+
module ClassMethods
|
4
|
+
def self.normalized_attributes(attributes)
|
5
|
+
attributes.each_with_object(attributes) do |attr, result|
|
6
|
+
result << attr.to_s.chomp('?').to_sym if attr.to_s.end_with?('?')
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.define_method(attr, klass)
|
11
|
+
klass.class_eval do
|
12
|
+
if attr.to_s.end_with?('?')
|
13
|
+
define_method(attr) do
|
14
|
+
instance_variable_get("@#{attr.to_s.chomp('?')}")
|
15
|
+
end
|
16
|
+
attr_writer "#{attr.to_s.chomp('?')}".to_sym
|
17
|
+
else
|
18
|
+
attr_accessor attr
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def attributes(*attributes)
|
24
|
+
attributes.each do |attr|
|
25
|
+
Valued::Mutable::ClassMethods.define_method(attr, self)
|
26
|
+
end
|
27
|
+
define_method('_attributes') do
|
28
|
+
Valued::Mutable::ClassMethods.normalized_attributes(attributes)
|
29
|
+
end
|
30
|
+
private :_attributes
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.define(*attrs, &block)
|
35
|
+
klass =
|
36
|
+
Class.new do
|
37
|
+
include Valued::Mutable
|
38
|
+
|
39
|
+
attributes(*attrs)
|
40
|
+
end
|
41
|
+
klass.class_eval(&block) if block_given?
|
42
|
+
klass
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.included(base)
|
46
|
+
base.extend(ClassMethods)
|
47
|
+
end
|
48
|
+
|
49
|
+
def initialize(attributes = {})
|
50
|
+
_attributes.each do |attribute|
|
51
|
+
if attributes.key?(attribute)
|
52
|
+
instance_variable_set(
|
53
|
+
"@#{attribute.to_s.chomp('?')}",
|
54
|
+
attributes.fetch(attribute)
|
55
|
+
)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def ==(other)
|
61
|
+
_attributes.all? do |attribute|
|
62
|
+
other.respond_to?(attribute) && send(attribute) == other.send(attribute)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def eql?(other)
|
67
|
+
self.class == other.class && self == other
|
68
|
+
end
|
69
|
+
|
70
|
+
def hash
|
71
|
+
(_attributes.map { |attribute| send(attribute) } + [self.class]).hash
|
72
|
+
end
|
73
|
+
|
74
|
+
def to_h
|
75
|
+
_attributes.each_with_object({}) do |attribute, hash|
|
76
|
+
hash[attribute] = send(attribute)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def to_s
|
81
|
+
inspect
|
82
|
+
end
|
83
|
+
|
84
|
+
def inspect
|
85
|
+
inspected_attributes =
|
86
|
+
_attributes.map do |attribute|
|
87
|
+
"#{attribute}=#{send(attribute).inspect}"
|
88
|
+
end.join(' ')
|
89
|
+
"#<#{self.class} #{inspected_attributes}>"
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
data/lib/valued/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: valued
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mario Mainz
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -97,6 +97,7 @@ files:
|
|
97
97
|
- README.md
|
98
98
|
- Rakefile
|
99
99
|
- lib/valued.rb
|
100
|
+
- lib/valued/mutable.rb
|
100
101
|
- lib/valued/version.rb
|
101
102
|
- valued.gemspec
|
102
103
|
homepage: https://github.com/mmainz/valued
|