fancystruct 0.9.2 → 0.9.3
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.
- data/VERSION +1 -1
- data/examples.rb +6 -0
- data/fancystruct.gemspec +1 -1
- data/lib/fancystruct.rb +33 -6
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.9.
|
1
|
+
0.9.3
|
data/examples.rb
CHANGED
@@ -66,4 +66,10 @@ end
|
|
66
66
|
eg "calling with a hash returns an instance of an anonymous FancyStruct" do
|
67
67
|
fs = FancyStruct :first_name => fn, :last_name => ln
|
68
68
|
check_fancystruct fs
|
69
|
+
end
|
70
|
+
|
71
|
+
eg "deep" do
|
72
|
+
fs = FancyStruct :first_name => fn, :last_name => ln,
|
73
|
+
:projects => [ { :name => 'Fancyviews' }, { :name => 'Fancydata' } ]
|
74
|
+
Check(fs.projects.first.name).is('Fancyviews')
|
69
75
|
end
|
data/fancystruct.gemspec
CHANGED
data/lib/fancystruct.rb
CHANGED
@@ -1,5 +1,36 @@
|
|
1
1
|
class FancyStruct
|
2
2
|
|
3
|
+
## class constuctors
|
4
|
+
|
5
|
+
def self.create(*attribs, &blk)
|
6
|
+
c = Class.new(FancyStruct)
|
7
|
+
c.attribs *attribs
|
8
|
+
c.class_eval(&blk) if blk
|
9
|
+
c
|
10
|
+
end
|
11
|
+
|
12
|
+
## object constructors
|
13
|
+
|
14
|
+
def self.obj(hsh)
|
15
|
+
keys, values = hsh.to_a.transpose
|
16
|
+
FancyStruct(*keys).new(*values)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.deep_obj(obj)
|
20
|
+
case obj
|
21
|
+
when Array
|
22
|
+
obj.map {|e| FancyStruct.deep_obj(e)}
|
23
|
+
when Hash
|
24
|
+
FancyStruct.obj( obj.each do |k,v|
|
25
|
+
obj[k] = FancyStruct.deep_obj(v)
|
26
|
+
end )
|
27
|
+
else
|
28
|
+
obj
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
## the class
|
33
|
+
|
3
34
|
def self.attribs(*attribs)
|
4
35
|
@attribs ||= []
|
5
36
|
unless attribs.empty?
|
@@ -30,12 +61,8 @@ end
|
|
30
61
|
|
31
62
|
def FancyStruct(*attribs, &blk)
|
32
63
|
if attribs.size == 1 && attribs.first.is_a?(Hash)
|
33
|
-
|
34
|
-
FancyStruct(*keys).new(*values)
|
64
|
+
FancyStruct.deep_obj attribs.first
|
35
65
|
else
|
36
|
-
|
37
|
-
c.attribs *attribs
|
38
|
-
c.class_eval(&blk) if blk
|
39
|
-
c
|
66
|
+
FancyStruct.create *attribs, &blk
|
40
67
|
end
|
41
68
|
end
|