static_struct 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/static_struct.rb +70 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6768ae197cb8c203a3f6656d488acacc5e953e62
4
+ data.tar.gz: 442a882a531bb1e45bbb61614738a970d59d6462
5
+ SHA512:
6
+ metadata.gz: 8a17dda252ffbe28bd65db51555ba91db181181450be96ea59ce909b19fb1e4a6dba953637a044e215bd699a76676e78c073fcaa1fc2d916342c08df68a14aef
7
+ data.tar.gz: 3ca29f06dbbb9f2c52ae27aa67b49eecb69e75fecc0bf30f29700ac98905e6ff3842f388f3cba972df8ed3d55327ff1c043a1ddfcebf1e12ded3dfd66d052c98
@@ -0,0 +1,70 @@
1
+ class StaticStruct
2
+ def initialize(hash)
3
+ if !hash.is_a?(Hash)
4
+ raise "StaticStruct must be initialized with a hash."
5
+ end
6
+
7
+ @table = build(hash)
8
+ end
9
+
10
+ def method_missing(mid, *args)
11
+ value_of(mid)
12
+ end
13
+
14
+ def [](key)
15
+ value_of(key)
16
+ end
17
+
18
+ def value_of(key)
19
+ if !@table.has_key?(key.to_sym)
20
+ raise "Property #{key} was not found for StaticStruct##{object_id}."
21
+ end
22
+
23
+ @table[key.to_sym]
24
+ end
25
+
26
+ def ==(other)
27
+ return false unless(other.is_a?(StaticStruct))
28
+ return @table == other.table
29
+ end
30
+
31
+ def to_h
32
+ Hash[@table.map {|k, v| [k.to_sym, build_hash(v)]}]
33
+ end
34
+
35
+ def to_json(options = nil)
36
+ to_h.to_json
37
+ end
38
+
39
+ def as_json(options = nil)
40
+ to_h.as_json
41
+ end
42
+
43
+ private
44
+
45
+ def build(node)
46
+ case node when Hash
47
+ Hash[node.map {|k,v| [k.to_sym, build_object(v)]}]
48
+ when Array
49
+ node.map { |item| build_object(item) }
50
+ else
51
+ node
52
+ end
53
+ end
54
+
55
+ def build_object(node)
56
+ case node when Hash
57
+ StaticStruct.new(node)
58
+ else
59
+ build(node)
60
+ end
61
+ end
62
+
63
+ def build_hash(node)
64
+ case node when Array
65
+ node.map { |item| build_hash(item) }
66
+ else
67
+ node.methods.include?(:to_h) ? node.to_h : node
68
+ end
69
+ end
70
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: static_struct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Federico Poli
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-02-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This library allows the creation of static objects from a hash.
14
+ email:
15
+ - fapoli@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/static_struct.rb
21
+ homepage: http://github.com/fapoli/static-struct
22
+ licenses: []
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.0.6
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Static Struct
44
+ test_files: []