static-struct 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -1
- data/lib/static_struct/structure.rb +28 -9
- data/lib/static_struct/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9fd1749e7ef593ea94f057e94a777de38336ffd2
|
4
|
+
data.tar.gz: 693813979b021137ca0f96e6bdec419d2e8872d4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 413f7d0cca6d36a551e82b59b464cb954f7e8c2877d0bfc78c0db22fb10194491685763f0307ad46f3da0cad00312ff5bb9f3cf70583dc7c635d153e9db149b4
|
7
|
+
data.tar.gz: 22055a15e112a16bb5e2d904b93150965a047763b7fbd0993cf6048aec4fafdbd21d0105ae8ee0fad656fa8c6d139f3397e1406b6d106ac5bb449377ef9570dc
|
data/README.md
CHANGED
@@ -1,12 +1,15 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/mezuka/static_struct.svg?branch=master)](https://travis-ci.org/mezuka/static_struct)
|
2
|
+
|
1
3
|
# StaticStruct
|
2
4
|
|
3
|
-
|
5
|
+
Convert Ruby hashes (or hash-like objects) into Ruby objects.
|
4
6
|
|
5
7
|
Key features:
|
6
8
|
|
7
9
|
* Nesting hashes and respond to objects `to_hash` methods are allowed to do the convertation;
|
8
10
|
* There are no limitations of the nesting;
|
9
11
|
* It is not possible to call undefined methods;
|
12
|
+
* The defined dynamically structure is enumerable;
|
10
13
|
* The converted structure is *readonly*. It's not possible to rewrite defined values someway.
|
11
14
|
|
12
15
|
## Installation
|
@@ -1,21 +1,40 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
1
3
|
module StaticStruct
|
2
4
|
class Structure
|
5
|
+
include Enumerable
|
6
|
+
|
3
7
|
attr_reader :static_methods
|
4
8
|
|
5
9
|
def initialize(hash)
|
6
|
-
@static_methods =
|
10
|
+
@static_methods = SortedSet.new
|
7
11
|
define_structure(self, hash)
|
8
12
|
end
|
9
13
|
|
10
|
-
def
|
11
|
-
|
12
|
-
|
13
|
-
end
|
14
|
-
|
14
|
+
def to_s
|
15
|
+
joined_line = [self.class, current_state].map(&:to_s).select do |str|
|
16
|
+
str.size > 0
|
17
|
+
end.join(' ')
|
18
|
+
|
19
|
+
"#<#{joined_line}>"
|
15
20
|
end
|
16
21
|
|
17
22
|
def ==(other)
|
18
|
-
|
23
|
+
current_state == other.current_state
|
24
|
+
end
|
25
|
+
|
26
|
+
def each
|
27
|
+
static_methods.each do |m|
|
28
|
+
yield m, public_send(m)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
def current_state
|
35
|
+
static_methods.map do |method|
|
36
|
+
"#{method} = #{public_send(method)}"
|
37
|
+
end.join(' ')
|
19
38
|
end
|
20
39
|
|
21
40
|
private
|
@@ -28,10 +47,10 @@ module StaticStruct
|
|
28
47
|
|
29
48
|
def safe_define_method(object, method, return_value)
|
30
49
|
if object.respond_to?(method)
|
31
|
-
fail MethodAlreadyDefinedError, "`#{method}' is already defined for #{object
|
50
|
+
fail MethodAlreadyDefinedError, "`#{method}' is already defined for #{object}"
|
32
51
|
end
|
33
52
|
|
34
|
-
object.static_methods.
|
53
|
+
object.static_methods.add(method.to_s)
|
35
54
|
case
|
36
55
|
when return_value.is_a?(Array)
|
37
56
|
object.define_singleton_method(method) do
|