klassy 0.0.1 → 0.0.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 +4 -4
- data/Gemfile +1 -0
- data/README.md +6 -5
- data/lib/klassy/klassic.rb +32 -14
- data/lib/klassy/version.rb +1 -1
- data/spec/klassy_spec.rb +7 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff888d87d4fd08f511a23222c408faa7b99b90ba
|
4
|
+
data.tar.gz: 8a4050708d3154dfcd2628ceb744b7c270e29857
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 570ce1e29591b37f525750b46217c9004e85464cd94c0091be99ffcd280a9b3712bf0e6add3defa95a34fb1b436e241297144800b96d87a53598ac38f43c869d
|
7
|
+
data.tar.gz: 0043dc3725f95ef8815e28583fadf980005bc54d5658aa4f7500f2791d57ddff8449d1a2447f13703878d2c66f62baa30833c066f6a827bfcea5eea0c9d020fe
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -36,11 +36,12 @@ Example:
|
|
36
36
|
|
37
37
|
results in:
|
38
38
|
```
|
39
|
-
#<Klassy::FirstClass @
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
39
|
+
#<Klassy::FirstClass:0x007fe25397e5b8 @name="bob", @second_class=[
|
40
|
+
#<Klassy::SecondClass:0x007fe25396bd50 @color="green", @answer=42,
|
41
|
+
@third_class=[
|
42
|
+
#<Klassy::ThirdClass:0x007fe2539690f0>
|
43
|
+
]>
|
44
|
+
]>
|
44
45
|
```
|
45
46
|
|
46
47
|
|
data/lib/klassy/klassic.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
1
3
|
module Klassy
|
2
4
|
class Klassic
|
3
5
|
|
@@ -15,25 +17,36 @@ module Klassy
|
|
15
17
|
private
|
16
18
|
|
17
19
|
def method_missing(name, *args, &block)
|
20
|
+
# find or create the class
|
21
|
+
klass = find_or_create_class(name)
|
22
|
+
|
18
23
|
# create the object
|
19
|
-
|
24
|
+
attribs = args.find{|x| x.class == Hash }
|
25
|
+
obj = create_object(klass, name, attribs)
|
20
26
|
|
21
27
|
# if there's a parent, make this object one of it's children
|
22
|
-
if parent
|
23
|
-
parent.children << obj
|
24
|
-
end
|
28
|
+
add_nested_object(name, obj) if parent
|
25
29
|
|
26
30
|
# if block_given? then we have more objects to create.
|
27
|
-
if block_given?
|
28
|
-
Klassic.klassify({parent: obj}, &block)
|
29
|
-
end
|
31
|
+
Klassic.klassify({parent: obj}, &block) if block_given?
|
30
32
|
|
31
33
|
obj
|
32
34
|
end
|
33
35
|
|
34
|
-
def
|
35
|
-
|
36
|
-
|
36
|
+
def add_nested_object(object_name, object)
|
37
|
+
parent.instance_eval do
|
38
|
+
if respond_to? object_name
|
39
|
+
eval(object_name.to_s) << object
|
40
|
+
else
|
41
|
+
instance_variable_set "@#{object_name}", [object]
|
42
|
+
self.class.class_eval do
|
43
|
+
attr_accessor object_name
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def create_object(klass, name, attribs)
|
37
50
|
obj = klass.new
|
38
51
|
if attribs
|
39
52
|
obj.instance_eval do
|
@@ -45,6 +58,13 @@ module Klassy
|
|
45
58
|
end
|
46
59
|
end
|
47
60
|
end
|
61
|
+
|
62
|
+
obj.class.class_eval do
|
63
|
+
def attributes
|
64
|
+
instance_values
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
48
68
|
obj
|
49
69
|
end
|
50
70
|
|
@@ -53,10 +73,8 @@ module Klassy
|
|
53
73
|
Klassy.const_get("#{name.to_s.camelize}")
|
54
74
|
rescue
|
55
75
|
Klassy.const_set("#{name.to_s.camelize}", Class.new do
|
56
|
-
|
57
|
-
|
58
|
-
end
|
59
|
-
attr_accessor :children
|
76
|
+
include ::ActiveModel::Serializers::JSON
|
77
|
+
include ::ActiveModel::Serializers::Xml
|
60
78
|
end)
|
61
79
|
end
|
62
80
|
end
|
data/lib/klassy/version.rb
CHANGED
data/spec/klassy_spec.rb
CHANGED
@@ -23,25 +23,25 @@ describe Klassy do
|
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should create children recursively" do
|
26
|
-
@result_object.
|
26
|
+
@result_object.second_class.first.should
|
27
27
|
be_an_instance_of(Klassy::SecondClass)
|
28
|
-
@result_object.
|
28
|
+
@result_object.second_class.first.third_class.first.should
|
29
29
|
be_an_instance_of(Klassy::ThirdClass)
|
30
|
-
@result_object.
|
30
|
+
@result_object.second_class.count.should eq(2)
|
31
31
|
end
|
32
32
|
|
33
33
|
it "should create attributes dynamically" do
|
34
34
|
@result_object.name.should eq("bob")
|
35
|
-
@result_object.
|
36
|
-
@result_object.
|
37
|
-
@result_object.
|
35
|
+
@result_object.second_class.first.color.should eq("green")
|
36
|
+
@result_object.second_class.first.answer.should eq(42)
|
37
|
+
@result_object.second_class.last.sky.should eq("blue")
|
38
38
|
end
|
39
39
|
|
40
40
|
end
|
41
41
|
|
42
42
|
describe "resulting object" do
|
43
43
|
it "should respond to #children" do
|
44
|
-
@result_object.should respond_to(:
|
44
|
+
@result_object.should respond_to(:second_class)
|
45
45
|
end
|
46
46
|
|
47
47
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: klassy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Johnny Holton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|