walt 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. data/.gitignore +14 -0
  2. data/CHANGELOG.md +3 -0
  3. data/Gemfile +4 -0
  4. data/Gemfile.lock +61 -0
  5. data/LICENSE +26 -0
  6. data/README.md +193 -0
  7. data/Rakefile +17 -0
  8. data/app/app_delegate.rb +71 -0
  9. data/examples/Apple/.gitignore +13 -0
  10. data/examples/Apple/Rakefile +14 -0
  11. data/examples/Apple/app/app_delegate.rb +99 -0
  12. data/examples/Apple/spec/main_spec.rb +9 -0
  13. data/examples/Apple/vendor/Podfile.lock +11 -0
  14. data/examples/Bouncing/.gitignore +13 -0
  15. data/examples/Bouncing/Rakefile +14 -0
  16. data/examples/Bouncing/app/app_delegate.rb +49 -0
  17. data/examples/Bouncing/spec/main_spec.rb +9 -0
  18. data/examples/Bouncing/vendor/Podfile.lock +11 -0
  19. data/lib/walt.rb +27 -0
  20. data/lib/walt/animation.rb +124 -0
  21. data/lib/walt/animation_set.rb +64 -0
  22. data/lib/walt/asset/asset.rb +79 -0
  23. data/lib/walt/asset/image.rb +48 -0
  24. data/lib/walt/asset/text.rb +64 -0
  25. data/lib/walt/operation/base.rb +60 -0
  26. data/lib/walt/operation/fade.rb +35 -0
  27. data/lib/walt/operation/move.rb +73 -0
  28. data/lib/walt/operation/rotate.rb +41 -0
  29. data/lib/walt/operation/scale.rb +41 -0
  30. data/lib/walt/patch/ui_color.rb +5 -0
  31. data/lib/walt/support/attr_default.rb +16 -0
  32. data/lib/walt/support/const_get.rb +60 -0
  33. data/lib/walt/support/font.rb +87 -0
  34. data/lib/walt/version.rb +3 -0
  35. data/lib/walt/walt.rb +53 -0
  36. data/spec/animation_set_spec.rb +49 -0
  37. data/spec/animation_spec.rb +128 -0
  38. data/spec/asset/image_spec.rb +16 -0
  39. data/spec/asset/text_spec.rb +24 -0
  40. data/spec/asset_spec.rb +11 -0
  41. data/spec/main_spec.rb +9 -0
  42. data/spec/operation/fade_spec.rb +25 -0
  43. data/spec/operation/move_spec.rb +43 -0
  44. data/spec/operation_spec.rb +26 -0
  45. data/spec/support_spec.rb +46 -0
  46. data/vendor/Podfile.lock +11 -0
  47. data/walt.gemspec +21 -0
  48. metadata +172 -0
@@ -0,0 +1,3 @@
1
+ module Walt
2
+ VERSION = "0.1"
3
+ end
data/lib/walt/walt.rb ADDED
@@ -0,0 +1,53 @@
1
+ =begin
2
+ Use Walt.animate(...) to add animations to a given `UIView`.
3
+ Walt.animate({
4
+ assets: [...],
5
+ animations: [...],
6
+ in: a_ui_view
7
+ })
8
+ =end
9
+ module Walt
10
+ module_function
11
+
12
+ def pending_animations
13
+ @pending_animations ||= []
14
+ end
15
+
16
+ def animation_sets
17
+ @animation_sets ||= []
18
+ end
19
+
20
+ def pending_assets
21
+ @pending_assets ||= {}
22
+ end
23
+
24
+ def animate(animation = {})
25
+ view = animation.delete(:in)
26
+
27
+ self.pending_animations << animation
28
+
29
+ self.pending_assets[animation.object_id] = animation[:assets].count
30
+ animation[:assets].collect! do |asset|
31
+ _asset = (asset.is_a?(Walt::Asset) ? asset : Walt::Asset.for(asset))
32
+ _asset.on_ready do |a|
33
+ self.pending_assets[animation.object_id] -= 1
34
+ if self.pending_assets[animation.object_id] == 0
35
+ self.trigger_animation(animation)
36
+ self.pending_animations.delete(animation)
37
+ end
38
+ end
39
+ view.addSubview(_asset.view)
40
+ _asset.view.hidden = true
41
+ _asset
42
+ end
43
+ end
44
+
45
+ def trigger_animation(animation = {})
46
+ animation_set = Walt::AnimationSet.new(animation)
47
+ animation_set.assets.each do |asset|
48
+ asset.view.hidden = false
49
+ end
50
+ self.animation_sets << animation_set
51
+ animation_set.animate
52
+ end
53
+ end
@@ -0,0 +1,49 @@
1
+ describe "Walt::AnimationSet" do
2
+ describe "#assets=" do
3
+ before do
4
+ @set = Walt::AnimationSet.new
5
+ end
6
+
7
+ it "works with Assets" do
8
+ asset = Walt::Asset.new(id: :hello, view: UIView.alloc.initWithFrame(CGRectZero))
9
+ @set.assets = [asset]
10
+ @set.assets.is_a?(NSArray).should == true
11
+ end
12
+
13
+ it "works with hash" do
14
+ asset = {id: :hello, view: UIView.alloc.initWithFrame(CGRectZero)}
15
+ @set.assets = [asset]
16
+ @set.assets.is_a?(NSArray).should == true
17
+ end
18
+
19
+ after do
20
+ @set.assets.each {|value|
21
+ value.is_a?(Walt::Asset).should == true
22
+ }
23
+ end
24
+ end
25
+
26
+ describe "#animations=" do
27
+ before do
28
+ @set = Walt::AnimationSet.new
29
+ end
30
+
31
+ it "works with Assets" do
32
+ animation = Walt::Animation.new(delay: 0, duration: 3)
33
+ @set.animations = [animation]
34
+ @set.animations.is_a?(NSArray).should == true
35
+ end
36
+
37
+ it "works with hash" do
38
+ animation = {delay: 0, duration: 3}
39
+ @set.animations = [animation]
40
+ @set.animations.is_a?(NSArray).should == true
41
+ end
42
+
43
+ after do
44
+ @set.animations.each {|value|
45
+ value.is_a?(Walt::Animation).should == true
46
+ }
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,128 @@
1
+ describe "Walt::Animation" do
2
+ describe "#after=" do
3
+ before do
4
+ @anim = Walt::Animation.new
5
+ end
6
+
7
+ it "works with hash" do
8
+ after = {delay: 0, duration: 3}
9
+ @anim.after = after
10
+ @anim.after.should.not == nil
11
+ end
12
+
13
+ it "works with Animation" do
14
+ after = Walt::Animation.new(delay: 0, duration: 3)
15
+ @anim.after = after
16
+ @anim.after.should == after
17
+ end
18
+
19
+ after do
20
+ @anim.after.is_a?(Walt::Animation).should == true
21
+ @anim.after.start.should == 0
22
+ @anim.after.duration.should == 3
23
+ end
24
+ end
25
+
26
+ describe "#operations=" do
27
+ before do
28
+ @anim = Walt::Animation.new
29
+ end
30
+
31
+ it "works with hash" do
32
+ op = {type: :move}
33
+ @anim.operations = [op]
34
+ @anim.operations.should.not == nil
35
+ end
36
+
37
+ it "works with Operation" do
38
+ op = Walt::Operation.for(type: :move)
39
+ @anim.operations = [op]
40
+ @anim.operations.should.not == nil
41
+ end
42
+
43
+ after do
44
+ @anim.operations.length.should == 1
45
+ @anim.operations.each {|op| op.is_a?(Walt::Operation::Base).should == true }
46
+ end
47
+ end
48
+
49
+ describe "#assets=" do
50
+ describe "with NSArray" do
51
+ before do
52
+ @anim = Walt::Animation.new
53
+ end
54
+
55
+ it "works with Assets" do
56
+ asset = Walt::Asset.new(id: :hello, view: UIView.alloc.initWithFrame(CGRectZero))
57
+ @anim.assets = [asset]
58
+ @anim.assets.is_a?(NSDictionary).should == true
59
+ end
60
+
61
+ it "works with hash" do
62
+ asset = {id: :hello, view: UIView.alloc.initWithFrame(CGRectZero)}
63
+ @anim.assets = [asset]
64
+ @anim.assets.is_a?(NSDictionary).should == true
65
+ end
66
+
67
+ after do
68
+ @anim.assets.each {|key, value|
69
+ value.is_a?(Walt::Asset).should == true
70
+ key.should == value.id
71
+ }
72
+ @anim.assets[:hello].should.not == nil
73
+ end
74
+ end
75
+
76
+ describe "with NSDictionary" do
77
+ before do
78
+ @anim = Walt::Animation.new
79
+ end
80
+
81
+ it "works with Assets" do
82
+ asset = Walt::Asset.new(id: :hello, view: UIView.alloc.initWithFrame(CGRectZero))
83
+ @anim.assets = {hello: asset}
84
+ @anim.assets.is_a?(NSDictionary).should == true
85
+ end
86
+
87
+ it "works with hash" do
88
+ asset = {id: :hello, view: UIView.alloc.initWithFrame(CGRectZero)}
89
+ @anim.assets = {hello: asset}
90
+ @anim.assets.is_a?(NSDictionary).should == true
91
+ end
92
+
93
+ after do
94
+ @anim.assets.each {|key, value|
95
+ value.is_a?(Walt::Asset).should == true
96
+ key.should == value.id
97
+ }
98
+ @anim.assets[:hello].should.not == nil
99
+ end
100
+ end
101
+ end
102
+
103
+ describe "#options=" do
104
+ describe "with constants" do
105
+ before do
106
+ @anim = Walt::Animation.new
107
+ end
108
+
109
+ it "works" do
110
+ options = UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
111
+ @anim.options = [UIViewAnimationOptionAutoreverse, UIViewAnimationOptionRepeat]
112
+ @anim.animation_options.should == options
113
+ end
114
+ end
115
+
116
+ describe "with symbols" do
117
+ before do
118
+ @anim = Walt::Animation.new
119
+ end
120
+
121
+ it "works" do
122
+ options = UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat
123
+ @anim.options = [:autoreverse, :repeat]
124
+ @anim.animation_options.should == options
125
+ end
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,16 @@
1
+ describe "Walt::ImageAsset" do
2
+ describe "#initialize" do
3
+ it "should work with multi params" do
4
+ asset = Walt::ImageAsset.new(url: "derp", id: "blue")
5
+ asset.url.should == "derp"
6
+ asset.id.should == "blue"
7
+ end
8
+ end
9
+
10
+ describe "#view" do
11
+ it "should create remote UIImageView" do
12
+ asset = Walt::ImageAsset.new(url: "http://upload.wikimedia.org/wikipedia/commons/3/30/Googlelogo.png", id: "google")
13
+ asset.view.af_imageRequestOperation.should.not == nil
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,24 @@
1
+ describe "Walt::TextAsset" do
2
+ describe "#view" do
3
+ describe "with attributes hash" do
4
+ it "should work" do
5
+ @asset = Walt::TextAsset.new(attributes: {font: {name: "MarkerFelt-Thin", size: 24}, color: "blue", shadow_color: "red", shadow_offset: {x: 10, y: 20}})
6
+ @asset.view.font.should == UIFont.fontWithName("MarkerFelt-Thin", size:24)
7
+ @asset.view.textColor.should == UIColor.blueColor
8
+ @asset.view.shadowColor.should == UIColor.redColor
9
+ @asset.view.shadowOffset.should == CGSizeMake(10, 20)
10
+ end
11
+ end
12
+
13
+ describe "without attributes hash" do
14
+ it "should work" do
15
+ @asset = Walt::TextAsset.new(text: "Hello World", text_color: "blue", "background_color" => "red", number_of_lines: 5, text_alignment: "center")
16
+ @asset.view.text.should == "Hello World"
17
+ @asset.view.textColor.should == UIColor.blueColor
18
+ @asset.view.backgroundColor.should == UIColor.redColor
19
+ @asset.view.numberOfLines.should == 5
20
+ @asset.view.textAlignment.should == UITextAlignmentCenter
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ describe "Walt::Asset" do
2
+ describe ".new" do
3
+ describe "with a UIView" do
4
+ it "should work" do
5
+ view = UIView.alloc.initWithFrame([[0,0,],[100,100]])
6
+ a = Walt::Asset.new(view)
7
+ a.view.should == view
8
+ end
9
+ end
10
+ end
11
+ end
data/spec/main_spec.rb ADDED
@@ -0,0 +1,9 @@
1
+ describe "Application 'Walt'" do
2
+ before do
3
+ @app = UIApplication.sharedApplication
4
+ end
5
+
6
+ it "has one window" do
7
+ @app.windows.size.should == 1
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ describe "Walt::Operation::FadeOperation" do
2
+ before do
3
+ @view = UIView.alloc.initWithFrame([[100,100], [50,50]])
4
+ end
5
+
6
+ describe "#setup" do
7
+ describe "with :from" do
8
+ it "should work" do
9
+ op = Walt::Operation.for(type: :fade, id: :herp, from: 0.3)
10
+ op.setup(@view, nil)
11
+ @view.alpha.should == 0.3
12
+ end
13
+ end
14
+ end
15
+
16
+ describe "#finalize" do
17
+ describe "with :to" do
18
+ it "should work" do
19
+ op = Walt::Operation.for(type: :fade, id: :herp, to: 0.8)
20
+ op.finalize(@view, nil)
21
+ @view.alpha.should == 0.8
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,43 @@
1
+ describe "Walt::Operation::MoveOperation" do
2
+ before do
3
+ @view = UIView.alloc.initWithFrame([[100,100], [50,50]])
4
+ end
5
+
6
+ describe "#setup" do
7
+ describe "with one param" do
8
+ it "should work" do
9
+ op = Walt::Operation.for(type: :move, id: :herp, from: 0)
10
+ op.setup(@view, nil)
11
+ @view.origin.x.should == 0
12
+ end
13
+ end
14
+
15
+ describe "with array" do
16
+ it "should work" do
17
+ op = Walt::Operation.for(type: :move, id: :herp, from: [200, 200])
18
+ op.setup(@view, nil)
19
+ @view.origin.x.should == 200
20
+ @view.origin.y.should == 200
21
+ end
22
+ end
23
+ end
24
+
25
+ describe "#finalize" do
26
+ describe "with one param" do
27
+ it "should work" do
28
+ op = Walt::Operation.for(type: :move, id: :herp, to: 200)
29
+ op.finalize(@view, nil)
30
+ @view.origin.x.should == 200
31
+ end
32
+ end
33
+
34
+ describe "with array" do
35
+ it "should work" do
36
+ op = Walt::Operation.for(type: :move, id: :herp, to: [200, 200])
37
+ op.finalize(@view, nil)
38
+ @view.origin.x.should == 200
39
+ @view.origin.y.should == 200
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,26 @@
1
+ describe "Walt::Operation" do
2
+ it "should return proper subclass for :type" do
3
+ o = Walt::Operation.for({type: :move})
4
+ o.is_a?(Walt::Operation::MoveOperation).should == true
5
+
6
+ o = Walt::Operation.for({type: :fade})
7
+ o.is_a?(Walt::Operation::FadeOperation).should == true
8
+
9
+ Walt::Operation.operation_types.each do |op|
10
+ string = "#{op.to_s.downcase}_operation".camelize
11
+ klass = Walt::Operation.const_get(string)
12
+
13
+ o = Walt::Operation.for({op => :blue})
14
+ o.is_a?(klass).should == true
15
+ o.id.should == :blue
16
+ end
17
+ end
18
+
19
+ it "should have nested attributes" do
20
+ o = Walt::Operation.for(type: :move, id: :herp, from: 0, to: 10)
21
+ o.type.should == :move
22
+ o.id.should == :herp
23
+ o.from.should.not == nil
24
+ o.to.should.not == nil
25
+ end
26
+ end
@@ -0,0 +1,46 @@
1
+ class AttrTest
2
+ extend Walt::Support::AttrDefault
3
+
4
+ attr_default :test, []
5
+ end
6
+
7
+ describe "Walt::Support::AttrDefault" do
8
+ it "should work" do
9
+ a = AttrTest.new
10
+ a.respond_to?("test").should == true
11
+ a.respond_to?("test=").should == true
12
+ a.test.should == []
13
+ end
14
+ end
15
+
16
+ describe "Walt::Font" do
17
+ describe ".make" do
18
+ [[:system, "systemFontOfSize:"], [:bold, "boldSystemFontOfSize:"], [:italic, "italicSystemFontOfSize:"]].each do |font, method|
19
+ it "should work with system font #{font}" do
20
+ f = Walt::Font.make(font, 12)
21
+ f.should == UIFont.send(method, 12)
22
+
23
+ f = Walt::Font.make(name: font, size: 12)
24
+ f.should == UIFont.send(method, 12)
25
+ end
26
+ end
27
+
28
+ it "should work with named font" do
29
+ f = Walt::Font.make("Chalkduster", 12)
30
+ f.should == UIFont.fontWithName("Chalkduster", size:12)
31
+ end
32
+ end
33
+
34
+ describe ".attributes" do
35
+ it "should work" do
36
+ _attributes = Walt::Font.attributes(font: UIFont.systemFontOfSize(12), color: "red", shadow_color: "blue", shadow_offset: {x: 5, y: 10})
37
+
38
+ _attributes.should == {
39
+ UITextAttributeFont => UIFont.systemFontOfSize(12),
40
+ UITextAttributeTextColor => UIColor.redColor,
41
+ UITextAttributeTextShadowColor => UIColor.blueColor,
42
+ UITextAttributeTextShadowOffset => NSValue.valueWithUIOffset(UIOffsetMake(5, 10))
43
+ }
44
+ end
45
+ end
46
+ end