ceres 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/lib/ceres/attribute.rb +32 -0
- data/lib/ceres/children.rb +98 -0
- data/lib/ceres/object.rb +1 -26
- data/lib/ceres/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7186d7c6414c74e3a2c9239b0868b3c07a41c58cc8dfb32c03898475f30323f5
|
4
|
+
data.tar.gz: de06c6bf33353964973abb38933661c71bc3eec020ebcea9cb330c0b00cbe14e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ecf2d1d9a093381410ab854cbfaadab765a78f8c6b0ff72e70aa4a09c56be7d74154090a5b82b0398f01e4f542b6adc656b43e8b0123de40db05c77d62bbf98
|
7
|
+
data.tar.gz: 54197fef42e255ccf6763f776155e5c87354a2e620f682ec5d7a78e2eccf2c86d4c2d7a61d672250b56105a8af60925822fc1644447085bdbd776d569e9f4069
|
data/Gemfile
CHANGED
data/lib/ceres/attribute.rb
CHANGED
@@ -3,6 +3,38 @@ require "ceres/writer"
|
|
3
3
|
require "ceres/inspector"
|
4
4
|
|
5
5
|
module Ceres
|
6
|
+
module AttributeModule
|
7
|
+
def self.included(base)
|
8
|
+
base.extend(Singleton)
|
9
|
+
end
|
10
|
+
|
11
|
+
module Singleton
|
12
|
+
def attribute(name, &block)
|
13
|
+
attribute = Ceres::Attribute.new(name, &block)
|
14
|
+
attribute.apply(self)
|
15
|
+
|
16
|
+
@attributes ||= []
|
17
|
+
@attributes << attribute
|
18
|
+
end
|
19
|
+
|
20
|
+
def attributes(all: true)
|
21
|
+
if all
|
22
|
+
self.ancestors.flat_map do |ancestor|
|
23
|
+
if ancestor.respond_to?(:attributes)
|
24
|
+
ancestor.attributes(all: false)
|
25
|
+
else
|
26
|
+
[]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
elsif defined?(@attributes)
|
30
|
+
@attributes
|
31
|
+
else
|
32
|
+
[]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
6
38
|
class Attribute
|
7
39
|
attr_reader :name
|
8
40
|
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require "ceres/attribute"
|
2
|
+
|
3
|
+
module Ceres
|
4
|
+
module Children
|
5
|
+
include Ceres::AttributeModule
|
6
|
+
include Enumerable
|
7
|
+
|
8
|
+
attribute :children do
|
9
|
+
reader { @children.values }
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(*args, &block)
|
13
|
+
super(*args, &block)
|
14
|
+
|
15
|
+
@children = {}
|
16
|
+
end
|
17
|
+
|
18
|
+
private def child_key_from_object(object)
|
19
|
+
object.name
|
20
|
+
end
|
21
|
+
|
22
|
+
private def child_value_from_object(object)
|
23
|
+
object
|
24
|
+
end
|
25
|
+
|
26
|
+
def add_child(child, replace: false)
|
27
|
+
key = child_key_from_object(child)
|
28
|
+
|
29
|
+
if replace || !@children.has_key?(key)
|
30
|
+
@children[key] = child_value_from_object(child)
|
31
|
+
else
|
32
|
+
message = "overwriting #{key.inspect} is not allowed"
|
33
|
+
message += "\n new value: #{child.inspect}"
|
34
|
+
message += "\n old value: #{@children[key].inspect}"
|
35
|
+
|
36
|
+
raise ArgumentError, message
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def remove_child(key, &block)
|
41
|
+
if result = @children.delete(key)
|
42
|
+
result
|
43
|
+
elsif block
|
44
|
+
block.call(key)
|
45
|
+
else
|
46
|
+
raise KeyError.new("key not found #{key.inspect}")
|
47
|
+
|
48
|
+
# TODO (ruby 2.6)
|
49
|
+
# raise KeyError.new("key not found #{key.inspect}", key: name, receiver: self)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def each_child(&block)
|
54
|
+
@children.each_value(&block)
|
55
|
+
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
59
|
+
def select_children(&block)
|
60
|
+
@children.values.select(&block)
|
61
|
+
end
|
62
|
+
|
63
|
+
def [](key)
|
64
|
+
@children[key]
|
65
|
+
end
|
66
|
+
|
67
|
+
def has_key?(key)
|
68
|
+
@children.has_key?(key)
|
69
|
+
end
|
70
|
+
|
71
|
+
def fetch(key, *argv, &block)
|
72
|
+
if self.has_key?(key)
|
73
|
+
@children[key]
|
74
|
+
elsif argv.count > 0
|
75
|
+
argv[0]
|
76
|
+
elsif block
|
77
|
+
block.call(key)
|
78
|
+
else
|
79
|
+
raise KeyError.new("key not found #{key.inspect}")
|
80
|
+
|
81
|
+
# TODO (ruby 2.6)
|
82
|
+
# raise KeyError.new("key not found #{key.inspect}", key: name, receiver: self)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def fetch_values(*keys, &block)
|
87
|
+
keys.map { |k| self.fetch(k, &block) }
|
88
|
+
end
|
89
|
+
|
90
|
+
def dig(key, *argv, &block)
|
91
|
+
if self.has_key?(key)
|
92
|
+
object = self[key]
|
93
|
+
|
94
|
+
argv.count > 0 ? object.dig(*argv) : object
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
data/lib/ceres/object.rb
CHANGED
@@ -2,32 +2,7 @@ require "ceres/attribute"
|
|
2
2
|
|
3
3
|
module Ceres
|
4
4
|
class Object
|
5
|
-
|
6
|
-
klass.instance_exec { @attributes = [] }
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.attribute(name, &block)
|
10
|
-
attribute = Ceres::Attribute.new(name, &block)
|
11
|
-
attribute.apply(self)
|
12
|
-
|
13
|
-
@attributes << attribute
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.attributes(all: true)
|
17
|
-
if all
|
18
|
-
self.ancestors.flat_map do |ancestor|
|
19
|
-
if ancestor.respond_to?(:attributes)
|
20
|
-
ancestor.attributes(all: false)
|
21
|
-
else
|
22
|
-
[]
|
23
|
-
end
|
24
|
-
end
|
25
|
-
elsif defined?(@attributes)
|
26
|
-
@attributes
|
27
|
-
else
|
28
|
-
[]
|
29
|
-
end
|
30
|
-
end
|
5
|
+
include Ceres::AttributeModule
|
31
6
|
|
32
7
|
def inspect
|
33
8
|
attributes = self.class.attributes.map do |attribute|
|
data/lib/ceres/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ceres
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aurora
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-11-29 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -30,6 +30,7 @@ files:
|
|
30
30
|
- ceres.gemspec
|
31
31
|
- lib/ceres.rb
|
32
32
|
- lib/ceres/attribute.rb
|
33
|
+
- lib/ceres/children.rb
|
33
34
|
- lib/ceres/inspector.rb
|
34
35
|
- lib/ceres/object.rb
|
35
36
|
- lib/ceres/reader.rb
|
@@ -55,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
56
|
version: '0'
|
56
57
|
requirements: []
|
57
58
|
rubyforge_project:
|
58
|
-
rubygems_version: 2.7.
|
59
|
+
rubygems_version: 2.7.7
|
59
60
|
signing_key:
|
60
61
|
specification_version: 4
|
61
62
|
summary: A collection of useful things
|