omnistruct 1.0.1 → 1.1.0

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: f2bca86c8dfacd6cc38fa06c599593f19ab4ad67
4
- data.tar.gz: 203817b49cdaf1f902cc88d98f451c0a545b856c
3
+ metadata.gz: 8671e686d090e2a235b8b1e198a7fc1d870e92b7
4
+ data.tar.gz: b382c62204754572c48efb03e0e17bc027583f81
5
5
  SHA512:
6
- metadata.gz: d91807ccee8ac5a7e9abf1b30c042795cfc75a9bce15fe8fbfebce09588984267723ea539795a16b956de9f2522148c936903b677fd5f83439a7a53dd818e7bb
7
- data.tar.gz: b8b4ff5b8c5c2086986451948d72bf6f3f18ddb79683d3b55b1fca4650dfcf894a2628d0627022a13e041e832e0ce9279626c52274f997bc6af7e2b96e5570e5
6
+ metadata.gz: 8763db20f4aefa27afa5ae7b50d01d12215a1fc11e5cd08b9a859a72d0cc40ef18a1ca9cfe4526a30af0ae999d689a14cb422fa2b24778a340d1a5bf149b0459
7
+ data.tar.gz: 015aa503072ea842f9e444456dcb826e787151ec088f22bcf235bc8112674bd52d9de42fcc592d64f79af35156d6e12b21de04a8b80a814fb9be712e1899b651
data/README.md CHANGED
@@ -45,6 +45,7 @@ Add's the following common `Hash` methods to struct types;
45
45
  struct = <struct type>.to_struct( [type] )
46
46
  merged = <struct type>.merge( Hash.new | <struct type> )
47
47
  json = <struct type>.to_json
48
+ value = <struct type>.delete(:key)
48
49
 
49
50
  # ClassyStruct
50
51
  locked = <classy struct>.lock
@@ -1,12 +1,13 @@
1
- require File.join(File.dirname(__FILE__), 'hash')
1
+ require File.join(File.dirname(__FILE__), 'common')
2
2
  require 'classy_struct'
3
- require 'json'
4
3
 
5
4
  # ClassyStruct patches
6
5
  ####
7
6
  class ClassyStruct
7
+ STRUCT_TYPE = :classy_struct
8
+ CLASS_NAME = "ClassyHashStruct"
9
+
8
10
  class ClassyStructClass
9
- alias :to_h :to_hash
10
11
 
11
12
  # Convert ClassyStruct to Struct or OpenStruct
12
13
  #
@@ -26,22 +27,21 @@ class ClassyStruct
26
27
  # s = struct.to_struct(:open_struct)
27
28
  # s.class
28
29
  # #=> OpenStruct
29
- def to_struct(type=:classy_struct)
30
- return self if type.to_sym == :classy_struct
31
- return self.to_h.to_struct(type)
30
+ include CommonStruct
31
+
32
+ def struct_type
33
+ STRUCT_TYPE
32
34
  end
33
35
 
34
36
  def merge other
35
- self.to_h.merge!(other.to_h).to_struct(:classy_struct)
37
+ self.to_h.merge!(other.to_h).to_struct(STRUCT_TYPE)
36
38
  end
37
39
 
38
40
  # Convert ClassyStruct to Struct, thus locking it.
39
41
  def lock
40
- self.to_h.to_struct(:struct)
42
+ self.to_h.to_struct(Struct::STRUCT_TYPE)
41
43
  end
42
44
 
43
- def to_json
44
- self.to_h.to_json
45
- end
45
+ alias :to_h :to_hash
46
46
  end
47
47
  end
@@ -0,0 +1,30 @@
1
+ require 'json'
2
+
3
+ module CommonStruct
4
+ DEFAULT_STRUCT_TYPE = :classy_struct
5
+
6
+ def struct_type
7
+ DEFAULT_STRUCT_TYPE
8
+ end
9
+
10
+ def to_struct(type=nil)
11
+ type ||= struct_type
12
+ return self if type.to_sym == struct_type
13
+ return self.to_h.to_struct(type)
14
+ end
15
+
16
+ def delete key
17
+ key = key.to_sym
18
+ val = send(key) rescue nil
19
+
20
+ unless val.nil?
21
+ send("#{key}=".to_sym, nil)
22
+ end
23
+
24
+ return val
25
+ end
26
+
27
+ def to_json
28
+ to_h.to_json
29
+ end
30
+ end
@@ -1,14 +1,14 @@
1
- require 'json'
1
+ require File.join(File.dirname(__FILE__), 'classystruct')
2
+ require File.join(File.dirname(__FILE__), 'openstruct')
3
+ require File.join(File.dirname(__FILE__), 'struct')
2
4
 
3
5
  # Supports converting Hashes in to different kinds of structs via a 'to_struct'
4
6
  # method.
5
7
  #
6
8
  # Note: ClassyStruct perferred over OpenStruct, it's faster.
7
9
  class Hash
8
- CLASSY_STRUCT_NAME = "ClassyHashStruct"
9
-
10
10
  attr_accessor :struct_type
11
- @@struct_type = :classy_struct
11
+ @@struct_type = ClassyStruct::STRUCT_TYPE
12
12
 
13
13
  # Convert Hash to Struct, OpenStruct or ClassyStruct
14
14
  #
@@ -33,19 +33,19 @@ class Hash
33
33
  def to_struct(type=nil)
34
34
  self.struct_type = type.nil? ? struct_type : type.to_sym
35
35
 
36
- if struct_type == :classy_struct
36
+ if struct_type == ClassyStruct::STRUCT_TYPE
37
37
  begin
38
- Object.send(:remove_const, CLASSY_STRUCT_NAME.to_sym)
38
+ Object.send(:remove_const, ClassyStruct::CLASS_NAME)
39
39
  rescue; end
40
40
 
41
- return Object.const_set(CLASSY_STRUCT_NAME, ClassyStruct.new).new(self)
41
+ return Object.const_set(ClassyStruct::CLASS_NAME, ClassyStruct.new).new(self)
42
42
  end
43
43
 
44
44
  self.each do |k,v|
45
45
  self[k] = v.to_struct(struct_type) if v.is_a? Hash
46
46
  end
47
47
 
48
- if struct_type == :open_struct
48
+ if struct_type == OpenStruct::STRUCT_TYPE
49
49
  # openstruct is said to be slower, so giving the option to disable
50
50
  return OpenStruct.new(self)
51
51
  end
@@ -100,6 +100,10 @@ class Hash
100
100
 
101
101
  protected
102
102
  def self.struct_types
103
- [ :classy_struct, :open_struct, :struct ]
103
+ [
104
+ ClassyStruct::STRUCT_TYPE,
105
+ OpenStruct::STRUCT_TYPE,
106
+ Struct::STRUCT_TYPE
107
+ ]
104
108
  end
105
109
  end
@@ -1,30 +1,8 @@
1
- require File.join(File.dirname(__FILE__), 'hash')
1
+ require File.join(File.dirname(__FILE__), 'common')
2
2
  require 'ostruct'
3
- require 'json'
4
3
 
5
4
  class OpenStruct
6
- # replaces object
7
- def merge! other
8
- self.marshal_load(merge(other).to_h)
9
- end
10
-
11
- # returns new object
12
- def merge other
13
- OpenStruct.new(self.marshal_dump.merge(other.to_h))
14
- end
15
-
16
- def to_json
17
- self.marshal_dump.to_json
18
- end
19
-
20
- # Convert OpenStruct to Struct, thus locking it.
21
- def lock
22
- self.marshal_dump.to_struct(:struct)
23
- end
24
-
25
- def unlock
26
- self # noop
27
- end
5
+ STRUCT_TYPE = :open_struct
28
6
 
29
7
  # Convert OpenStruct to Struct or ClassyStruct
30
8
  #
@@ -44,8 +22,31 @@ class OpenStruct
44
22
  # s = struct.to_struct(:classy_struct)
45
23
  # s.class
46
24
  # #=> ClassyHashStruct
47
- def to_struct(type=:open_struct)
48
- return self if type.to_sym == :open_struct
49
- return self.marshal_dump.to_struct(type)
25
+ include CommonStruct
26
+
27
+ def struct_type
28
+ STRUCT_TYPE
50
29
  end
30
+
31
+ # replaces object
32
+ def merge! other
33
+ self.marshal_load(merge(other).to_h)
34
+ end
35
+
36
+ # returns new object
37
+ def merge other
38
+ OpenStruct.new(self.to_h.merge(other.to_h))
39
+ end
40
+
41
+ # Convert OpenStruct to Struct, thus locking it.
42
+ def lock
43
+ self.to_h.to_struct(Struct::STRUCT_TYPE)
44
+ end
45
+
46
+ def unlock
47
+ self # noop
48
+ end
49
+
50
+ alias :to_h :marshal_dump
51
+ alias :to_hash :to_h
51
52
  end
@@ -1,28 +1,11 @@
1
1
  require File.join(File.dirname(__FILE__), 'hash')
2
- require 'json'
2
+ require File.join(File.dirname(__FILE__), 'common')
3
3
 
4
4
  class Struct
5
- # returns new object
6
- def merge other
7
- selfHash = self.to_h
8
- otherHash = other.is_a?(Hash) ? other : other.to_h
9
-
10
- selfHash.merge!(otherHash)
11
- selfHash.to_struct(:struct)
12
- end
13
-
14
- def lock
15
- self #noop
16
- end
17
-
18
- # Convert Struct to OpenStruct or ClassyStruct (default), thus unlocking it.
19
- def unlock(type=:classy_struct)
20
- return nil if type.to_sym == :struct
21
- return self.to_h.to_struct(type)
22
- end
5
+ STRUCT_TYPE = :struct
23
6
 
24
- def to_json
25
- self.to_h.to_json
7
+ def struct_type
8
+ STRUCT_TYPE
26
9
  end
27
10
 
28
11
  # Convert Struct # to OpenStruct or ClassyStruct
@@ -43,10 +26,24 @@ class Struct
43
26
  # s = struct.to_struct(:classy_struct)
44
27
  # s.class
45
28
  # #=> ClassyHashStruct
46
- def to_struct(type=:classy_struct)
47
- return self if type.to_sym == :struct
48
- return self.to_h.to_struct(type)
29
+ include CommonStruct
30
+
31
+ # returns new object
32
+ def merge other
33
+ selfHash = self.to_h
34
+ otherHash = other.is_a?(Hash) ? other : other.to_h
35
+
36
+ selfHash.merge!(otherHash)
37
+ selfHash.to_struct(STRUCT_TYPE)
49
38
  end
50
- end
51
39
 
40
+ def lock
41
+ self #noop
42
+ end
52
43
 
44
+ # Convert Struct to OpenStruct or ClassyStruct (default), thus unlocking it.
45
+ def unlock(type=ClassyStruct::STRUCT_TYPE)
46
+ return nil if type.to_sym == Struct::STRUCT_TYPE
47
+ return self.to_h.to_struct(type)
48
+ end
49
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omnistruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Mervine
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-12 00:00:00.000000000 Z
11
+ date: 2014-12-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: classy_struct
@@ -60,6 +60,7 @@ extra_rdoc_files: []
60
60
  files:
61
61
  - README.md
62
62
  - ext/classystruct.rb
63
+ - ext/common.rb
63
64
  - ext/hash.rb
64
65
  - ext/openstruct.rb
65
66
  - ext/struct.rb