dyi 1.1.1 → 1.1.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.
Files changed (48) hide show
  1. data/CHANGES +7 -1
  2. data/lib/dyi.rb +3 -1
  3. data/lib/dyi/animation.rb +5 -4
  4. data/lib/dyi/canvas.rb +5 -8
  5. data/lib/dyi/chart.rb +1 -1
  6. data/lib/dyi/chart/array_reader.rb +104 -10
  7. data/lib/dyi/chart/axis_util.rb +31 -17
  8. data/lib/dyi/chart/base.rb +104 -7
  9. data/lib/dyi/chart/csv_reader.rb +56 -8
  10. data/lib/dyi/chart/excel_reader.rb +27 -4
  11. data/lib/dyi/chart/legend.rb +10 -8
  12. data/lib/dyi/chart/line_chart.rb +29 -25
  13. data/lib/dyi/chart/pie_chart.rb +192 -29
  14. data/lib/dyi/chart/table.rb +12 -10
  15. data/lib/dyi/color.rb +9 -7
  16. data/lib/dyi/coordinate.rb +177 -61
  17. data/lib/dyi/drawing.rb +1 -1
  18. data/lib/dyi/drawing/clipping.rb +9 -3
  19. data/lib/dyi/drawing/color_effect.rb +7 -4
  20. data/lib/dyi/drawing/filter.rb +10 -7
  21. data/lib/dyi/drawing/pen.rb +421 -11
  22. data/lib/dyi/drawing/pen_3d.rb +12 -7
  23. data/lib/dyi/element.rb +5 -4
  24. data/lib/dyi/event.rb +3 -3
  25. data/lib/dyi/font.rb +6 -4
  26. data/lib/dyi/formatter.rb +1 -1
  27. data/lib/dyi/formatter/base.rb +24 -15
  28. data/lib/dyi/formatter/emf_formatter.rb +6 -5
  29. data/lib/dyi/formatter/eps_formatter.rb +15 -14
  30. data/lib/dyi/formatter/svg_formatter.rb +16 -14
  31. data/lib/dyi/formatter/svg_reader.rb +4 -3
  32. data/lib/dyi/formatter/xaml_formatter.rb +9 -7
  33. data/lib/dyi/length.rb +213 -114
  34. data/lib/dyi/matrix.rb +4 -2
  35. data/lib/dyi/painting.rb +161 -87
  36. data/lib/dyi/script.rb +1 -1
  37. data/lib/dyi/script/ecmascript.rb +18 -29
  38. data/lib/dyi/script/simple_script.rb +4 -8
  39. data/lib/dyi/shape.rb +1 -1
  40. data/lib/dyi/shape/base.rb +8 -17
  41. data/lib/dyi/shape/path.rb +102 -19
  42. data/lib/dyi/stylesheet.rb +4 -3
  43. data/lib/dyi/svg_element.rb +9 -7
  44. data/lib/dyi/type.rb +5 -2
  45. data/lib/dyi/util.rb +36 -1
  46. data/lib/ironruby.rb +1 -1
  47. data/lib/util.rb +53 -5
  48. metadata +4 -19
@@ -1,6 +1,6 @@
1
1
  # -*- encoding: UTF-8 -*-
2
2
 
3
- # Copyright (c) 2009-2011 Sound-F Co., Ltd. All rights reserved.
3
+ # Copyright (c) 2009-2012 Sound-F Co., Ltd. All rights reserved.
4
4
  #
5
5
  # Author:: Mamoru Yuo
6
6
  #
@@ -21,9 +21,10 @@
21
21
 
22
22
  require 'stringio'
23
23
 
24
- module DYI #:nodoc:
25
- module Formatter #:nodoc:
24
+ module DYI
25
+ module Formatter
26
26
 
27
+ # @since 0.0.0
27
28
  class SvgFormatter < XmlFormatter
28
29
 
29
30
  def initialize(canvas, options={})
@@ -577,7 +578,7 @@ module DYI #:nodoc:
577
578
  end
578
579
 
579
580
  # @since 1.0.0
580
- def amin_event(shape, event)
581
+ def anim_event(shape, event)
581
582
  return nil unless event
582
583
  if shape && shape == event.target
583
584
  event.event_name.to_s
@@ -588,11 +589,11 @@ module DYI #:nodoc:
588
589
 
589
590
  # @since 1.0.0
590
591
  def anim_period(shape, event, offset)
591
- [amin_event(shape, event), anim_duration(offset)].compact.join('+')
592
+ [anim_event(shape, event), anim_duration(offset)].compact.join('+')
592
593
  end
593
594
 
594
595
  # @since 1.0.0
595
- def merge_anim_attributes(anim, shape, attrs) #:nodoc:
596
+ def merge_anim_attributes(anim, shape, attrs)
596
597
  attrs[:dur] = anim_duration(anim.duration) if anim.duration && anim.duration != 0
597
598
  if anim.begin_event || anim.begin_offset
598
599
  attrs[:begin] = anim_period(shape, anim.begin_event, anim.begin_offset)
@@ -607,11 +608,11 @@ module DYI #:nodoc:
607
608
  attrs[:restart] = anim.restart if anim.restart
608
609
  end
609
610
 
610
- def name_to_attribute(name) #:nodoc:
611
+ def name_to_attribute(name)
611
612
  name.to_s.gsub(/_/,'-').to_sym
612
613
  end
613
614
 
614
- def common_attributes(shape) #:nodoc:
615
+ def common_attributes(shape)
615
616
  attributes = {}
616
617
  create_style(shape, attributes)
617
618
  attributes[:class] = shape.css_class if shape.css_class
@@ -623,7 +624,7 @@ module DYI #:nodoc:
623
624
  attributes
624
625
  end
625
626
 
626
- def create_style(shape, attributes) #:nodoc:
627
+ def create_style(shape, attributes)
627
628
  styles = {}
628
629
  if shape.font && !shape.font.empty?
629
630
  styles.merge!(shape.font.attributes)
@@ -642,17 +643,17 @@ module DYI #:nodoc:
642
643
  end
643
644
  end
644
645
 
645
- def attribute_string(value) #:nodoc:
646
+ def attribute_string(value)
646
647
  value.respond_to?(:write_as) ? "url(##{add_defs(value)})" : value.to_s
647
648
  end
648
649
 
649
- def create_transform(shape) #:nodoc:
650
+ def create_transform(shape)
650
651
  if shape.respond_to?(:transform) && !shape.transform.empty?
651
652
  shape.transform.map{|item| "#{item[0]}(#{item[1...item.size].join(',')})"}.join(' ')
652
653
  end
653
654
  end
654
655
 
655
- def add_defs(value) #:nodoc:
656
+ def add_defs(value)
656
657
  @defs.each do |def_id, def_item|
657
658
  return def_id if def_item == value
658
659
  end
@@ -661,15 +662,16 @@ module DYI #:nodoc:
661
662
  def_id
662
663
  end
663
664
 
664
- def create_def_id(index) #:nodoc:
665
+ def create_def_id(index)
665
666
  'def%03d' % index
666
667
  end
667
668
 
668
- def attribute_name(key) #:nodoc:
669
+ def attribute_name(key)
669
670
  key.to_s.gsub(/_/,'-')
670
671
  end
671
672
  end
672
673
 
674
+ # @since 1.0.0
673
675
  class PngFormatter
674
676
  def save(file_name, options={})
675
677
  IO.popen("rsvg-convert -f png -o #{file_name}", 'w+b') {|io|
@@ -1,6 +1,6 @@
1
1
  # -*- encoding: UTF-8 -*-
2
2
 
3
- # Copyright (c) 2009-2011 Sound-F Co., Ltd. All rights reserved.
3
+ # Copyright (c) 2009-2012 Sound-F Co., Ltd. All rights reserved.
4
4
  #
5
5
  # Author:: Mamoru Yuo
6
6
  #
@@ -22,9 +22,10 @@
22
22
  require 'enumerator'
23
23
  require 'rexml/document'
24
24
 
25
- module DYI #:nodoc:
26
- module Formatter #:nodoc:
25
+ module DYI
26
+ module Formatter
27
27
 
28
+ # @since 0.0.0
28
29
  class SvgReader
29
30
  class << self
30
31
  def read(file_name)
@@ -1,6 +1,6 @@
1
1
  # -*- encoding: UTF-8 -*-
2
2
 
3
- # Copyright (c) 2009-2011 Sound-F Co., Ltd. All rights reserved.
3
+ # Copyright (c) 2009-2012 Sound-F Co., Ltd. All rights reserved.
4
4
  #
5
5
  # Author:: Mamoru Yuo
6
6
  #
@@ -19,9 +19,11 @@
19
19
  # You should have received a copy of the GNU General Public License
20
20
  # along with DYI. If not, see <http://www.gnu.org/licenses/>.
21
21
 
22
- module DYI #:nodoc:
23
- module Formatter #:nodoc:
22
+ #
23
+ module DYI
24
+ module Formatter
24
25
 
26
+ # @since 0.0.0
25
27
  class XamlFormatter < XmlFormatter
26
28
 
27
29
  FONT_STYLE = {'normal'=>'Normal','italic'=>'Italic'}
@@ -222,7 +224,7 @@ module DYI #:nodoc:
222
224
 
223
225
  private
224
226
 
225
- def common_attributes(shape, type=nil) # :nodoc:
227
+ def common_attributes(shape, type=nil)
226
228
  attributes = {}
227
229
  font = create_font_attr(shape)
228
230
  painting, attr_creator = create_painting_attr(shape, type)
@@ -231,7 +233,7 @@ module DYI #:nodoc:
231
233
  [attributes, attr_creator]
232
234
  end
233
235
 
234
- def create_font_attr(shape) # :nodoc:
236
+ def create_font_attr(shape)
235
237
  attr = {}
236
238
  if shape.respond_to?(:font) && (font = shape.font) && !font.empty?
237
239
  attr[:FontFamily] = font.font_family if font.font_family
@@ -249,7 +251,7 @@ module DYI #:nodoc:
249
251
  attr
250
252
  end
251
253
 
252
- def create_painting_attr(shape, type) # :nodoc:
254
+ def create_painting_attr(shape, type)
253
255
  attr = {}
254
256
  attr_creator = nil
255
257
  if shape.respond_to?(:painting) && (painting = shape.painting) && !painting.empty?
@@ -294,7 +296,7 @@ module DYI #:nodoc:
294
296
  [attr, attr_creator]
295
297
  end
296
298
 
297
- def create_transform_node(shape, io) # :nodoc:
299
+ def create_transform_node(shape, io)
298
300
  create_node(io, 'TransformGroup') {
299
301
  shape.transform.each do |tr|
300
302
  case tr.first
@@ -1,9 +1,8 @@
1
1
  # -*- encoding: UTF-8 -*-
2
2
 
3
- # Copyright (c) 2009-2011 Sound-F Co., Ltd. All rights reserved.
3
+ # Copyright (c) 2009-2012 Sound-F Co., Ltd. All rights reserved.
4
4
  #
5
5
  # Author:: Mamoru Yuo
6
- # Documentation:: Mamoru Yuo
7
6
  #
8
7
  # This file is part of DYI.
9
8
  #
@@ -19,27 +18,15 @@
19
18
  #
20
19
  # You should have received a copy of the GNU General Public License
21
20
  # along with DYI. If not, see <http://www.gnu.org/licenses/>.
22
- #
23
- # == Overview
24
- #
25
- # This file provides the DYI::Length class, which provides length
26
- # supports for DYI scripts. The length is a distance measurement.
27
- #
28
- # See the documentation to the DYI::Length class for more details and
29
- # examples of usage.
30
- #
31
21
 
32
- module DYI #:nodoc:
22
+ #
23
+ module DYI
33
24
 
34
- # Class representing a length. See documentation for the file
35
- # dyi/length.rb for an overview.
36
- #
37
- # == Introduction
38
- #
39
- # This class works with an amount and a unit. The lists of unit identifiers
40
- # matches the list of unit identifiers in CSS: em, ex, px, pt, pc, cm, mm, in
41
- # and percentages(%). When a unit is not given, then the length is assumed
42
- # to be in user units (i.e., a value in the current user coordinate sytem).
25
+ # This class representing a length. Length object holds an amount and a unit.
26
+ # The lists of unit identifiers matches the list of unit identifiers in CSS:
27
+ # em, ex, px, pt, pc, cm, mm, in and percentages(%). When a unit is not given,
28
+ # then the length is assumed to be in user units (i.e., a value in the current
29
+ # user coordinate sytem).
43
30
  #
44
31
  # * As in CSS, the _em_ and _ex_ unit identifiers are relative to the current
45
32
  # font's font-size and x-height, respectively.
@@ -52,44 +39,49 @@ module DYI #:nodoc:
52
39
  # * For percentage values that are defined to be relative to the size of
53
40
  # parent element.
54
41
  #
55
- # == Ways of comparing and calculating
42
+ # = Distuptive Change
43
+ #
44
+ # Length is not possible to change destructively. +#clone+ method and +#dup+
45
+ # method raise TypeError.
56
46
  #
57
- # This class include +Comparable+ module, therefore you can use the
58
- # comparative operators. In the comparison between DYI::Length
59
- # objects, the unit of each objects are arranged and it does. The equality
60
- # operator '<tt>==</tt>' does not test equality instance but test equality
61
- # value.
47
+ # = Ways of Comparing and Calculating
62
48
  #
63
- # This class suports following arithmetic operators and methods: <tt>+</tt>,
64
- # <tt>-</tt>, <tt>*</tt>, <tt>/</tt>, <tt>%</tt>, <tt>**</tt>, +#div+, +#quo+,
65
- # +#modulo+. The operators '<tt>+</tt>', '<tt>-</tt>' coerced right hand
66
- # operand into Length, and then calculate.
49
+ # This class includes +Comparable+ module, therefore you can use the
50
+ # comparative operators. In the comparison between DYI::Length objects, the
51
+ # unit of each objects are arranged and it compares. The equality operator
52
+ # '<tt>==</tt>' does not test equality instance but test equality value.
67
53
  #
68
- # See the documentation to each operators and methods class for details.
54
+ # DYI::Length.new('1in') > DYI::Length.new(50) # => true, 1in equals 90px.
55
+ # DYI::Length.new('1in') > DYI::Length.new('2em') # => Error, 'em' is not comparable unit.
56
+ # DYI::Length.new('10cm') == DYI::Length.new('100mm') # => true
69
57
  #
70
- # == Examples of use
58
+ # This class suports following arithmetic operators and methods: {#+ +},
59
+ # {#- -}, {#* *}, {#/ /}, {#% %}, {#** **}, {#div}, {#quo}, {#modulo}. The
60
+ # operators '{#+ +}', '{#- -}' coerces a right hand operand into Length, and
61
+ # then calculates.
71
62
  #
72
- # length1 = DYI::Length.new(10) # 10 user unit (equals to 10px).
73
- # length2 = DYI::Length.new('10') # it is 10px too.
74
- # DYI::Length.new('1in') > DYI::Length.new(50)
75
- # # => true, 1in equals 90px.
76
- # DYI::Length.new('1in') > DYI::Length.new('2em')
77
- # # => Error, 'em' is not comparable unit.
78
- # DYI::Length.new('10cm') == DYI::Length.new('100mm')
79
- # # => true
63
+ # @since 0.0.0
80
64
  class Length
81
65
  include Comparable
82
66
 
83
67
  # Array of unit that can be used.
84
68
  UNITS = ['px', 'pt', '%', 'cm', 'mm', 'in', 'em', 'ex', 'pc']
85
69
 
70
+ # @private
86
71
  @@units = {'px'=>1.0,'pt'=>1.25,'cm'=>35.43307,'mm'=>3.543307,'in'=>90.0,'pc'=>15.0}
72
+ # @private
87
73
  @@default_format = '0.###U'
88
74
 
89
- # Returns a new DYI::Length object.
90
- #
91
- # +length+ is instance of +Numeric+, +String+, or
92
- # <tt>DYI::Length</tt> class.
75
+ # @overload initialize(length)
76
+ # Returns the argument itself.
77
+ # @param [Length] length the source length
78
+ # @overload initialize(num)
79
+ # @param [Numeric] num the user unit value
80
+ # @overload initialize(str)
81
+ # @param [String] str the string that mean the length
82
+ # @raise [ArgumentError] the argument is a string that could not be understand
83
+ # @raise [TypeError] the argument can not be coerced into Length
84
+ # @see .new
93
85
  def initialize(length)
94
86
  case length
95
87
  when Length
@@ -110,20 +102,26 @@ module DYI #:nodoc:
110
102
  end
111
103
  end
112
104
 
113
- # This constant is zoro length.
105
+ # Zero length.
114
106
  ZERO = new(0)
115
107
 
116
- # Returns the receiver's value.
108
+ # Unary Plus -- Returns the receiver's value.
109
+ # @return [Length] receiver itself
117
110
  def +@
118
111
  self
119
112
  end
120
113
 
121
- # Returns the receiver's value, negated.
114
+ # Unary Minus -- Returns the receiver's value, negated.
115
+ # @return [Length] the negated receiver's value
122
116
  def -@
123
117
  new_length(-@value)
124
118
  end
125
119
 
126
- # Returns a new length which is the sum of the receiver and +other+.
120
+ # Returns a new length which is the sum of the receiver and _other_. First,
121
+ # _other_ is converted into +Length+.
122
+ # @param [Length, Numeric, String] other the value that can be converted
123
+ # into +Length+
124
+ # @return [Length] a new length which is the sum of the receiver and _other_
127
125
  def +(other)
128
126
  other = self.class.new(other)
129
127
  if @unit == other._unit
@@ -133,7 +131,12 @@ module DYI #:nodoc:
133
131
  end
134
132
  end
135
133
 
136
- # Returns a new length which is the difference of the receiver and +other+.
134
+ # Returns a new length which is the difference of the receiver and _other_.
135
+ # First _other_ is converted into +Length+.
136
+ # @param [Length, Numeric, String] other the value that can be converted
137
+ # into +Length+
138
+ # @return [Length] a new length which is the difference of the receiver and
139
+ # _other_
137
140
  def -(other)
138
141
  other = self.class.new(other)
139
142
  if @unit == other._unit
@@ -143,18 +146,24 @@ module DYI #:nodoc:
143
146
  end
144
147
  end
145
148
 
146
- # Returns a new muliplicative length of the receiver by +number+.
149
+ # Returns a new muliplicative length of the receiver by _number_.
150
+ # @param [Numeric] number the operand value
151
+ # @return [Length] a new muliplicative length
147
152
  def *(number)
148
153
  new_length(@value * number)
149
154
  end
150
155
 
151
- # Raises a length the number power.
156
+ # Raises a length the _number_ power.
157
+ # @param [Numeric] number the operand value
158
+ # @return [Length] a length the _number_ power
152
159
  def **(number)
153
160
  new_length(@value ** number)
154
161
  end
155
162
 
156
- # Returns a new length which is the result of dividing the receiver by
157
- # +other+.
163
+ # Returns a number which is the result of dividing the receiver by _other_.
164
+ # @param [Length] other the operand length
165
+ # @return [Number] a number which is the result of dividing
166
+ # @raise [TypeError] _other_ can't be coerced into Length
158
167
  def div(other)
159
168
  case other
160
169
  when Length
@@ -168,8 +177,11 @@ module DYI #:nodoc:
168
177
  end
169
178
  end
170
179
 
171
- # Return a new length which is the modulo after division of the receiver by
172
- # +other+.
180
+ # Return a number which is the modulo after division of the receiver by
181
+ # _other_.
182
+ # @param [Length] other the operand length
183
+ # @return [Number] a number which is the modul
184
+ # @raise [TypeError] _other_ can't be coerced into Length
173
185
  def % (other)
174
186
  case other
175
187
  when Length
@@ -183,12 +195,19 @@ module DYI #:nodoc:
183
195
  end
184
196
  end
185
197
 
186
- # If argument +number+ is a numeric, returns a new divisional length of the
187
- # receiver by +other+. If argument +number+ is a length, returns a divisional
188
- # float of the receiver by +other+.
189
- #
190
- # DYI::Length.new(10) / 4 # => 2.5px
198
+ # Divisional operator
199
+ # @overload /(other)
200
+ # Returns a divisional float of the receiver by _other_.
201
+ # @param [Length] other the operand length
202
+ # @return [Float] a divisional value
203
+ # @overload /(number)
204
+ # Returns a new divisional length of the receiver by _number_.
205
+ # @param [Numeric] other the operand value
206
+ # @return [Length] a new divisional length
207
+ # @example
208
+ # DYI::Length.new(10) / 4 # => 2.5px
191
209
  # DYI::Length.new(10) / DYI::Length.new(4) # => 2.5
210
+ # @raise [TypeError] the argument can't be coerced into Length or Numeric
192
211
  def /(number)
193
212
  case number
194
213
  when Numeric
@@ -207,31 +226,39 @@ module DYI #:nodoc:
207
226
  alias quo /
208
227
  alias modulo %
209
228
 
210
- def clone #:nodoc:
229
+ # @private
230
+ def clone
211
231
  raise TypeError, "allocator undefined for Length"
212
232
  end
213
233
 
214
- def dup #:nodoc:
234
+ # @private
235
+ def dup
215
236
  raise TypeError, "allocator undefined for Length"
216
237
  end
217
238
 
218
- # Returns +true+ if the receiver has a zero length, +false+ otherwise.
239
+ # Returns whether the receiver is a zero length.
240
+ # @return [Boolean] true if the receiver is a zero length, false otherwise
219
241
  def zero?
220
242
  @value == 0
221
243
  end
222
244
 
223
- # Returns +self+ if the receiver is not zero, +nil+ otherwise.
245
+ # Returns whether the receiver is a no-zero length.
246
+ # @return [Length, nil] self if the receiver is not zero, nil otherwise
224
247
  def nonzero?
225
248
  @value == 0 ? nil : self
226
249
  end
227
250
 
228
251
  # Returns the absolute length of the receiver.
252
+ # @return [Length] the absolute length
229
253
  def abs
230
254
  @value >= 0 ? self : -self
231
255
  end
232
256
 
233
- # Returns +-1+, +0+, <tt>+1</tt> or +nil+ depending on whether the receiver
234
- # is less than, equal to, greater than real or not comparable.
257
+ # Comparision -- compares two values. This is the basis for the tests in
258
+ # +Comparable+.
259
+ # @return [Fixnum, nil] +-1+, +0+, <tt>+1</tt> or +nil+ depending on whether
260
+ # the receiver is less than, equal to, greater than _other_; if is not
261
+ # comparable, +nil+.
235
262
  def <=>(other)
236
263
  return nil unless other.kind_of?(Length)
237
264
  if @unit == other._unit
@@ -241,21 +268,28 @@ module DYI #:nodoc:
241
268
  end
242
269
  end
243
270
 
244
- # Returns the receiver's unit. If receiver has no unit, returns 'px'.
271
+ # Returns the receiver's unit. If receiver has no unit, returns 'px'.
272
+ # @return [String] the receiver's unit
245
273
  def unit
246
274
  @unit.nil? ? 'px' : @unit
247
275
  end
248
276
 
249
- # :call-seq:
250
- # step (limit, step) {|length| ...}
251
- # step (limit, step)
252
- #
253
- # Invokes block with the sequence of length starting at receiver,
254
- # incremented by +step+ on each call. The loop finishes when +length+ to be
255
- # passed to the block is greater than +limit+ (if +step+ is positive) or
256
- # less than +limit+ (if +step+ is negative).
257
- #
258
- # If no block is given, an enumerator is returned instead.
277
+ # Invokes block with the sequence of length starting at receiver.
278
+ # @overload step (limit, step)
279
+ # Invokes block with the sequence of length starting at receiver,
280
+ # incremented by _step_ on each call. The loop finishes when _length_ to
281
+ # be passed to the block is greater than _limit_ (if _step_ is positive)
282
+ # or less than _limit_ (if _step_ is negative).
283
+ # @param [Length] limit the limit of iteration continuation
284
+ # @param [Length] step an iteration interval
285
+ # @yield [len] an iteration block
286
+ # @yieldparam [Length] len the length that is created by stepping
287
+ # @return [Length] the receiver itself
288
+ # @overload step (limit, step)
289
+ # Returns an enumerator instead of the iteration.
290
+ # @param [Length] limit the limit of iteration continuation
291
+ # @param [Length] step an iteration interval
292
+ # @return [Enumrator] an enumrator for stepping
259
293
  def step(limit, step)
260
294
  if @unit == limit._unit && @unit == step._unit
261
295
  self_value, limit_value, step_value = @value, limit._num, step._num
@@ -275,16 +309,30 @@ module DYI #:nodoc:
275
309
  end
276
310
  end
277
311
 
278
- # Returns a new length that converted into length of user unit.
312
+ # Returns a length that converted into length of user unit.
313
+ # @return [Length] a length that converted into length of user unit
279
314
  def to_user_unit
280
315
  @unit ? self.class.new(to_f) : self
281
316
  end
282
317
 
283
- # Returns a string representing obj.
318
+ # Returns a string to represent the receiver.
284
319
  #
285
- # Format string can be specified for the argument. If no argument is given,
286
- # +default_format+ of this class is used as format string. About format
287
- # string, see the documentation to +default_format+ method.
320
+ # Format string can be specified for the argument. If no argument is given,
321
+ # {.default_format} is used as format string. About format string, see the
322
+ # documentation of {.default_format} method.
323
+ # @param [String] format a format string
324
+ # @return [Length] a string to represent the receiver
325
+ # @example
326
+ # len1 = DYI::Length.new(1234.56)
327
+ # len1.to_s('0.#u') # => "1234.6px"
328
+ # len1.to_s('0.#U') # => "1234.6"
329
+ # len1.to_s('#,#0U') # => "1,235"
330
+ #
331
+ # len2 = DYI::Length.new('10pt')
332
+ # len2.to_s('0u') # => "10pt"
333
+ # len2.to_s('0U') # => "10pt"
334
+ # @see .default_format=
335
+ # @see .set_default_format
288
336
  def to_s(format=nil)
289
337
  fmts = (format || @@default_format).split('\\\\')
290
338
  fmts = fmts.map do |fmt|
@@ -293,8 +341,12 @@ module DYI #:nodoc:
293
341
  @value.strfnum(fmts.join('\\\\'))
294
342
  end
295
343
 
296
- # Returns amount part of a length converted into given unit as float. If
344
+ # Returns amount part of the length converted into given unit as float. If
297
345
  # parameter +unit+ is given, converts into user unit.
346
+ # @param [String] unit a unit converted into
347
+ # @return [Float] amout part of the length
348
+ # @raise [RuntimeError] length can not convert into other unit
349
+ # @raise [ArgumentError] _unit_ is unknown unit
298
350
  def to_f(unit=nil)
299
351
  unless self_ratio = @unit ? @@units[@unit] : 1.0
300
352
  raise RuntimeError, "unit `#{@unit}' can not convert into user unit"
@@ -309,23 +361,24 @@ module DYI #:nodoc:
309
361
  (@value * self_ratio.quo(param_ratio)).to_f
310
362
  end
311
363
 
312
- def inspect #:nodoc:
364
+ # @private
365
+ def inspect
313
366
  @value.to_s + @unit.to_s
314
367
  end
315
368
 
316
369
  protected
317
370
 
318
- def _num #:nodoc:
371
+ def _num
319
372
  @value
320
373
  end
321
374
 
322
- def _unit #:nodoc:
375
+ def _unit
323
376
  @unit
324
377
  end
325
378
 
326
379
  private
327
380
 
328
- def new_length(value) #:nodoc:
381
+ def new_length(value)
329
382
  other = self.class.allocate
330
383
  other.instance_variable_set(:@value, value)
331
384
  other.instance_variable_set(:@unit, @unit)
@@ -336,33 +389,70 @@ module DYI #:nodoc:
336
389
 
337
390
  public
338
391
 
339
- def new(*args) #:nodoc:
392
+ # Creates and returns a new instance of Length provided the argument is
393
+ # not an instace of Length. If the argument is an instace of Length,
394
+ # returns the argument itself.
395
+ # @overload new(length)
396
+ # Returns the argument itself.
397
+ # @param [Length] length the source length
398
+ # @return [Length] the argument itself
399
+ # @overload new(num)
400
+ # @param [Numeric] num the user unit value
401
+ # @overload new(str)
402
+ # @param [String] str the string that mean the length
403
+ # @return [Length] an instace of Length
404
+ # @raise (see #initialize)
405
+ # @example
406
+ # length1 = DYI::Length.new(10) # 10 user unit (equals to 10px)
407
+ # length2 = DYI::Length.new('10') # it is 10px too
408
+ # length3 = DYI::Length.new('10px') # it is 10px too
409
+ def new(*args)
340
410
  return args.first if args.size == 1 && args.first.instance_of?(self)
341
411
  super
342
412
  end
343
413
 
344
- # Returns new instance as +new+ method when an argument is not +nil+.
345
- # If an argument is +nil+, returns +nil+.
414
+ # Returns a new instace of Length if the argments is not +nil+ (calls
415
+ # +Length.new+ method), but returns +nil+ if the argument is +nil+.
416
+ # @return [Length, nil] a new instace of Length if the argments is not
417
+ # nil, nil otherwise
418
+ # @see .new
346
419
  def new_or_nil(*args)
347
420
  (args.size == 1 && args.first.nil?) ? nil : new(*args)
348
421
  end
349
422
 
350
- # Returns a coefficient that is used for conversion from any unit into
351
- # user unit.
423
+ # Returns a coefficient that is used for conversion from _unit_ into user
424
+ # unit.
425
+ # @param [String] unit a unit name
426
+ # @return [Float] a coefficient that is used for conversion
427
+ # @raise [ArgumentError] a given unit can not be converted into another
428
+ # unit
352
429
  def unit_ratio(unit)
353
- raise ArgumentError, "unit `#{unit}' can not convert other unit" unless ratio = @@units[unit.to_s]
430
+ unless ratio = @@units[unit.to_s]
431
+ raise ArgumentError, "unit `#{unit}' can not be converted into another unit"
432
+ end
354
433
  ratio
355
434
  end
356
435
 
357
- # :call-seq:
358
- # set_default_format (format) {...}
359
- # set_default_format (format)
360
- #
361
- # Invokes block with given +format+ string as default format. After
362
- # invokes block, formar format is used.
363
- #
364
- # If no block is given, sets default format setring as +default_format=+
365
- # method.
436
+ # Invokes block with given format string as default format.
437
+ # @overload set_default_format(format)
438
+ # Invokes block with given _format_ as default format. After invokes the
439
+ # block, the original format is used.
440
+ # @param [String] format a format string
441
+ # @yield a block which the format string is used in
442
+ # @return [Coordinate] the receiver itself
443
+ # @overload set_default_format(format)
444
+ # Sets default format setring as {.default_format=} method.
445
+ # @param [String] format a format string
446
+ # @return [String] the given argument
447
+ # @example
448
+ # # an initial format string is "#.###U"
449
+ # len = DYI::Length.new(1234.56)
450
+ # len.to_s # => "1234.56"
451
+ # DYI::Length.set_default_format('#,##0u') {
452
+ # len.to_s # => "1,235px"
453
+ # }
454
+ # len.to_s # => "1234.56"
455
+ # @see .default_format=
366
456
  def set_default_format(format)
367
457
  if block_given?
368
458
  org_format = @@default_format
@@ -375,22 +465,31 @@ module DYI #:nodoc:
375
465
  end
376
466
  end
377
467
 
378
- # Returns format that is used when called to_s. See the documentation to
379
- # +default_format=+ method too.
468
+ # Returns a format that is used when called {#to_s} without an argument.
469
+ # @return [String] a format string
470
+ # @see .default_format=
380
471
  def default_format
381
472
  @@default_format
382
473
  end
383
474
 
384
- # Sets format that is used when called to_s.
385
- #
386
- # The format string is same as <tt>Numeric#strfnum</tt> format. See the
387
- # documentation to <tt>Numeric#strfnum</tt> method. In addition to
388
- # place-holder of +strfnum+, following placeholder can be used.
475
+ # Sets a format string that is used when called {#to_s}. The format string
476
+ # that is set at this method is used permanently. Use {.set_default_format}
477
+ # with a block when you want to use a format string
478
+ # temporarily.
389
479
  #
390
- # +u+:: (unit placeholder) Placeholder '+u+' is replaced as a unit. If
391
- # the unit is user unit, '+u+' is repleced as 'px'.
392
- # +U+:: (unit placeholder) Placeholder '+U+' is replaced as a unit. If
393
- # the unit is user unit, '+U+' is replece as empty string.
480
+ # The format string is same as {Numeric#strfnum} format. In addition to
481
+ # place-holder of {Numeric#strfnum}, following placeholder can be used.
482
+ # [<tt>"u"</tt> (unit placeholder)] Placeholder '+u+' is replaced as a
483
+ # unit. If the unit is user unit, '+u+'
484
+ # is repleced as 'px'.
485
+ # [<tt>"U"</tt> (unit placeholder)] Placeholder '+U+' is replaced as a
486
+ # unit. If the unit is user unit, '+U+'
487
+ # is replece as empty string.
488
+ # [<tt>"\\"</tt> (Escape Character)] Causes the next character to be
489
+ # interpreted as a literal.
490
+ # @see #to_s
491
+ # @see .set_default_format
492
+ # @see Numeric#strfnum
394
493
  def default_format=(fromat)
395
494
  @@default_format = fromat.clone
396
495
  end