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 CHANGED
@@ -1 +1 @@
1
- 0.9.2
1
+ 0.9.3
@@ -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
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{fancystruct}
8
- s.version = "0.9.2"
8
+ s.version = "0.9.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Myles Byrne"]
@@ -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
- keys, values = attribs.first.to_a.transpose
34
- FancyStruct(*keys).new(*values)
64
+ FancyStruct.deep_obj attribs.first
35
65
  else
36
- c = Class.new(FancyStruct)
37
- c.attribs *attribs
38
- c.class_eval(&blk) if blk
39
- c
66
+ FancyStruct.create *attribs, &blk
40
67
  end
41
68
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fancystruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Myles Byrne