static-struct 0.1.1 → 0.1.2

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
  SHA1:
3
- metadata.gz: 58615a4e4020074ad25736704b1735dcf4c2ff03
4
- data.tar.gz: 637824c12247deda96a58c9422e7826369e1eb82
3
+ metadata.gz: 9fd1749e7ef593ea94f057e94a777de38336ffd2
4
+ data.tar.gz: 693813979b021137ca0f96e6bdec419d2e8872d4
5
5
  SHA512:
6
- metadata.gz: 556ad7e756ab9dc8c1ce2506cfc767bf23ba47eeb55cab17358627fa7c2a6115a7dd50f9c918b63714f1f2de354251d009ab0a5a84b55561171647a7b6d4321e
7
- data.tar.gz: 8cd846995eb3ca8f3b75aa1b258bcbc9027b311096a94e1d47059fb561e26a5a48eb3b4600bde20edd84409b8f96014dd4db70c0511157c6c1414591992ca44a
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
- Concert Ruby hashes (or hash-like objects) into Ruby objects.
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 inspect
11
- methods = static_methods.sort.each_with_object({}) do |method, result|
12
- result[method] = public_send(method)
13
- end
14
- "#<#{self.class} #{methods}>"
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
- inspect == other.inspect
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.inspect}"
50
+ fail MethodAlreadyDefinedError, "`#{method}' is already defined for #{object}"
32
51
  end
33
52
 
34
- object.static_methods.push(method.to_sym)
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
@@ -1,3 +1,3 @@
1
1
  module StaticStruct
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: static-struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mezuka LLC