sugarcube 0.1.6 → 0.2.0

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/README.md CHANGED
@@ -54,18 +54,6 @@ Examples
54
54
  # create a percentage
55
55
  100.0.percent # => 1.00
56
56
  55.0.percent # => 0.55
57
- ```
58
-
59
- NSNotificationCenter
60
- ----------------------
61
-
62
- Makes it easy to post a notification to some or all objects.
63
-
64
- ```ruby
65
- # this one is handy, I think:
66
- "my notification".post_notification # => NSNotificationCenter.defaultCenter.postNotificationName("my notification", object:nil)
67
- "my notification".post_notification(obj) # => NSNotificationCenter.defaultCenter.postNotificationName("my notification", object:obj)
68
- "my notification".post_notification(obj, user: 'dict') # => NSNotificationCenter.defaultCenter.postNotificationName("my notification", object:obj, userInfo:{user: 'dict'})
69
57
  ```
70
58
 
71
59
  NSURL
@@ -76,16 +64,8 @@ Makes it easy to post a notification to some or all objects.
76
64
  "https://github.com".nsurl.open # => UIApplication.sharedApplication.openURL(NSURL.URLWithString("https://github.com"))
77
65
  ```
78
66
 
79
- NSUserDefaults
80
- ----------------
81
-
82
- ```ruby
83
- ['any', 'objects'].save_to_default(:key) # => NSUserDefaults.standardUserDefaults.setObject(['any', 'objects'], forKey: :key)
84
- :key.get_default # => NSUserDefaults.standardUserDefaults.objectForKey(:key)
85
- ```
86
-
87
- String
88
- --------
67
+ NSString
68
+ ----------
89
69
 
90
70
  ```ruby
91
71
  # UIImage from name
@@ -178,4 +158,139 @@ button.off(:all)
178
158
  You can only remove handlers by "type", not by the action. e.g. If you bind
179
159
  three `:touch` events, calling `button.off(:touch)` will remove all three.
180
160
 
161
+ NSNotificationCenter
162
+ ----------------------
163
+
164
+ Makes it easy to post a notification to some or all objects.
165
+
166
+ ```ruby
167
+ # this one is handy, I think:
168
+ "my notification".post_notification # => NSNotificationCenter.defaultCenter.postNotificationName("my notification", object:nil)
169
+ "my notification".post_notification(obj) # => NSNotificationCenter.defaultCenter.postNotificationName("my notification", object:obj)
170
+ "my notification".post_notification(obj, user: 'dict') # => NSNotificationCenter.defaultCenter.postNotificationName("my notification", object:obj, userInfo:{user: 'dict'})
171
+ ```
172
+
173
+ NSUserDefaults
174
+ ----------------
175
+
176
+ ```ruby
177
+ :key.set_default(['any', 'objects']) # => NSUserDefaults.standardUserDefaults.setObject(['any', 'objects'], forKey: :key)
178
+ :key.get_default # => NSUserDefaults.standardUserDefaults.objectForKey(:key)
179
+ ```
180
+
181
+ This is strange, and backwards, which is just sugarcube's style. But there is
182
+ one advantage to doing it this way. Compare these two snippets:
183
+
184
+ ```ruby
185
+ # BubbleWrap
186
+ App::Persistance[:test] = { my: 'test' }
187
+ # sugarcube
188
+ :test.set_default { my: 'test' }
189
+ # k, BubbleWrap looks better
190
+
191
+ App::Persistance[:test][:my] == 'test' # true
192
+ :test.get_default[:my] # true, and odd looking - what's my point?
193
+
194
+ App::Persistance[:test][:my] = 'new' # nothing is saved. bug
195
+ :test.get_default[:my] = 'new' # nothing is saved, but that's *obvious*
196
+
197
+ test = App::Persistance[:test]
198
+ test[:my] = 'new'
199
+ App::Persistance[:test] = test # saved
200
+
201
+ test = :test.get_default
202
+ test[:my] = 'new'
203
+ :test.set_default test
204
+ ```
205
+
206
+ CoreGraphics
207
+ --------------
208
+
209
+ ###### Is it `CGMakeRect` or `CGRectMake`?
210
+
211
+ Instead, just use `Rect`, `Size` and `Point`. These are namespaced in
212
+ `SugarCube` module, but I recommend you `include SugarCube` in app_delegate.rb.
213
+
214
+ ```ruby
215
+ f = SugarCube::Rect(view.frame) # converts a CGRect into a CGRectArray
216
+ o = SugarCube::Point(view.frame.origin) # converts a CGPoint into a CGPointArray
217
+ s = SugarCube::Size(view.frame.size) # converts a CGSize into a CGSizeArray
218
+
219
+ # lots of other conversions are possible. Let's assume `include SugarCube`, too
220
+ # 4 numbers
221
+ f = Rect(x, y, w, h)
222
+ # or two arrays
223
+ p = Point(x, y) # or just [x, y] works, too
224
+ s = Size(w, h) # again, [w, h] is fine
225
+ f = Rect(p, s)
226
+ f = Rect([[x, y], [w, h]]) # same as above (the CG*Array objects will get created for you)
227
+
228
+ view.frame = f # Rect returns a CGRectArray, which view.frame can accept
229
+ ```
230
+
231
+ ###### Animations
232
+
233
+ jQuery-like animation methods.
234
+
235
+ ```ruby
236
+ # default timeout is 0.3
237
+ view.fadeout { |view|
238
+ view.removeFromSuperview
239
+ }
240
+ # options:
241
+ view.fadeout(0.5, delay: 0,
242
+ options: UIViewAnimationOptionCurveLinear,
243
+ opacity: 0.5) { |view|
244
+ view.removeFromSuperview
245
+ }
246
+ view.move_to([0, 100]) # move to position 0, 100
247
+ view.delta_to([0, 100]) # move over 0, down 100, from current position
248
+ ```
249
+
250
+ REPL View adjustments
251
+ -----------------------
252
+
253
+ Pixel pushing is an unfortunate but necessary evil. Well, at least we can make
254
+ it a little less painful.
255
+
256
+ These methods help you adjust the frame of a view. They are in the `SugarCube`
257
+ module so as not to conflict. If you don't want the prefix, `include SugarCube` in
258
+ app_delegate.rb
259
+
260
+ ```ruby
261
+ # if you are in the REPL, you might not be able to click on the view you want...
262
+ > SugarCube::adjust superview.subviews[4].subviews[1]
263
+ > SugarCube::up 1
264
+ > SugarCube::down 1 # same as up -1, obviously
265
+ > SugarCube::left 1
266
+ > SugarCube::right 1 # same as up -1, obviously
267
+ > SugarCube::origin 10, 12 # move to x:10, y:12
268
+ > SugarCube::wider 1
269
+ > SugarCube::thinner 1
270
+ > SugarCube::taller 1
271
+ > SugarCube::shorter 1
272
+ > SugarCube::size 100, 10 # set size to width:100, height: 10
273
+ > SugarCube::restore
274
+ ```
275
+
276
+ ```ruby
277
+ > # short versions! and let's assume I ran `include SC`
278
+ > a superview.subviews[4].subviews[1] # this is not uncommon in the REPL
279
+ > u # up, default value=1
280
+ > d # down
281
+ > l # left
282
+ > r # right
283
+ > o 10, 12 # origin, also accepts an array (or Point() object)
284
+ > w # wider
285
+ > n # thinner
286
+ > t # taller
287
+ > s # shorter
288
+ > z 100, 10 # size, also accepts an array (or Size() object)
289
+ > r # restore
290
+
291
+ # if you forget what view you are adjusting, run `adjust` again
292
+ > a
293
+ => {UITextField @ x: 46.0 y:214.0, 280.0×33.0} child of UIView
294
+ ```
295
+
181
296
  [BubbleWrap]: https://github.com/rubymotion/BubbleWrap
@@ -0,0 +1,93 @@
1
+ module SugarCube
2
+ module_function
3
+
4
+ def adjust view=nil
5
+ return @@sugarcube_view if not view
6
+
7
+ @@sugarcube_view = view
8
+ @@sugarcube_restore = view.frame
9
+ view
10
+ end
11
+ alias :a :adjust
12
+
13
+ ##| ORIGIN
14
+ def left val=1
15
+ SugarCube::right -val
16
+ end
17
+ alias :l :left
18
+
19
+ def right val=1
20
+ f = @@sugarcube_view.frame
21
+ f.origin.x += val
22
+ @@sugarcube_view.frame = f
23
+ end
24
+ alias :r :right
25
+
26
+ def up val=1
27
+ SugarCube::down -val
28
+ end
29
+ alias :u :up
30
+
31
+ def down val=1
32
+ f = @@sugarcube_view.frame
33
+ f.origin.y += val
34
+ @@sugarcube_view.frame = f
35
+ end
36
+ alias :d :down
37
+
38
+ def origin x, y=nil
39
+ f = @@sugarcube_view.frame
40
+ if y
41
+ f.origin.x = x
42
+ f.origin.y = y
43
+ else
44
+ f.origin = x
45
+ end
46
+ @@sugarcube_view.frame = f
47
+ end
48
+ alias :o :origin
49
+
50
+ ##| SIZE
51
+ def thinner val=1
52
+ SugarCube::wider -val
53
+ end
54
+ alias :n :thinner
55
+
56
+ def wider val=1
57
+ f = @@sugarcube_view.frame
58
+ f.size.width += val
59
+ @@sugarcube_view.frame = f
60
+ end
61
+ alias :w :wider
62
+
63
+ def taller val=1
64
+ SugarCube::shorter -val
65
+ end
66
+ alias :t :taller
67
+
68
+ def shorter val=1
69
+ f = @@sugarcube_view.frame
70
+ f.size.height += val
71
+ @@sugarcube_view.frame = f
72
+ end
73
+ alias :s :shorter
74
+
75
+ def size w, h=nil
76
+ f = @@sugarcube_view.frame
77
+ if h
78
+ f.size.width = w
79
+ f.size.height = h
80
+ else
81
+ f.size = w
82
+ end
83
+ @@sugarcube_view.frame = f
84
+ end
85
+ alias :z :size
86
+
87
+ ##| RESTORE
88
+ def restore
89
+ @@sugarcube_view.frame = @@sugarcube_restore
90
+ end
91
+ alias :r :restore
92
+
93
+ end
@@ -1,12 +1,34 @@
1
1
 
2
2
  class CGRectArray < Array
3
3
 
4
+ def initialize args
5
+ if args.length == 4
6
+ super [CGPointArray.new([args[0], args[1]]), CGSizeArray.new([args[2], args[3]])]
7
+ else
8
+ unless CGPointArray === args[0]
9
+ args[0] = Point(args[0])
10
+ end
11
+ unless CGSizeArray === args[1]
12
+ args[1] = Size(args[1])
13
+ end
14
+ super [args[0], args[1]]
15
+ end
16
+ end
17
+
4
18
  def origin
5
- return CGPointArray.new(self[0])
19
+ return self[0]
20
+ end
21
+
22
+ def origin= val
23
+ self[0] = Point(val)
6
24
  end
7
25
 
8
26
  def size
9
- return CGSizeArray.new(self[1])
27
+ return self[1]
28
+ end
29
+
30
+ def size= val
31
+ self[1] = Size(val)
10
32
  end
11
33
 
12
34
  end
@@ -18,10 +40,18 @@ class CGPointArray < Array
18
40
  return self[0]
19
41
  end
20
42
 
43
+ def x= val
44
+ self[0] = val
45
+ end
46
+
21
47
  def y
22
48
  return self[1]
23
49
  end
24
50
 
51
+ def y= val
52
+ self[1] = val
53
+ end
54
+
25
55
  end
26
56
 
27
57
  class CGSizeArray < Array
@@ -30,28 +60,68 @@ class CGSizeArray < Array
30
60
  return self[0]
31
61
  end
32
62
 
63
+ def width= val
64
+ self[0] = val
65
+ end
66
+
33
67
  def height
34
68
  return self[1]
35
69
  end
36
70
 
71
+ def height= val
72
+ self[1] = val
73
+ end
74
+
37
75
  end
38
76
 
39
77
 
40
- class Kernel
78
+ module SugarCube
79
+ module_function
41
80
 
42
- def Size(w, h)
43
- retun CGSizeArray.new([w, h])
81
+ def Size(w_or_size, h=nil)
82
+ if not h
83
+ if CGSize === w_or_size
84
+ w = w_or_size.width
85
+ h = w_or_size.height
86
+ elsif Array === w_or_size
87
+ w = w_or_size[0]
88
+ h = w_or_size[1]
89
+ else
90
+ raise RuntimeError.new("Invalid argument sent to Size(#{w_or_size.inspect})")
91
+ end
92
+ else
93
+ w = w_or_size
94
+ end
95
+ return CGSizeArray.new([w, h])
44
96
  end
45
97
 
46
- def Point(x, y)
47
- retun CGPointArray.new([x, y])
98
+ def Point(x_or_origin, y=nil)
99
+ if not y
100
+ if CGPoint === x_or_origin
101
+ x = x_or_origin.x
102
+ y = x_or_origin.y
103
+ elsif Array === x_or_origin
104
+ x = x_or_origin[0]
105
+ y = x_or_origin[1]
106
+ else
107
+ raise RuntimeError.new("Invalid argument sent to Point(#{x_or_origin.inspect})")
108
+ end
109
+ else
110
+ x = x_or_origin
111
+ end
112
+ return CGPointArray.new([x, y])
48
113
  end
49
114
 
50
115
  # Accepts 2 or 4 arguments. 2 arguments should be a point and a size,
51
116
  # 4 should be x, y, w, h
52
117
  def Rect(x_or_origin, y_or_size=nil, w=nil, h=nil)
53
118
  if not y_or_size
54
- if CGRect === x_or_origin
119
+ if CGRectArray === x_or_origin
120
+ x = x_or_origin[0][0]
121
+ y = x_or_origin[0][1]
122
+ w = x_or_origin[1][0]
123
+ h = x_or_origin[1][1]
124
+ elsif CGRect === x_or_origin
55
125
  x = x_or_origin.origin.x
56
126
  y = x_or_origin.origin.y
57
127
  w = x_or_origin.size.width
@@ -1,21 +1,15 @@
1
-
2
1
  class Symbol
3
2
 
4
3
  def get_default
5
4
  NSUserDefaults.standardUserDefaults.objectForKey(self)
6
5
  end
7
6
 
8
- def remove_default
9
- NSUserDefaults.standardUserDefaults.removeObjectForKey(self)
7
+ def set_default val
8
+ NSUserDefaults.standardUserDefaults.setObject(val, forKey:self)
10
9
  end
11
10
 
12
- end
13
-
14
-
15
- class Object
16
-
17
- def save_to_default(key)
18
- NSUserDefaults.standardUserDefaults.setObject(self, forKey:key)
11
+ def remove_default
12
+ NSUserDefaults.standardUserDefaults.removeObjectForKey(self)
19
13
  end
20
14
 
21
15
  end
@@ -1,6 +1,6 @@
1
1
 
2
2
 
3
- class String
3
+ class NSString
4
4
 
5
5
  def document
6
6
  @docs ||= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, true)[0]
@@ -1,6 +1,4 @@
1
-
2
-
3
- class String
1
+ class NSString
4
2
 
5
3
  def post_notification(object=nil, user_info=nil)
6
4
  if user_info and not Hash === user_info
@@ -1,4 +1,4 @@
1
- class String
1
+ class NSString
2
2
 
3
3
  def nsurl
4
4
  @url ||= NSURL.alloc.initWithString(self)
@@ -1,5 +1,7 @@
1
1
 
2
2
  class UIColor
3
+ def uicolor ; self ; end
4
+
3
5
 
4
6
  def to_s
5
7
  alpha = self.alpha.to_s
@@ -0,0 +1,6 @@
1
+
2
+ class UIFont
3
+ def uifont ; self ; end
4
+
5
+
6
+ end
@@ -1,6 +1,8 @@
1
1
 
2
2
 
3
3
  class UIImage
4
+ def uiimage ; self ; end
5
+
4
6
 
5
7
  def uicolor(alpha=nil)
6
8
  color = UIColor.colorWithPatternImage(self)
@@ -6,19 +6,49 @@ class UIView
6
6
  self.addSubview view
7
7
  end
8
8
 
9
- # i like this idea, but view[0] = view doesn't *look* like an insertion,
10
- # it lookes like a replacement... it should be written as
11
- # view[0] += sub_view
12
- # or
13
- # view[0] << sub_view
14
- # def []=(index, view)
15
- # self.insertSubview(view, atIndex:index)
16
- # end
17
-
18
9
  def to_s
19
10
  "{#{self.class.name} @ x: #{self.frame.origin.x} y:#{self.frame.origin.y}, "\
20
11
  "#{self.frame.size.width}×#{self.frame.size.height}}"\
21
12
  "#{self.superview ? ' child of ' + self.superview.class.name : ''}"
22
13
  end
23
14
 
15
+ def fadeout(duration=0.3, options={}, &after)
16
+ UIView.animateWithDuration(duration,
17
+ delay: options[:delay] || 0,
18
+ options: options[:options] || UIViewAnimationOptionCurveLinear,
19
+ animations: proc{
20
+ self.layer.opacity = options[:opacity] || 0
21
+ }, completion:after
22
+ )
23
+ end
24
+
25
+ def move_to(position, duration=0.3, options={}, &after)
26
+ UIView.animateWithDuration(duration,
27
+ delay: options[:delay] || 0,
28
+ options: options[:options] || UIViewAnimationOptionCurveLinear,
29
+ animations: proc{
30
+ f = self.frame
31
+ f.origin = position
32
+ self.frame = position
33
+ }, completion:after
34
+ )
35
+ end
36
+
37
+ def delta_to(delta, duration=0.3, options={}, &after)
38
+ f = self.frame
39
+ delta = Point(delta)
40
+ position = Point(f.origin)
41
+ position.x += delta.x
42
+ position.y += delta.y
43
+ UIView.animateWithDuration(duration,
44
+ delay: options[:delay] || 0,
45
+ options: options[:options] || UIViewAnimationOptionCurveLinear,
46
+ animations: proc{
47
+ f = self.frame
48
+ f.origin = position
49
+ self.frame = position
50
+ }, completion:after
51
+ )
52
+ end
53
+
24
54
  end
@@ -1,3 +1,3 @@
1
1
  module SugarCube
2
- Version = '0.1.6'
2
+ Version = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sugarcube
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-07-11 00:00:00.000000000 Z
13
+ date: 2012-07-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
17
- requirement: &70156160598480 !ruby/object:Gem::Requirement
17
+ requirement: &70353599174140 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70156160598480
25
+ version_requirements: *70353599174140
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rspec
28
- requirement: &70156160597880 !ruby/object:Gem::Requirement
28
+ requirement: &70353599189900 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *70156160597880
36
+ version_requirements: *70353599189900
37
37
  description: ! 'CocoaTouch/iOS is a *verbose* framework. These extensions hope to
38
38
  make
39
39
 
@@ -64,6 +64,7 @@ files:
64
64
  - README.md
65
65
  - Rakefile
66
66
  - lib/sugarcube.rb
67
+ - lib/sugarcube/adjust.rb
67
68
  - lib/sugarcube/core_graphics.rb
68
69
  - lib/sugarcube/defaults.rb
69
70
  - lib/sugarcube/document.rb
@@ -76,6 +77,7 @@ files:
76
77
  - lib/sugarcube/symbol/symbol_uicolor.rb
77
78
  - lib/sugarcube/uicolor.rb
78
79
  - lib/sugarcube/uicontrol.rb
80
+ - lib/sugarcube/uifont.rb
79
81
  - lib/sugarcube/uiimage.rb
80
82
  - lib/sugarcube/uiview.rb
81
83
  - lib/sugarcube/version.rb