facets 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +5 -2
- data/ProjectInfo +21 -12
- data/README +73 -44
- data/Rakefile +11 -11
- data/data/facets/index.yml +1 -0
- data/lib/facet/annotation.rb +1 -0
- data/lib/facet/ansicode.rb +1 -0
- data/lib/facet/array.rb +1 -34
- data/lib/facet/array/at_rand.rb +1 -52
- data/lib/facet/array/body.rb +1 -2
- data/lib/facet/array/delete_unless.rb +1 -35
- data/lib/facet/array/delete_values.rb +1 -38
- data/lib/facet/array/delete_values_at.rb +1 -57
- data/lib/facet/array/each_with_key.rb +1 -9
- data/lib/facet/array/first.rb +1 -84
- data/lib/facet/array/foot.rb +1 -2
- data/lib/facet/array/head.rb +1 -80
- data/lib/facet/array/last.rb +1 -2
- data/lib/facet/array/last_index.rb +1 -35
- data/lib/facet/array/merge.rb +1 -40
- data/lib/facet/array/mid.rb +1 -50
- data/lib/facet/array/middle.rb +1 -45
- data/lib/facet/array/op_fetch.rb +1 -40
- data/lib/facet/array/op_mod.rb +1 -16
- data/lib/facet/array/op_store.rb +1 -47
- data/lib/facet/array/pick.rb +1 -68
- data/lib/facet/array/pos.rb +1 -39
- data/lib/facet/array/pull.rb +1 -37
- data/lib/facet/array/rand_index.rb +1 -30
- data/lib/facet/array/rand_subset.rb +1 -56
- data/lib/facet/array/range.rb +1 -47
- data/lib/facet/array/rotate.rb +1 -67
- data/lib/facet/array/select.rb +1 -37
- data/lib/facet/array/shuffle.rb +1 -66
- data/lib/facet/array/slap.rb +1 -37
- data/lib/facet/array/store.rb +1 -7
- data/lib/facet/array/tail.rb +1 -2
- data/lib/facet/array/thru.rb +1 -38
- data/lib/facet/array/to_b.rb +1 -31
- data/lib/facet/array/to_h.rb +1 -57
- data/lib/facet/array/to_hash.rb +1 -46
- data/lib/facet/array/unzip.rb +1 -20
- data/lib/facet/association.rb +1 -0
- data/lib/facet/basicobject.rb +1 -0
- data/lib/facet/bbcode.rb +1 -0
- data/lib/facet/binaryreader.rb +1 -0
- data/lib/facet/binding.rb +1 -14
- data/lib/facet/binding/__DIR__.rb +1 -2
- data/lib/facet/binding/__FILE__.rb +1 -2
- data/lib/facet/binding/__LINE__.rb +1 -58
- data/lib/facet/binding/call_stack.rb +1 -42
- data/lib/facet/binding/called.rb +1 -41
- data/lib/facet/binding/caller.rb +1 -42
- data/lib/facet/binding/defined.rb +1 -41
- data/lib/facet/binding/eval.rb +1 -42
- data/lib/facet/binding/local_variables.rb +1 -45
- data/lib/facet/binding/method_name.rb +1 -48
- data/lib/facet/binding/op_fetch.rb +1 -45
- data/lib/facet/binding/op_store.rb +1 -46
- data/lib/facet/binding/self.rb +1 -41
- data/lib/facet/binding/self/of_caller.rb +1 -122
- data/lib/facet/bitmask.rb +1 -0
- data/lib/facet/blankslate.rb +1 -0
- data/lib/facet/bytes.rb +1 -0
- data/lib/facet/class.rb +1 -7
- data/lib/facet/class/by_name.rb +1 -1
- data/lib/facet/class/descendents.rb +1 -44
- data/lib/facet/class/method_name.rb +1 -33
- data/lib/facet/class/remove_descendents.rb +1 -46
- data/lib/facet/class/remove_subclasses.rb +1 -2
- data/lib/facet/class/subclasses.rb +1 -2
- data/lib/facet/class/unix_path.rb +1 -34
- data/lib/facet/classinherit.rb +1 -0
- data/lib/facet/classmethods.rb +1 -0
- data/lib/facet/cloneable.rb +1 -0
- data/lib/facet/comparable.rb +1 -5
- data/lib/facet/comparable/at_least.rb +1 -53
- data/lib/facet/comparable/at_most.rb +1 -2
- data/lib/facet/comparable/cap.rb +1 -2
- data/lib/facet/comparable/clip.rb +1 -72
- data/lib/facet/comparable/cmp.rb +1 -38
- data/lib/facet/consoleapp.rb +1 -0
- data/lib/facet/continuation.rb +1 -1
- data/lib/facet/continuation/self/create.rb +1 -71
- data/lib/facet/coroutine.rb +1 -0
- data/lib/facet/crypt.rb +1 -0
- data/lib/facet/date.rb +1 -0
- data/lib/facet/date/days_in_month.rb +1 -42
- data/lib/facet/date/days_of_month.rb +1 -34
- data/lib/facet/date/stamp.rb +1 -52
- data/lib/facet/date/to_date.rb +1 -36
- data/lib/facet/date/to_s.rb +1 -33
- data/lib/facet/date/to_time.rb +1 -36
- data/lib/facet/dictionary.rb +1 -0
- data/lib/facet/dir.rb +1 -4
- data/lib/facet/dir/self/ancestor.rb +1 -33
- data/lib/facet/dir/self/ascend.rb +1 -56
- data/lib/facet/dir/self/ls_r.rb +1 -76
- data/lib/facet/dir/self/recurse.rb +1 -2
- data/lib/facet/downloader.rb +1 -0
- data/lib/facet/enumerable.rb +1 -39
- data/lib/facet/enumerable/collect_with_counter.rb +1 -2
- data/lib/facet/enumerable/collect_with_index.rb +1 -51
- data/lib/facet/enumerable/commonality.rb +1 -63
- data/lib/facet/enumerable/compact_collect.rb +1 -50
- data/lib/facet/enumerable/compact_map.rb +1 -2
- data/lib/facet/enumerable/cross.rb +1 -44
- data/lib/facet/enumerable/each_by.rb +1 -2
- data/lib/facet/enumerable/each_combination.rb +1 -59
- data/lib/facet/enumerable/each_pair.rb +1 -50
- data/lib/facet/enumerable/each_permutation.rb +1 -2
- data/lib/facet/enumerable/each_slice.rb +1 -131
- data/lib/facet/enumerable/each_unique_pair.rb +1 -66
- data/lib/facet/enumerable/each_with_counter.rb +1 -8
- data/lib/facet/enumerable/elementwise.rb +1 -2
- data/lib/facet/enumerable/entropy.rb +1 -52
- data/lib/facet/enumerable/every.rb +1 -65
- data/lib/facet/enumerable/ew.rb +1 -64
- data/lib/facet/enumerable/filter_collect.rb +1 -58
- data/lib/facet/enumerable/filter_map.rb +1 -2
- data/lib/facet/enumerable/find_collisions.rb +1 -41
- data/lib/facet/enumerable/frequency.rb +1 -39
- data/lib/facet/enumerable/graph.rb +1 -59
- data/lib/facet/enumerable/ideal_entropy.rb +1 -49
- data/lib/facet/enumerable/map_with_counter.rb +1 -2
- data/lib/facet/enumerable/map_with_index.rb +1 -2
- data/lib/facet/enumerable/none.rb +1 -56
- data/lib/facet/enumerable/nonuniq.rb +1 -39
- data/lib/facet/enumerable/occur.rb +1 -62
- data/lib/facet/enumerable/one.rb +1 -65
- data/lib/facet/enumerable/op_pow.rb +1 -38
- data/lib/facet/enumerable/partition_by.rb +1 -55
- data/lib/facet/enumerable/permute.rb +1 -135
- data/lib/facet/enumerable/probability.rb +1 -59
- data/lib/facet/enumerable/project.rb +1 -11
- data/lib/facet/enumerable/self/combinations.rb +1 -68
- data/lib/facet/enumerable/self/cross.rb +1 -79
- data/lib/facet/enumerable/to_h.rb +1 -50
- data/lib/facet/enumerable/uniq_by.rb +1 -39
- data/lib/facet/enumerable/where.rb +1 -12
- data/lib/facet/enumerablepass.rb +1 -0
- data/lib/facet/expirable.rb +1 -0
- data/lib/facet/file.rb +1 -5
- data/lib/facet/file/self/create.rb +1 -52
- data/lib/facet/file/self/open_as_string.rb +1 -81
- data/lib/facet/file/self/read_list.rb +1 -47
- data/lib/facet/file/self/sanitize.rb +1 -50
- data/lib/facet/file/self/split_all.rb +1 -36
- data/lib/facet/filelist.rb +1 -0
- data/lib/facet/fileutils.rb +1 -0
- data/lib/facet/fileutils/head.rb +1 -2
- data/lib/facet/fileutils/safe_ln.rb +1 -38
- data/lib/facet/fileutils/slice.rb +1 -152
- data/lib/facet/fileutils/split_all.rb +1 -29
- data/lib/facet/fileutils/tail.rb +1 -2
- data/lib/facet/fileutils/wc.rb +1 -128
- data/lib/facet/fileutils/whereis.rb +1 -76
- data/lib/facet/fileutils/which.rb +1 -117
- data/lib/facet/float.rb +1 -3
- data/lib/facet/float/approx.rb +1 -42
- data/lib/facet/float/round_at.rb +1 -72
- data/lib/facet/float/round_to.rb +1 -73
- data/lib/facet/floatstring.rb +1 -0
- data/lib/facet/functor.rb +1 -41
- data/lib/facet/gem.rb +1 -3
- data/lib/facet/gem/self/active.rb +1 -9
- data/lib/facet/gem/self/gempath.rb +1 -10
- data/lib/facet/gem/self/gemspec.rb +1 -10
- data/lib/facet/hash.rb +1 -40
- data/lib/facet/hash/alias.rb +1 -54
- data/lib/facet/hash/assert_has_keys.rb +1 -38
- data/lib/facet/hash/assert_has_only_keys.rb +1 -38
- data/lib/facet/hash/at.rb +1 -26
- data/lib/facet/hash/collate.rb +1 -53
- data/lib/facet/hash/delete_unless.rb +1 -15
- data/lib/facet/hash/each.rb +1 -61
- data/lib/facet/hash/each_with_index.rb +1 -35
- data/lib/facet/hash/each_with_key.rb +1 -32
- data/lib/facet/hash/graph.rb +1 -31
- data/lib/facet/hash/has_keys.rb +1 -37
- data/lib/facet/hash/has_only_keys.rb +1 -37
- data/lib/facet/hash/inverse.rb +1 -51
- data/lib/facet/hash/join.rb +1 -19
- data/lib/facet/hash/keys_to_iv.rb +1 -60
- data/lib/facet/hash/keys_to_s.rb +1 -80
- data/lib/facet/hash/keys_to_sym.rb +1 -91
- data/lib/facet/hash/op_fetch.rb +1 -146
- data/lib/facet/hash/op_lshift.rb +1 -28
- data/lib/facet/hash/op_store.rb +1 -2
- data/lib/facet/hash/rand_key.rb +1 -47
- data/lib/facet/hash/rand_pair.rb +1 -50
- data/lib/facet/hash/rand_value.rb +1 -60
- data/lib/facet/hash/replace_each.rb +1 -33
- data/lib/facet/hash/self/zipnew.rb +1 -37
- data/lib/facet/hash/shuffle.rb +1 -53
- data/lib/facet/hash/slice.rb +1 -33
- data/lib/facet/hash/stringify_keys.rb +1 -2
- data/lib/facet/hash/swap.rb +1 -36
- data/lib/facet/hash/swapkey.rb +1 -44
- data/lib/facet/hash/symoblize_keys.rb +1 -2
- data/lib/facet/hash/to_h.rb +1 -35
- data/lib/facet/hash/to_ostruct.rb +1 -40
- data/lib/facet/hash/to_ostruct_recurse.rb +1 -45
- data/lib/facet/hash/traverse.rb +1 -63
- data/lib/facet/hash/update_each.rb +1 -43
- data/lib/facet/hash/update_keys.rb +1 -31
- data/lib/facet/hash/update_values.rb +1 -32
- data/lib/facet/hash/variablize_keys.rb +1 -2
- data/lib/facet/hash/weave.rb +1 -88
- data/lib/facet/heap.rb +1 -0
- data/lib/facet/inflect.rb +1 -70
- data/lib/facet/inheritor.rb +1 -0
- data/lib/facet/integer.rb +1 -9
- data/lib/facet/integer/even.rb +1 -2
- data/lib/facet/integer/fac.rb +1 -2
- data/lib/facet/integer/fact.rb +1 -2
- data/lib/facet/integer/factorial.rb +1 -61
- data/lib/facet/integer/multiple.rb +1 -69
- data/lib/facet/integer/odd.rb +1 -2
- data/lib/facet/integer/ordinal.rb +1 -41
- data/lib/facet/integer/times_collect.rb +1 -39
- data/lib/facet/integer/times_map.rb +1 -2
- data/lib/facet/interval.rb +1 -0
- data/lib/facet/ioredirect.rb +1 -0
- data/lib/facet/kernel.rb +1 -68
- data/lib/facet/kernel/__DIR__.rb +1 -11
- data/lib/facet/kernel/__class__.rb +1 -31
- data/lib/facet/kernel/__meta__.rb +1 -28
- data/lib/facet/kernel/adhoc.rb +1 -11
- data/lib/facet/kernel/as.rb +1 -91
- data/lib/facet/kernel/assign_from.rb +1 -82
- data/lib/facet/kernel/assign_with.rb +1 -75
- data/lib/facet/kernel/autoreload.rb +1 -124
- data/lib/facet/kernel/autoreload_files.rb +1 -2
- data/lib/facet/kernel/autoreload_glob.rb +1 -3
- data/lib/facet/kernel/bool.rb +1 -77
- data/lib/facet/kernel/bug.rb +1 -39
- data/lib/facet/kernel/call_stack.rb +1 -59
- data/lib/facet/kernel/called.rb +1 -40
- data/lib/facet/kernel/complete.rb +1 -6
- data/lib/facet/kernel/constant.rb +1 -52
- data/lib/facet/kernel/copy.rb +1 -48
- data/lib/facet/kernel/deep_copy.rb +1 -49
- data/lib/facet/kernel/demo.rb +1 -46
- data/lib/facet/kernel/eigenclass.rb +1 -17
- data/lib/facet/kernel/false.rb +1 -2
- data/lib/facet/kernel/fn.rb +1 -33
- data/lib/facet/kernel/generate_method_name.rb +1 -39
- data/lib/facet/kernel/get_by_id.rb +1 -33
- data/lib/facet/kernel/here.rb +1 -34
- data/lib/facet/kernel/in.rb +1 -35
- data/lib/facet/kernel/instance_assign.rb +1 -25
- data/lib/facet/kernel/instvar.rb +1 -13
- data/lib/facet/kernel/is.rb +1 -5
- data/lib/facet/kernel/maybe.rb +1 -41
- data/lib/facet/kernel/me.rb +1 -17
- data/lib/facet/kernel/meta.rb +1 -2
- data/lib/facet/kernel/metaclass.rb +1 -42
- data/lib/facet/kernel/method.rb +1 -76
- data/lib/facet/kernel/method_name.rb +1 -49
- data/lib/facet/kernel/methods.rb +1 -79
- data/lib/facet/kernel/nack.rb +1 -12
- data/lib/facet/kernel/new.rb +1 -40
- data/lib/facet/kernel/object_class.rb +1 -31
- data/lib/facet/kernel/object_hexid.rb +1 -35
- data/lib/facet/kernel/op_esc.rb +1 -37
- data/lib/facet/kernel/own.rb +1 -17
- data/lib/facet/kernel/p.rb +1 -12
- data/lib/facet/kernel/require_all.rb +1 -44
- data/lib/facet/kernel/require_esc.rb +1 -42
- data/lib/facet/kernel/require_facet.rb +1 -41
- data/lib/facet/kernel/require_local.rb +1 -25
- data/lib/facet/kernel/resc.rb +1 -32
- data/lib/facet/kernel/returning.rb +1 -49
- data/lib/facet/kernel/send_as.rb +1 -34
- data/lib/facet/kernel/set_from.rb +1 -69
- data/lib/facet/kernel/set_with.rb +1 -74
- data/lib/facet/kernel/silence_warnings.rb +1 -2
- data/lib/facet/kernel/silently.rb +1 -41
- data/lib/facet/kernel/singleton.rb +1 -47
- data/lib/facet/kernel/superior.rb +1 -53
- data/lib/facet/kernel/supermethod.rb +1 -44
- data/lib/facet/kernel/this.rb +1 -33
- data/lib/facet/kernel/to_b.rb +1 -41
- data/lib/facet/kernel/to_bool.rb +1 -47
- data/lib/facet/kernel/true.rb +1 -2
- data/lib/facet/kernel/unuri.rb +1 -2
- data/lib/facet/kernel/uri.rb +1 -38
- data/lib/facet/kernel/val.rb +1 -52
- data/lib/facet/kernel/warn_with_line.rb +1 -30
- data/lib/facet/kernel/with_accessor.rb +1 -94
- data/lib/facet/kernel/with_reader.rb +1 -2
- data/lib/facet/kernel/with_writer.rb +1 -2
- data/lib/facet/lisp.rb +1 -0
- data/lib/facet/lisp_format.rb +1 -0
- data/lib/facet/logger.rb +1 -0
- data/lib/facet/logger/format.rb +1 -28
- data/lib/facet/logger/format_message.rb +1 -1
- data/lib/facet/lrucache.rb +1 -0
- data/lib/facet/matchdata.rb +1 -3
- data/lib/facet/matchdata/match.rb +1 -37
- data/lib/facet/matchdata/matchset.rb +1 -2
- data/lib/facet/matchdata/matchtree.rb +1 -62
- data/lib/facet/mathconstants.rb +1 -0
- data/lib/facet/methodprobe.rb +1 -0
- data/lib/facet/mock.rb +1 -0
- data/lib/facet/module.rb +1 -40
- data/lib/facet/module/abstract.rb +1 -50
- data/lib/facet/module/alias_module_function.rb +1 -57
- data/lib/facet/module/ancestor.rb +1 -38
- data/lib/facet/module/attr.rb +1 -23
- data/lib/facet/module/attr_setter.rb +1 -48
- data/lib/facet/module/attr_tester.rb +1 -56
- data/lib/facet/module/basename.rb +1 -39
- data/lib/facet/module/by_name.rb +1 -68
- data/lib/facet/module/cattr_accessor.rb +1 -141
- data/lib/facet/module/cattr_reader.rb +1 -2
- data/lib/facet/module/cattr_writer.rb +1 -2
- data/lib/facet/module/clone_removing.rb +1 -2
- data/lib/facet/module/clone_renaming.rb +1 -2
- data/lib/facet/module/clone_using.rb +1 -87
- data/lib/facet/module/dirname.rb +1 -42
- data/lib/facet/module/equate_on.rb +1 -70
- data/lib/facet/module/generate_instance_method_name.rb +1 -47
- data/lib/facet/module/include_as.rb +1 -102
- data/lib/facet/module/initializer.rb +1 -65
- data/lib/facet/module/instance_methods.rb +1 -186
- data/lib/facet/module/integrate.rb +1 -51
- data/lib/facet/module/is.rb +1 -34
- data/lib/facet/module/memoize.rb +1 -95
- data/lib/facet/module/modspace.rb +1 -43
- data/lib/facet/module/namespace.rb +1 -61
- data/lib/facet/module/nesting.rb +1 -45
- data/lib/facet/module/redef.rb +1 -31
- data/lib/facet/module/redefine_method.rb +1 -57
- data/lib/facet/module/redirect.rb +1 -30
- data/lib/facet/module/redirect_method.rb +1 -64
- data/lib/facet/module/remove.rb +1 -29
- data/lib/facet/module/rename.rb +1 -31
- data/lib/facet/module/rename_method.rb +1 -38
- data/lib/facet/module/revisal.rb +1 -49
- data/lib/facet/module/shadow_all.rb +1 -17
- data/lib/facet/module/shadow_method.rb +1 -42
- data/lib/facet/module/sort_on.rb +1 -73
- data/lib/facet/module/undef.rb +1 -29
- data/lib/facet/module/wrap.rb +1 -30
- data/lib/facet/module/wrap_method.rb +1 -48
- data/lib/facet/multipliers.rb +1 -0
- data/lib/facet/multiton.rb +1 -0
- data/lib/facet/nackclass.rb +1 -0
- data/lib/facet/nil_as_emptiness.rb +1 -34
- data/lib/facet/nilclass.rb +1 -7
- data/lib/facet/nilclass/empty.rb +1 -32
- data/lib/facet/nilclass/include.rb +1 -32
- data/lib/facet/nilclass/length.rb +1 -2
- data/lib/facet/nilclass/op_fetch.rb +1 -37
- data/lib/facet/nilclass/size.rb +1 -44
- data/lib/facet/nilclass/to_f.rb +1 -32
- data/lib/facet/nilclass/to_h.rb +1 -33
- data/lib/facet/nilcomparable.rb +1 -0
- data/lib/facet/nullclass.rb +1 -0
- data/lib/facet/numeric.rb +1 -5
- data/lib/facet/numeric/ceil_multiple.rb +1 -40
- data/lib/facet/numeric/pred.rb +1 -2
- data/lib/facet/numeric/succ.rb +1 -69
- data/lib/facet/numeric/to_b.rb +1 -38
- data/lib/facet/one.rb +1 -0
- data/lib/facet/openobject.rb +1 -0
- data/lib/facet/orderedhash.rb +1 -0
- data/lib/facet/ormsupport.rb +1 -0
- data/lib/facet/ostruct.rb +1 -0
- data/lib/facet/ostruct/__merge__.rb +1 -52
- data/lib/facet/ostruct/__table__.rb +1 -17
- data/lib/facet/ostruct/__update__.rb +1 -45
- data/lib/facet/ostruct/op_fetch.rb +1 -40
- data/lib/facet/ostruct/op_store.rb +1 -42
- data/lib/facet/ostruct/to_h.rb +1 -8
- data/lib/facet/overload.rb +1 -0
- data/lib/facet/paramix.rb +1 -0
- data/lib/facet/pathlist.rb +1 -0
- data/lib/facet/pathname.rb +1 -2
- data/lib/facet/pathname/ascend.rb +1 -22
- data/lib/facet/pathname/descend.rb +1 -20
- data/lib/facet/pool.rb +1 -0
- data/lib/facet/preinitialize.rb +1 -0
- data/lib/facet/proc.rb +1 -3
- data/lib/facet/proc/compose.rb +1 -62
- data/lib/facet/proc/op_mul.rb +1 -2
- data/lib/facet/proc/to_method.rb +1 -74
- data/lib/facet/progressbar.rb +1 -0
- data/lib/facet/promoteself.rb +1 -0
- data/lib/facet/quaternion.rb +1 -0
- data/lib/facet/random.rb +1 -21
- data/lib/facet/range.rb +1 -4
- data/lib/facet/range/to_r.rb +1 -32
- data/lib/facet/range/to_range.rb +1 -36
- data/lib/facet/range/umbrella.rb +1 -67
- data/lib/facet/range/within.rb +1 -44
- data/lib/facet/reference.rb +1 -0
- data/lib/facet/regexp.rb +1 -3
- data/lib/facet/regexp/arity.rb +1 -41
- data/lib/facet/regexp/to_re.rb +1 -38
- data/lib/facet/regexp/to_regexp.rb +1 -36
- data/lib/facet/rtals.rb +1 -0
- data/lib/facet/semaphore.rb +1 -0
- data/lib/facet/snapshot.rb +1 -0
- data/lib/facet/stateparser.rb +1 -0
- data/lib/facet/statichash.rb +1 -0
- data/lib/facet/string.rb +1 -91
- data/lib/facet/string/align_center.rb +1 -87
- data/lib/facet/string/align_left.rb +1 -2
- data/lib/facet/string/align_right.rb +1 -2
- data/lib/facet/string/at.rb +1 -8
- data/lib/facet/string/at_rand.rb +1 -65
- data/lib/facet/string/basename.rb +1 -48
- data/lib/facet/string/blank.rb +1 -36
- data/lib/facet/string/bracket.rb +1 -70
- data/lib/facet/string/brief.rb +1 -33
- data/lib/facet/string/bytes.rb +1 -19
- data/lib/facet/string/camelcase.rb +1 -58
- data/lib/facet/string/camelize.rb +1 -35
- data/lib/facet/string/capitalized.rb +1 -38
- data/lib/facet/string/chars.rb +1 -34
- data/lib/facet/string/cmp.rb +1 -43
- data/lib/facet/string/demodulize.rb +1 -40
- data/lib/facet/string/dequote.rb +1 -29
- data/lib/facet/string/downcase.rb +1 -37
- data/lib/facet/string/dresner.rb +1 -49
- data/lib/facet/string/each_char.rb +1 -35
- data/lib/facet/string/each_word.rb +1 -50
- data/lib/facet/string/ends_with.rb +1 -2
- data/lib/facet/string/first.rb +1 -94
- data/lib/facet/string/first_char.rb +1 -20
- data/lib/facet/string/fold.rb +1 -67
- data/lib/facet/string/format.rb +1 -17
- data/lib/facet/string/frequency.rb +1 -39
- data/lib/facet/string/humanize.rb +1 -31
- data/lib/facet/string/indent.rb +1 -68
- data/lib/facet/string/index_all.rb +1 -40
- data/lib/facet/string/last.rb +1 -94
- data/lib/facet/string/last_char.rb +1 -22
- data/lib/facet/string/line_wrap.rb +1 -77
- data/lib/facet/string/lines.rb +1 -33
- data/lib/facet/string/lowercase.rb +1 -32
- data/lib/facet/string/margin.rb +1 -138
- data/lib/facet/string/methodize.rb +1 -44
- data/lib/facet/string/modulize.rb +1 -44
- data/lib/facet/string/mscan.rb +1 -42
- data/lib/facet/string/natcmp.rb +1 -108
- data/lib/facet/string/nchar.rb +1 -45
- data/lib/facet/string/ordinal.rb +1 -16
- data/lib/facet/string/pathize.rb +1 -46
- data/lib/facet/string/plural.rb +1 -2
- data/lib/facet/string/pop.rb +1 -54
- data/lib/facet/string/probability.rb +1 -34
- data/lib/facet/string/pull.rb +1 -17
- data/lib/facet/string/push.rb +1 -44
- data/lib/facet/string/quote.rb +1 -68
- data/lib/facet/string/rand_byte.rb +1 -55
- data/lib/facet/string/rand_index.rb +1 -29
- data/lib/facet/string/range.rb +1 -37
- data/lib/facet/string/range_all.rb +1 -48
- data/lib/facet/string/range_of_line.rb +1 -42
- data/lib/facet/string/regesc.rb +1 -32
- data/lib/facet/string/self/format.rb +1 -17
- data/lib/facet/string/self/interpolate.rb +1 -31
- data/lib/facet/string/self/patterns.rb +1 -51
- data/lib/facet/string/self/rand_letter.rb +1 -34
- data/lib/facet/string/self/random.rb +1 -37
- data/lib/facet/string/shatter.rb +1 -45
- data/lib/facet/string/shell_escape.rb +1 -0
- data/lib/facet/string/shift.rb +1 -42
- data/lib/facet/string/shuffle.rb +1 -42
- data/lib/facet/string/similarity.rb +1 -108
- data/lib/facet/string/singular.rb +1 -117
- data/lib/facet/string/slap.rb +1 -44
- data/lib/facet/string/soundex.rb +1 -102
- data/lib/facet/string/starts_with.rb +1 -24
- data/lib/facet/string/succ.rb +1 -38
- data/lib/facet/string/tab.rb +1 -2
- data/lib/facet/string/tabto.rb +1 -2
- data/lib/facet/string/to_a.rb +1 -35
- data/lib/facet/string/to_arr.rb +1 -55
- data/lib/facet/string/to_b.rb +1 -64
- data/lib/facet/string/to_const.rb +1 -39
- data/lib/facet/string/to_date.rb +1 -35
- data/lib/facet/string/to_proc.rb +1 -54
- data/lib/facet/string/to_re.rb +1 -46
- data/lib/facet/string/to_rx.rb +1 -2
- data/lib/facet/string/to_time.rb +1 -34
- data/lib/facet/string/unbracket.rb +1 -46
- data/lib/facet/string/underscore.rb +1 -18
- data/lib/facet/string/unix_crypt.rb +1 -41
- data/lib/facet/string/unpack.rb +1 -45
- data/lib/facet/string/unshift.rb +1 -42
- data/lib/facet/string/upcase.rb +1 -45
- data/lib/facet/string/uppercase.rb +1 -2
- data/lib/facet/string/whitespace.rb +1 -27
- data/lib/facet/string/word_filter.rb +1 -61
- data/lib/facet/string/word_wrap.rb +1 -90
- data/lib/facet/string/words.rb +1 -45
- data/lib/facet/string_as_array.rb +1 -16
- data/lib/facet/symbol.rb +1 -13
- data/lib/facet/symbol/camelcase.rb +1 -42
- data/lib/facet/symbol/camelize.rb +1 -33
- data/lib/facet/symbol/capitalize.rb +1 -31
- data/lib/facet/symbol/capitalized.rb +1 -33
- data/lib/facet/symbol/downcase.rb +1 -36
- data/lib/facet/symbol/not.rb +1 -40
- data/lib/facet/symbol/pad.rb +1 -43
- data/lib/facet/symbol/succ.rb +1 -42
- data/lib/facet/symbol/to_const.rb +1 -40
- data/lib/facet/symbol/to_proc.rb +1 -45
- data/lib/facet/symbol/to_str.rb +1 -39
- data/lib/facet/symbol/underscore.rb +1 -32
- data/lib/facet/symbol/upcase.rb +1 -37
- data/lib/facet/syncarray.rb +1 -0
- data/lib/facet/synchash.rb +1 -0
- data/lib/facet/system.rb +1 -0
- data/lib/facet/tagiterator.rb +1 -0
- data/lib/facet/time.rb +1 -8
- data/lib/facet/time/change.rb +1 -51
- data/lib/facet/time/elapse.rb +1 -41
- data/lib/facet/time/self/days_extrema.rb +1 -22
- data/lib/facet/time/self/stamp.rb +1 -2
- data/lib/facet/time/stamp.rb +1 -51
- data/lib/facet/time/to_date.rb +1 -37
- data/lib/facet/time/to_s.rb +1 -33
- data/lib/facet/time/to_time.rb +1 -36
- data/lib/facet/timer.rb +1 -0
- data/lib/facet/times.rb +1 -0
- data/lib/facet/tracepoint.rb +1 -0
- data/lib/facet/tuple.rb +1 -0
- data/lib/facet/typecast.rb +1 -0
- data/lib/facet/unboundmethod.rb +1 -1
- data/lib/facet/unboundmethod/name.rb +1 -8
- data/lib/facet/uninheritable.rb +1 -0
- data/lib/facet/units.rb +1 -0
- data/lib/facet/yamlstruct.rb +1 -0
- data/lib/facets.rb +77 -45
- data/lib/facets/core/array.rb +34 -0
- data/lib/facets/core/array/at_rand.rb +52 -0
- data/lib/facets/core/array/body.rb +2 -0
- data/lib/facets/core/array/delete_unless.rb +35 -0
- data/lib/facets/core/array/delete_values.rb +38 -0
- data/lib/facets/core/array/delete_values_at.rb +57 -0
- data/lib/facets/core/array/each_with_key.rb +9 -0
- data/lib/facets/core/array/first.rb +84 -0
- data/lib/facets/core/array/foot.rb +2 -0
- data/lib/facets/core/array/head.rb +80 -0
- data/lib/facets/core/array/last.rb +2 -0
- data/lib/facets/core/array/last_index.rb +35 -0
- data/lib/facets/core/array/merge.rb +40 -0
- data/lib/facets/core/array/mid.rb +50 -0
- data/lib/facets/core/array/middle.rb +45 -0
- data/lib/facets/core/array/op_fetch.rb +40 -0
- data/lib/facets/core/array/op_mod.rb +16 -0
- data/lib/facets/core/array/op_store.rb +47 -0
- data/lib/facets/core/array/pick.rb +68 -0
- data/lib/facets/core/array/pos.rb +39 -0
- data/lib/facets/core/array/pull.rb +37 -0
- data/lib/facets/core/array/rand_index.rb +30 -0
- data/lib/facets/core/array/rand_subset.rb +56 -0
- data/lib/facets/core/array/range.rb +47 -0
- data/lib/facets/core/array/rotate.rb +67 -0
- data/lib/facets/core/array/select.rb +37 -0
- data/lib/facets/core/array/shuffle.rb +66 -0
- data/lib/facets/core/array/slap.rb +37 -0
- data/lib/facets/core/array/store.rb +7 -0
- data/lib/facets/core/array/tail.rb +2 -0
- data/lib/facets/core/array/thru.rb +38 -0
- data/lib/facets/core/array/to_b.rb +31 -0
- data/lib/facets/core/array/to_h.rb +57 -0
- data/lib/facets/core/array/to_hash.rb +46 -0
- data/lib/facets/core/array/unzip.rb +20 -0
- data/lib/facets/core/binding.rb +14 -0
- data/lib/facets/core/binding/__DIR__.rb +2 -0
- data/lib/facets/core/binding/__FILE__.rb +2 -0
- data/lib/facets/core/binding/__LINE__.rb +58 -0
- data/lib/facets/core/binding/call_stack.rb +42 -0
- data/lib/facets/core/binding/called.rb +42 -0
- data/lib/facets/core/binding/caller.rb +42 -0
- data/lib/facets/core/binding/defined.rb +41 -0
- data/lib/facets/core/binding/eval.rb +42 -0
- data/lib/facets/core/binding/local_variables.rb +45 -0
- data/lib/facets/core/binding/method_name.rb +48 -0
- data/lib/facets/core/binding/op_fetch.rb +45 -0
- data/lib/facets/core/binding/op_store.rb +46 -0
- data/lib/facets/core/binding/self.rb +41 -0
- data/lib/facets/core/binding/self/of_caller.rb +122 -0
- data/lib/facets/core/class.rb +7 -0
- data/lib/facets/core/class/by_name.rb +1 -0
- data/lib/facets/core/class/descendents.rb +44 -0
- data/lib/facets/core/class/method_name.rb +33 -0
- data/lib/facets/core/class/remove_descendents.rb +46 -0
- data/lib/facets/core/class/remove_subclasses.rb +2 -0
- data/lib/facets/core/class/subclasses.rb +2 -0
- data/lib/facets/core/class/unix_path.rb +34 -0
- data/lib/facets/core/comparable.rb +5 -0
- data/lib/facets/core/comparable/at_least.rb +53 -0
- data/lib/facets/core/comparable/at_most.rb +2 -0
- data/lib/facets/core/comparable/cap.rb +2 -0
- data/lib/facets/core/comparable/clip.rb +72 -0
- data/lib/facets/core/comparable/cmp.rb +38 -0
- data/lib/facets/core/continuation.rb +1 -0
- data/lib/facets/core/continuation/self/create.rb +71 -0
- data/lib/facets/core/date.rb +0 -0
- data/lib/facets/core/date/days_in_month.rb +42 -0
- data/lib/facets/core/date/days_of_month.rb +34 -0
- data/lib/facets/core/date/stamp.rb +52 -0
- data/lib/facets/core/date/to_date.rb +36 -0
- data/lib/facets/core/date/to_s.rb +33 -0
- data/lib/facets/core/date/to_time.rb +36 -0
- data/lib/facets/core/dir.rb +4 -0
- data/lib/facets/core/dir/self/ancestor.rb +33 -0
- data/lib/facets/core/dir/self/ascend.rb +56 -0
- data/lib/facets/core/dir/self/ls_r.rb +76 -0
- data/lib/facets/core/dir/self/recurse.rb +2 -0
- data/lib/facets/core/enumerable.rb +39 -0
- data/lib/facets/core/enumerable/collect_with_counter.rb +2 -0
- data/lib/facets/core/enumerable/collect_with_index.rb +51 -0
- data/lib/facets/core/enumerable/commonality.rb +63 -0
- data/lib/facets/core/enumerable/compact_collect.rb +50 -0
- data/lib/facets/core/enumerable/compact_map.rb +2 -0
- data/lib/facets/core/enumerable/cross.rb +44 -0
- data/lib/facets/core/enumerable/each_by.rb +2 -0
- data/lib/facets/core/enumerable/each_combination.rb +59 -0
- data/lib/facets/core/enumerable/each_pair.rb +50 -0
- data/lib/facets/core/enumerable/each_permutation.rb +2 -0
- data/lib/facets/core/enumerable/each_slice.rb +131 -0
- data/lib/facets/core/enumerable/each_unique_pair.rb +66 -0
- data/lib/facets/core/enumerable/each_with_counter.rb +8 -0
- data/lib/facets/core/enumerable/elementwise.rb +2 -0
- data/lib/facets/core/enumerable/entropy.rb +52 -0
- data/lib/facets/core/enumerable/every.rb +65 -0
- data/lib/facets/core/enumerable/ew.rb +64 -0
- data/lib/facets/core/enumerable/filter_collect.rb +58 -0
- data/lib/facets/core/enumerable/filter_map.rb +2 -0
- data/lib/facets/core/enumerable/find_collisions.rb +41 -0
- data/lib/facets/core/enumerable/frequency.rb +39 -0
- data/lib/facets/core/enumerable/graph.rb +59 -0
- data/lib/facets/core/enumerable/ideal_entropy.rb +49 -0
- data/lib/facets/core/enumerable/map_with_counter.rb +2 -0
- data/lib/facets/core/enumerable/map_with_index.rb +2 -0
- data/lib/facets/core/enumerable/none.rb +56 -0
- data/lib/facets/core/enumerable/nonuniq.rb +39 -0
- data/lib/facets/core/enumerable/occur.rb +62 -0
- data/lib/facets/core/enumerable/one.rb +65 -0
- data/lib/facets/core/enumerable/op_pow.rb +38 -0
- data/lib/facets/core/enumerable/partition_by.rb +55 -0
- data/lib/facets/core/enumerable/permute.rb +135 -0
- data/lib/facets/core/enumerable/probability.rb +59 -0
- data/lib/facets/core/enumerable/project.rb +11 -0
- data/lib/facets/core/enumerable/self/combinations.rb +68 -0
- data/lib/facets/core/enumerable/self/cross.rb +79 -0
- data/lib/facets/core/enumerable/to_h.rb +50 -0
- data/lib/facets/core/enumerable/uniq_by.rb +39 -0
- data/lib/facets/core/enumerable/where.rb +12 -0
- data/lib/facets/core/file.rb +5 -0
- data/lib/facets/core/file/self/create.rb +52 -0
- data/lib/facets/core/file/self/open_as_string.rb +81 -0
- data/lib/facets/core/file/self/read_list.rb +47 -0
- data/lib/facets/core/file/self/sanitize.rb +50 -0
- data/lib/facets/core/file/self/split_all.rb +36 -0
- data/lib/facets/core/fileutils.rb +0 -0
- data/lib/facets/core/fileutils/head.rb +2 -0
- data/lib/facets/core/fileutils/safe_ln.rb +38 -0
- data/lib/facets/core/fileutils/slice.rb +152 -0
- data/lib/facets/core/fileutils/split_all.rb +29 -0
- data/lib/facets/core/fileutils/tail.rb +2 -0
- data/lib/facets/core/fileutils/wc.rb +128 -0
- data/lib/facets/core/fileutils/whereis.rb +76 -0
- data/lib/facets/core/fileutils/which.rb +117 -0
- data/lib/facets/core/float.rb +3 -0
- data/lib/facets/core/float/approx.rb +42 -0
- data/lib/facets/core/float/round_at.rb +72 -0
- data/lib/facets/core/float/round_to.rb +73 -0
- data/lib/facets/core/gem.rb +3 -0
- data/lib/facets/core/gem/self/active.rb +9 -0
- data/lib/facets/core/gem/self/gempath.rb +10 -0
- data/lib/facets/core/gem/self/gemspec.rb +10 -0
- data/lib/facets/core/hash.rb +40 -0
- data/lib/facets/core/hash/alias.rb +54 -0
- data/lib/facets/core/hash/assert_has_keys.rb +38 -0
- data/lib/facets/core/hash/assert_has_only_keys.rb +38 -0
- data/lib/facets/core/hash/at.rb +26 -0
- data/lib/facets/core/hash/collate.rb +53 -0
- data/lib/facets/core/hash/delete_unless.rb +15 -0
- data/lib/facets/core/hash/each.rb +61 -0
- data/lib/facets/core/hash/each_with_index.rb +35 -0
- data/lib/facets/core/hash/each_with_key.rb +32 -0
- data/lib/facets/core/hash/graph.rb +31 -0
- data/lib/facets/core/hash/has_keys.rb +37 -0
- data/lib/facets/core/hash/has_only_keys.rb +37 -0
- data/lib/facets/core/hash/inverse.rb +51 -0
- data/lib/facets/core/hash/join.rb +19 -0
- data/lib/facets/core/hash/keys_to_iv.rb +60 -0
- data/lib/facets/core/hash/keys_to_s.rb +80 -0
- data/lib/facets/core/hash/keys_to_sym.rb +91 -0
- data/lib/facets/core/hash/op_fetch.rb +146 -0
- data/lib/facets/core/hash/op_lshift.rb +28 -0
- data/lib/facets/core/hash/op_store.rb +2 -0
- data/lib/facets/core/hash/rand_key.rb +47 -0
- data/lib/facets/core/hash/rand_pair.rb +50 -0
- data/lib/facets/core/hash/rand_value.rb +60 -0
- data/lib/facets/core/hash/replace_each.rb +33 -0
- data/lib/facets/core/hash/self/zipnew.rb +37 -0
- data/lib/facets/core/hash/shuffle.rb +53 -0
- data/lib/facets/core/hash/slice.rb +33 -0
- data/lib/facets/core/hash/stringify_keys.rb +2 -0
- data/lib/facets/core/hash/swap.rb +36 -0
- data/lib/facets/core/hash/swapkey.rb +44 -0
- data/lib/facets/core/hash/symoblize_keys.rb +2 -0
- data/lib/facets/core/hash/to_h.rb +35 -0
- data/lib/facets/core/hash/to_ostruct.rb +40 -0
- data/lib/facets/core/hash/to_ostruct_recurse.rb +45 -0
- data/lib/facets/core/hash/traverse.rb +63 -0
- data/lib/facets/core/hash/update_each.rb +43 -0
- data/lib/facets/core/hash/update_keys.rb +31 -0
- data/lib/facets/core/hash/update_values.rb +32 -0
- data/lib/facets/core/hash/variablize_keys.rb +2 -0
- data/lib/facets/core/hash/weave.rb +88 -0
- data/lib/facets/core/inflect.rb +70 -0
- data/lib/facets/core/integer.rb +9 -0
- data/lib/facets/core/integer/even.rb +2 -0
- data/lib/facets/core/integer/fac.rb +2 -0
- data/lib/facets/core/integer/fact.rb +2 -0
- data/lib/facets/core/integer/factorial.rb +61 -0
- data/lib/facets/core/integer/multiple.rb +69 -0
- data/lib/facets/core/integer/odd.rb +2 -0
- data/lib/facets/core/integer/ordinal.rb +41 -0
- data/lib/facets/core/integer/times_collect.rb +39 -0
- data/lib/facets/core/integer/times_map.rb +2 -0
- data/lib/facets/core/kernel.rb +68 -0
- data/lib/facets/core/kernel/__DIR__.rb +11 -0
- data/lib/facets/core/kernel/__class__.rb +31 -0
- data/lib/facets/core/kernel/__meta__.rb +28 -0
- data/lib/facets/core/kernel/adhoc.rb +11 -0
- data/lib/facets/core/kernel/as.rb +87 -0
- data/lib/facets/core/kernel/assign_from.rb +82 -0
- data/lib/facets/core/kernel/assign_with.rb +75 -0
- data/lib/facets/core/kernel/autoreload.rb +124 -0
- data/lib/facets/core/kernel/autoreload_files.rb +2 -0
- data/lib/facets/core/kernel/autoreload_glob.rb +3 -0
- data/lib/facets/core/kernel/bool.rb +77 -0
- data/lib/facets/core/kernel/bug.rb +39 -0
- data/lib/facets/core/kernel/call_stack.rb +59 -0
- data/lib/facets/core/kernel/called.rb +40 -0
- data/lib/facets/core/kernel/complete.rb +6 -0
- data/lib/facets/core/kernel/constant.rb +52 -0
- data/lib/facets/core/kernel/copy.rb +48 -0
- data/lib/facets/core/kernel/deep_copy.rb +49 -0
- data/lib/facets/core/kernel/demo.rb +46 -0
- data/lib/facets/core/kernel/eigenclass.rb +17 -0
- data/lib/facets/core/kernel/false.rb +2 -0
- data/lib/facets/core/kernel/fn.rb +33 -0
- data/lib/facets/core/kernel/generate_method_name.rb +39 -0
- data/lib/facets/core/kernel/get_by_id.rb +33 -0
- data/lib/facets/core/kernel/here.rb +34 -0
- data/lib/facets/core/kernel/in.rb +35 -0
- data/lib/facets/core/kernel/instance_assign.rb +25 -0
- data/lib/facets/core/kernel/instvar.rb +13 -0
- data/lib/facets/core/kernel/is.rb +7 -0
- data/lib/facets/core/kernel/maybe.rb +41 -0
- data/lib/facets/core/kernel/me.rb +17 -0
- data/lib/facets/core/kernel/meta.rb +2 -0
- data/lib/facets/core/kernel/metaclass.rb +42 -0
- data/lib/facets/core/kernel/method.rb +76 -0
- data/lib/facets/core/kernel/method_name.rb +49 -0
- data/lib/facets/core/kernel/methods.rb +79 -0
- data/lib/facets/core/kernel/nack.rb +12 -0
- data/lib/facets/core/kernel/new.rb +40 -0
- data/lib/facets/core/kernel/object_class.rb +31 -0
- data/lib/facets/core/kernel/object_hexid.rb +35 -0
- data/lib/facets/core/kernel/op_esc.rb +37 -0
- data/lib/facets/core/kernel/own.rb +17 -0
- data/lib/facets/core/kernel/p.rb +12 -0
- data/lib/facets/core/kernel/require_all.rb +44 -0
- data/lib/facets/core/kernel/require_esc.rb +42 -0
- data/lib/facets/core/kernel/require_facet.rb +41 -0
- data/lib/facets/core/kernel/require_local.rb +25 -0
- data/lib/facets/core/kernel/resc.rb +32 -0
- data/lib/facets/core/kernel/returning.rb +49 -0
- data/lib/facets/core/kernel/send_as.rb +34 -0
- data/lib/facets/core/kernel/set_from.rb +69 -0
- data/lib/facets/core/kernel/set_with.rb +74 -0
- data/lib/facets/core/kernel/silence_warnings.rb +2 -0
- data/lib/facets/core/kernel/silently.rb +41 -0
- data/lib/facets/core/kernel/singleton.rb +47 -0
- data/lib/facets/core/kernel/superior.rb +53 -0
- data/lib/facets/core/kernel/supermethod.rb +44 -0
- data/lib/facets/core/kernel/this.rb +33 -0
- data/lib/facets/core/kernel/to_b.rb +41 -0
- data/lib/facets/core/kernel/to_bool.rb +47 -0
- data/lib/facets/core/kernel/true.rb +2 -0
- data/lib/facets/core/kernel/unuri.rb +2 -0
- data/lib/facets/core/kernel/uri.rb +38 -0
- data/lib/facets/core/kernel/val.rb +52 -0
- data/lib/facets/core/kernel/warn_with_line.rb +30 -0
- data/lib/facets/core/kernel/with_accessor.rb +94 -0
- data/lib/facets/core/kernel/with_reader.rb +2 -0
- data/lib/facets/core/kernel/with_writer.rb +2 -0
- data/lib/facets/core/logger.rb +0 -0
- data/lib/facets/core/logger/format.rb +28 -0
- data/lib/facets/core/logger/format_message.rb +1 -0
- data/lib/facets/core/matchdata.rb +3 -0
- data/lib/facets/core/matchdata/match.rb +37 -0
- data/lib/facets/core/matchdata/matchset.rb +2 -0
- data/lib/facets/core/matchdata/matchtree.rb +62 -0
- data/lib/facets/core/module.rb +40 -0
- data/lib/facets/core/module/abstract.rb +50 -0
- data/lib/facets/core/module/alias_module_function.rb +57 -0
- data/lib/facets/core/module/ancestor.rb +38 -0
- data/lib/facets/core/module/attr.rb +23 -0
- data/lib/facets/core/module/attr_setter.rb +48 -0
- data/lib/facets/core/module/attr_tester.rb +56 -0
- data/lib/facets/core/module/basename.rb +39 -0
- data/lib/facets/core/module/by_name.rb +68 -0
- data/lib/facets/core/module/cattr_accessor.rb +141 -0
- data/lib/facets/core/module/cattr_reader.rb +2 -0
- data/lib/facets/core/module/cattr_writer.rb +2 -0
- data/lib/facets/core/module/clone_removing.rb +2 -0
- data/lib/facets/core/module/clone_renaming.rb +2 -0
- data/lib/facets/core/module/clone_using.rb +87 -0
- data/lib/facets/core/module/dirname.rb +42 -0
- data/lib/facets/core/module/equate_on.rb +70 -0
- data/lib/facets/core/module/generate_instance_method_name.rb +47 -0
- data/lib/facets/core/module/include_as.rb +102 -0
- data/lib/facets/core/module/initializer.rb +65 -0
- data/lib/facets/core/module/instance_methods.rb +186 -0
- data/lib/facets/core/module/integrate.rb +51 -0
- data/lib/facets/core/module/is.rb +35 -0
- data/lib/facets/core/module/memoize.rb +95 -0
- data/lib/facets/core/module/modspace.rb +43 -0
- data/lib/facets/core/module/namespace.rb +61 -0
- data/lib/facets/core/module/nesting.rb +45 -0
- data/lib/facets/core/module/redef.rb +31 -0
- data/lib/facets/core/module/redefine_method.rb +57 -0
- data/lib/facets/core/module/redirect.rb +30 -0
- data/lib/facets/core/module/redirect_method.rb +64 -0
- data/lib/facets/core/module/remove.rb +29 -0
- data/lib/facets/core/module/rename.rb +31 -0
- data/lib/facets/core/module/rename_method.rb +38 -0
- data/lib/facets/core/module/revisal.rb +49 -0
- data/lib/facets/core/module/shadow_all.rb +17 -0
- data/lib/facets/core/module/shadow_method.rb +42 -0
- data/lib/facets/core/module/sort_on.rb +73 -0
- data/lib/facets/core/module/undef.rb +29 -0
- data/lib/facets/core/module/wrap.rb +30 -0
- data/lib/facets/core/module/wrap_method.rb +48 -0
- data/lib/facets/core/nil_as_emptiness.rb +34 -0
- data/lib/facets/core/nilclass.rb +7 -0
- data/lib/facets/core/nilclass/empty.rb +32 -0
- data/lib/facets/core/nilclass/include.rb +32 -0
- data/lib/facets/core/nilclass/length.rb +2 -0
- data/lib/facets/core/nilclass/op_fetch.rb +37 -0
- data/lib/facets/core/nilclass/size.rb +44 -0
- data/lib/facets/core/nilclass/to_f.rb +32 -0
- data/lib/facets/core/nilclass/to_h.rb +33 -0
- data/lib/facets/core/numeric.rb +5 -0
- data/lib/facets/core/numeric/ceil_multiple.rb +40 -0
- data/lib/facets/core/numeric/pred.rb +2 -0
- data/lib/facets/core/numeric/succ.rb +69 -0
- data/lib/facets/core/numeric/to_b.rb +38 -0
- data/lib/facets/core/ostruct.rb +0 -0
- data/lib/facets/core/ostruct/__merge__.rb +52 -0
- data/lib/facets/core/ostruct/__table__.rb +17 -0
- data/lib/facets/core/ostruct/__update__.rb +45 -0
- data/lib/facets/core/ostruct/op_fetch.rb +40 -0
- data/lib/facets/core/ostruct/op_store.rb +42 -0
- data/lib/facets/core/ostruct/to_h.rb +8 -0
- data/lib/facets/core/pathname.rb +2 -0
- data/lib/facets/core/pathname/ascend.rb +22 -0
- data/lib/facets/core/pathname/descend.rb +20 -0
- data/lib/facets/core/proc.rb +3 -0
- data/lib/facets/core/proc/compose.rb +62 -0
- data/lib/facets/core/proc/op_mul.rb +2 -0
- data/lib/facets/core/proc/to_method.rb +74 -0
- data/lib/facets/core/random.rb +21 -0
- data/lib/facets/core/range.rb +4 -0
- data/lib/facets/core/range/to_r.rb +32 -0
- data/lib/facets/core/range/to_range.rb +36 -0
- data/lib/facets/core/range/umbrella.rb +67 -0
- data/lib/facets/core/range/within.rb +44 -0
- data/lib/facets/core/regexp.rb +3 -0
- data/lib/facets/core/regexp/arity.rb +41 -0
- data/lib/facets/core/regexp/to_re.rb +38 -0
- data/lib/facets/core/regexp/to_regexp.rb +36 -0
- data/lib/facets/core/string.rb +92 -0
- data/lib/facets/core/string/align_center.rb +87 -0
- data/lib/facets/core/string/align_left.rb +2 -0
- data/lib/facets/core/string/align_right.rb +2 -0
- data/lib/facets/core/string/at.rb +8 -0
- data/lib/facets/core/string/at_rand.rb +65 -0
- data/lib/facets/core/string/basename.rb +48 -0
- data/lib/facets/core/string/blank.rb +36 -0
- data/lib/facets/core/string/bracket.rb +70 -0
- data/lib/facets/core/string/brief.rb +33 -0
- data/lib/facets/core/string/bytes.rb +19 -0
- data/lib/facets/core/string/camelcase.rb +58 -0
- data/lib/facets/core/string/camelize.rb +35 -0
- data/lib/facets/core/string/capitalized.rb +38 -0
- data/lib/facets/core/string/chars.rb +34 -0
- data/lib/facets/core/string/cmp.rb +43 -0
- data/lib/facets/core/string/demodulize.rb +40 -0
- data/lib/facets/core/string/dequote.rb +29 -0
- data/lib/facets/core/string/downcase.rb +37 -0
- data/lib/facets/core/string/dresner.rb +49 -0
- data/lib/facets/core/string/each_char.rb +35 -0
- data/lib/facets/core/string/each_word.rb +50 -0
- data/lib/facets/core/string/ends_with.rb +2 -0
- data/lib/facets/core/string/first.rb +94 -0
- data/lib/facets/core/string/first_char.rb +20 -0
- data/lib/facets/core/string/fold.rb +67 -0
- data/lib/facets/core/string/format.rb +17 -0
- data/lib/facets/core/string/frequency.rb +39 -0
- data/lib/facets/core/string/humanize.rb +31 -0
- data/lib/facets/core/string/indent.rb +68 -0
- data/lib/facets/core/string/index_all.rb +40 -0
- data/lib/facets/core/string/last.rb +94 -0
- data/lib/facets/core/string/last_char.rb +22 -0
- data/lib/facets/core/string/line_wrap.rb +77 -0
- data/lib/facets/core/string/lines.rb +33 -0
- data/lib/facets/core/string/lowercase.rb +32 -0
- data/lib/facets/core/string/margin.rb +138 -0
- data/lib/facets/core/string/methodize.rb +44 -0
- data/lib/facets/core/string/modulize.rb +44 -0
- data/lib/facets/core/string/mscan.rb +42 -0
- data/lib/facets/core/string/natcmp.rb +108 -0
- data/lib/facets/core/string/nchar.rb +45 -0
- data/lib/facets/core/string/ordinal.rb +16 -0
- data/lib/facets/core/string/pathize.rb +46 -0
- data/lib/facets/core/string/plural.rb +2 -0
- data/lib/facets/core/string/pop.rb +54 -0
- data/lib/facets/core/string/probability.rb +34 -0
- data/lib/facets/core/string/pull.rb +17 -0
- data/lib/facets/core/string/push.rb +44 -0
- data/lib/facets/core/string/quote.rb +68 -0
- data/lib/facets/core/string/rand_byte.rb +55 -0
- data/lib/facets/core/string/rand_index.rb +29 -0
- data/lib/facets/core/string/range.rb +37 -0
- data/lib/facets/core/string/range_all.rb +48 -0
- data/lib/facets/core/string/range_of_line.rb +42 -0
- data/lib/facets/core/string/regesc.rb +32 -0
- data/lib/facets/core/string/self/format.rb +17 -0
- data/lib/facets/core/string/self/interpolate.rb +31 -0
- data/lib/facets/core/string/self/patterns.rb +51 -0
- data/lib/facets/core/string/self/rand_letter.rb +34 -0
- data/lib/facets/core/string/self/random.rb +37 -0
- data/lib/facets/core/string/shatter.rb +45 -0
- data/lib/facets/core/string/shell_escape.rb +11 -0
- data/lib/facets/core/string/shift.rb +42 -0
- data/lib/facets/core/string/shuffle.rb +42 -0
- data/lib/facets/core/string/similarity.rb +108 -0
- data/lib/facets/core/string/singular.rb +117 -0
- data/lib/facets/core/string/slap.rb +44 -0
- data/lib/facets/core/string/soundex.rb +102 -0
- data/lib/facets/core/string/starts_with.rb +24 -0
- data/lib/facets/core/string/succ.rb +38 -0
- data/lib/facets/core/string/tab.rb +2 -0
- data/lib/facets/core/string/tabto.rb +2 -0
- data/lib/facets/core/string/to_a.rb +35 -0
- data/lib/facets/core/string/to_arr.rb +55 -0
- data/lib/facets/core/string/to_b.rb +64 -0
- data/lib/facets/core/string/to_const.rb +39 -0
- data/lib/facets/core/string/to_date.rb +35 -0
- data/lib/facets/core/string/to_proc.rb +54 -0
- data/lib/facets/core/string/to_re.rb +46 -0
- data/lib/facets/core/string/to_rx.rb +2 -0
- data/lib/facets/core/string/to_time.rb +34 -0
- data/lib/facets/core/string/unbracket.rb +46 -0
- data/lib/facets/core/string/underscore.rb +18 -0
- data/lib/facets/core/string/unix_crypt.rb +41 -0
- data/lib/facets/core/string/unpack.rb +45 -0
- data/lib/facets/core/string/unshift.rb +42 -0
- data/lib/facets/core/string/upcase.rb +45 -0
- data/lib/facets/core/string/uppercase.rb +2 -0
- data/lib/facets/core/string/whitespace.rb +27 -0
- data/lib/facets/core/string/word_filter.rb +61 -0
- data/lib/facets/core/string/word_wrap.rb +90 -0
- data/lib/facets/core/string/words.rb +45 -0
- data/lib/facets/core/string_as_array.rb +16 -0
- data/lib/facets/core/symbol.rb +13 -0
- data/lib/facets/core/symbol/camelcase.rb +42 -0
- data/lib/facets/core/symbol/camelize.rb +33 -0
- data/lib/facets/core/symbol/capitalize.rb +31 -0
- data/lib/facets/core/symbol/capitalized.rb +33 -0
- data/lib/facets/core/symbol/downcase.rb +36 -0
- data/lib/facets/core/symbol/not.rb +40 -0
- data/lib/facets/core/symbol/pad.rb +43 -0
- data/lib/facets/core/symbol/succ.rb +42 -0
- data/lib/facets/core/symbol/to_const.rb +40 -0
- data/lib/facets/core/symbol/to_proc.rb +45 -0
- data/lib/facets/core/symbol/to_str.rb +39 -0
- data/lib/facets/core/symbol/underscore.rb +32 -0
- data/lib/facets/core/symbol/upcase.rb +37 -0
- data/lib/facets/core/time.rb +8 -0
- data/lib/facets/core/time/change.rb +51 -0
- data/lib/facets/core/time/elapse.rb +41 -0
- data/lib/facets/core/time/self/days_extrema.rb +22 -0
- data/lib/facets/core/time/self/stamp.rb +2 -0
- data/lib/facets/core/time/stamp.rb +51 -0
- data/lib/facets/core/time/to_date.rb +37 -0
- data/lib/facets/core/time/to_s.rb +33 -0
- data/lib/facets/core/time/to_time.rb +36 -0
- data/lib/facets/core/unboundmethod.rb +1 -0
- data/lib/facets/core/unboundmethod/name.rb +8 -0
- data/lib/facets/more/annotation.rb +580 -0
- data/lib/facets/more/ansicode.rb +311 -0
- data/lib/facets/more/aspects.rb +230 -0
- data/lib/facets/more/association.rb +156 -0
- data/lib/facets/more/basicobject.rb +135 -0
- data/lib/facets/more/bbcode.rb +388 -0
- data/lib/facets/more/binaryreader.rb +246 -0
- data/lib/facets/more/bitmask.rb +112 -0
- data/lib/facets/more/blankslate.rb +3 -0
- data/lib/facets/more/bytes.rb +212 -0
- data/lib/facets/more/classinherit.rb +216 -0
- data/lib/facets/more/classmethods.rb +219 -0
- data/lib/facets/more/cloneable.rb +55 -0
- data/lib/facets/more/consoleapp.rb +123 -0
- data/lib/facets/more/coroutine.rb +172 -0
- data/lib/facets/more/crypt.rb +180 -0
- data/lib/facets/more/dictionary.rb +436 -0
- data/lib/facets/more/downloader.rb +272 -0
- data/lib/facets/more/enumerablepass.rb +285 -0
- data/lib/facets/more/expirable.rb +87 -0
- data/lib/facets/more/filelist.rb +486 -0
- data/lib/facets/more/floatstring.rb +179 -0
- data/lib/facets/more/functor.rb +105 -0
- data/lib/facets/more/heap.rb +226 -0
- data/lib/facets/more/inheritor.rb +239 -0
- data/lib/facets/more/interval.rb +424 -0
- data/lib/facets/more/ioredirect.rb +130 -0
- data/lib/facets/more/lisp.rb +387 -0
- data/lib/facets/more/lisp_format.rb +1843 -0
- data/lib/facets/more/lrucache.rb +183 -0
- data/lib/facets/more/mathconstants.rb +320 -0
- data/lib/facets/more/methodprobe.rb +215 -0
- data/lib/facets/more/mock.rb +233 -0
- data/lib/facets/more/multipliers.rb +192 -0
- data/lib/facets/more/multiton.rb +359 -0
- data/lib/facets/more/nackclass.rb +162 -0
- data/lib/facets/more/nilcomparable.rb +133 -0
- data/lib/facets/more/nullclass.rb +68 -0
- data/lib/facets/more/one.rb +117 -0
- data/lib/facets/more/openobject.rb +220 -0
- data/lib/facets/more/orderedhash.rb +3 -0
- data/lib/facets/more/ormsupport.rb +235 -0
- data/lib/facets/more/overload.rb +110 -0
- data/lib/facets/more/paramix.rb +243 -0
- data/lib/facets/more/pathlist.rb +105 -0
- data/lib/facets/more/pool.rb +98 -0
- data/lib/facets/more/preinitialize.rb +152 -0
- data/lib/facets/more/progressbar.rb +227 -0
- data/lib/facets/more/promoteself.rb +104 -0
- data/lib/facets/more/quaternion.rb +527 -0
- data/lib/facets/more/reference.rb +83 -0
- data/lib/facets/more/rtals.rb +14 -0
- data/lib/facets/more/semaphore.rb +103 -0
- data/lib/facets/more/snapshot.rb +190 -0
- data/lib/facets/more/stateparser.rb +579 -0
- data/lib/facets/more/statichash.rb +87 -0
- data/lib/facets/more/syncarray.rb +144 -0
- data/lib/facets/more/synchash.rb +173 -0
- data/lib/facets/more/system.rb +169 -0
- data/lib/facets/more/tagiterator.rb +387 -0
- data/lib/facets/more/timer.rb +307 -0
- data/lib/facets/more/times.rb +327 -0
- data/lib/facets/more/tracepoint.rb +183 -0
- data/lib/facets/more/tuple.rb +170 -0
- data/lib/facets/more/typecast.rb +280 -0
- data/lib/facets/more/uninheritable.rb +90 -0
- data/lib/facets/more/units.rb +938 -0
- data/lib/facets/more/yamlstruct.rb +90 -0
- metadata +680 -39
- data/data/facets/methods.yaml +0 -446
@@ -0,0 +1,938 @@
|
|
1
|
+
#--
|
2
|
+
# Units
|
3
|
+
#
|
4
|
+
# Copyright (c) 2005 Peter Vanbroekhoven & Thomas Sawyer
|
5
|
+
#
|
6
|
+
# Ruby License
|
7
|
+
#
|
8
|
+
# This module is free software. You may use, modify, and/or redistribute this
|
9
|
+
# software under the same terms as Ruby.
|
10
|
+
#
|
11
|
+
# This program is distributed in the hope that it will be useful, but WITHOUT
|
12
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13
|
+
# FOR A PARTICULAR PURPOSE.
|
14
|
+
#
|
15
|
+
# ==========================================================================
|
16
|
+
# Revision History ::
|
17
|
+
# --------------------------------------------------------------------------
|
18
|
+
# 2005.10.01 peter Finished
|
19
|
+
# 2005.08.01 trans Started
|
20
|
+
# ==========================================================================
|
21
|
+
#
|
22
|
+
#++
|
23
|
+
|
24
|
+
# :title: Units
|
25
|
+
#
|
26
|
+
# This is a very extensive SI units system.
|
27
|
+
#
|
28
|
+
# == Usage
|
29
|
+
#
|
30
|
+
# Here are some examples of usage.
|
31
|
+
#
|
32
|
+
# 1.bit/s + 8.bytes/s
|
33
|
+
# (1.bit/s).to(byte/s)
|
34
|
+
# 1.mile.to(feet)
|
35
|
+
# 1.acre.to(yd**2)
|
36
|
+
# 1.acre.to(sq_yd)
|
37
|
+
# 1.gallon.to(self.L)
|
38
|
+
# 1.lb.to(kg)
|
39
|
+
# 1.m.s.to(m.s)
|
40
|
+
# 1.sq_mi.to(km**2)
|
41
|
+
# 1.mile.to(km)
|
42
|
+
# 1.usd.to(twd)
|
43
|
+
#
|
44
|
+
# == Author(s)
|
45
|
+
#
|
46
|
+
# * Peter Vanbroekhoven
|
47
|
+
# * Thomas Sawyer
|
48
|
+
#
|
49
|
+
|
50
|
+
require 'rbconfig'
|
51
|
+
require 'soap/wsdlDriver'
|
52
|
+
require 'yaml'
|
53
|
+
|
54
|
+
# this is needed until RubyGems provides fix to access data path
|
55
|
+
require 'facet/gem/self/gempath'
|
56
|
+
|
57
|
+
# The namespace for all unit related classes. Mixing this in has the additional effect
|
58
|
+
# of making Units.with_unit_converter available without the <code>Units.</code> prefix,
|
59
|
+
# as well as the shortcuts for creating Units (see Units#method_missing).
|
60
|
+
|
61
|
+
module Units
|
62
|
+
|
63
|
+
def method_missing(m, *args, &blk)
|
64
|
+
if args.length == 1
|
65
|
+
args[0] = Units::Converter.converter(args[0]) if not args[0].is_a? Units::Converter
|
66
|
+
return Units::Unit.new({m => 1}, args[0]) if args[0].registered?(m)
|
67
|
+
elsif Units::Converter.current.registered?(m)
|
68
|
+
raise ArgumentError, "Wrong number of arguments" if args.length != 0
|
69
|
+
return Units::Unit.new({m => 1}, Units::Converter.current)
|
70
|
+
end
|
71
|
+
super
|
72
|
+
end
|
73
|
+
|
74
|
+
# Executes the block with the current Converter changed to
|
75
|
+
# the given Converter. This allows to temporarily change the
|
76
|
+
# Converter used by default.
|
77
|
+
#
|
78
|
+
# Example:
|
79
|
+
#
|
80
|
+
# with_unit_converter(:uk) {
|
81
|
+
# puts 1.cwt.to(lb) # => 112.0 lb
|
82
|
+
# }
|
83
|
+
#
|
84
|
+
# with_unit_converter(:us) {
|
85
|
+
# puts 1.cwt.to(lb) # => 100.0 lb
|
86
|
+
# }
|
87
|
+
#
|
88
|
+
# See also Converter.current.
|
89
|
+
def with_unit_converter(name, &blk) # :yields:
|
90
|
+
Units::Converter.with_converter(name, &blk)
|
91
|
+
end
|
92
|
+
|
93
|
+
module_function :with_unit_converter
|
94
|
+
|
95
|
+
# This class represents a Unit. A Unit uses a given Converter with
|
96
|
+
# a number of registered Units in which it can be expressed.
|
97
|
+
# A Unit is the product of the powers of other Units. In principle, these
|
98
|
+
# need not be integer powers, but this may cause problems with rounding.
|
99
|
+
# The following code for example returns +false+:
|
100
|
+
# Unit.new(:m => 0.1) * Unit.new(:m => 0.2) == Unit.new(:m => 0.3)
|
101
|
+
#
|
102
|
+
# Units can be multiplied and divided, or raised to a given power.
|
103
|
+
# This can only be done when both units use the same Converter.
|
104
|
+
# As an extra, 1 can be divided by a Unit.
|
105
|
+
#
|
106
|
+
# Examples:
|
107
|
+
#
|
108
|
+
# Unit.new(:mi => 1, :s => -1) ** 2 # => mi**2/s**2
|
109
|
+
# Unit.new(:mi => 1, :s => -1) * Unit.new(:s => 1, :usd => -1) # => mi/usd
|
110
|
+
# Unit.new(:mi => 1, :s => -1, Converter.converter(:uk)) *
|
111
|
+
# Unit.new(:s => 1, :usd => -1, Converter.converter(:us)) # => TypeError
|
112
|
+
# 1 / Unit.new(:mi => 1, :s => -1) # => s/mi
|
113
|
+
class Unit
|
114
|
+
|
115
|
+
attr_reader :units, :converter
|
116
|
+
|
117
|
+
# Creates a new (composite) Unit.
|
118
|
+
# It is passed a hash of the form <code>{:unit => exponent}</code>, and the
|
119
|
+
# Converter to use.
|
120
|
+
#
|
121
|
+
# Examples:
|
122
|
+
#
|
123
|
+
# Unit.new(:m => 1, :s => -1, Converter.converter(:uk)) # => m/s
|
124
|
+
# Unit.new(:mi => 1, :s => -2) # => mi/s**2
|
125
|
+
#
|
126
|
+
# See also Converter, Converter.converter
|
127
|
+
def initialize(units = {}, converter = Units::Converter.current)
|
128
|
+
@units, @converter = {}, converter
|
129
|
+
units.each_pair { |k, v| @units[k.to_sym] = v }
|
130
|
+
check_units
|
131
|
+
end
|
132
|
+
|
133
|
+
# Raises to the given power.
|
134
|
+
def **(p)
|
135
|
+
result = {}
|
136
|
+
@units.each_pair do |u, e|
|
137
|
+
result[u] = e * p
|
138
|
+
end
|
139
|
+
Units::Unit.new(result, @converter)
|
140
|
+
end
|
141
|
+
|
142
|
+
# Multiplies with the given Unit.
|
143
|
+
def *(other)
|
144
|
+
do_op(:*, :+, other)
|
145
|
+
end
|
146
|
+
|
147
|
+
# Divides by the given Unit.
|
148
|
+
def /(other)
|
149
|
+
do_op(:/, :-, other)
|
150
|
+
end
|
151
|
+
|
152
|
+
# Returns +true+ iff this Unit has all exponents equal to 0.
|
153
|
+
def unitless?
|
154
|
+
@units.empty?
|
155
|
+
end
|
156
|
+
|
157
|
+
def coerce(other) # :nodoc:
|
158
|
+
return [self, Units::Unit.new({}, converter)] if other == 1
|
159
|
+
raise TypeError, "incompatible converters" if converter != other.converter
|
160
|
+
converter.coerce_units(self, other)
|
161
|
+
end
|
162
|
+
|
163
|
+
# Returns +true+ iff the two Units are equals, <i>i.e.,</i> iff they have
|
164
|
+
# the same exponent for all units, and they both use the same Converter.
|
165
|
+
def ==(other)
|
166
|
+
other.is_a?(Units::Unit) && other.units == units && other.converter == converter
|
167
|
+
end
|
168
|
+
|
169
|
+
# Returns +true+ iff this Unit is compatible with the given
|
170
|
+
# Unit. This is less strict than equality because for example hours are
|
171
|
+
# compatible with seconds ("we can add them"), but hours are not seconds.
|
172
|
+
def compatible_with?(other)
|
173
|
+
return false if converter != other.converter
|
174
|
+
conv1, conv2 = converter.coerce_units(self, other)
|
175
|
+
conv1.units == conv2.units
|
176
|
+
end
|
177
|
+
|
178
|
+
# Returns a human readable string representation.
|
179
|
+
def to_s
|
180
|
+
numerator = ""
|
181
|
+
denominator = ""
|
182
|
+
@units.each_pair do |u, e|
|
183
|
+
(e > 0 ? numerator : denominator) << " #{u.to_s}#{"**#{e.abs}" if e.abs != 1}"
|
184
|
+
end
|
185
|
+
"#{numerator.lstrip}#{"/" + denominator.lstrip if not denominator.empty?}"
|
186
|
+
end
|
187
|
+
|
188
|
+
alias inspect to_s
|
189
|
+
|
190
|
+
def method_missing(m, *args, &blk)
|
191
|
+
if converter.registered?(m)
|
192
|
+
raise ArgumentError, "Wrong number of arguments" if args.length != 0
|
193
|
+
return Units::Unit.new({m => 1}, converter) * self
|
194
|
+
end
|
195
|
+
super
|
196
|
+
end
|
197
|
+
|
198
|
+
private
|
199
|
+
|
200
|
+
def check_units
|
201
|
+
@units.keys.each do |unit|
|
202
|
+
raise ArgumentError, "unit #{unit.to_s.dump} not registered" if not @converter.registered? unit
|
203
|
+
@units.delete(unit) if @units[unit] == 0
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
def do_op(op, dual_op, other)
|
208
|
+
raise TypeError, "incompatible converters for #{op}" if converter != other.converter
|
209
|
+
result = @units.dup
|
210
|
+
other.units.each_pair do |u, e|
|
211
|
+
result[u] = 0 if not result[u]
|
212
|
+
result[u] = result[u].send(dual_op, e)
|
213
|
+
end
|
214
|
+
Units::Unit.new(result, @converter)
|
215
|
+
end
|
216
|
+
|
217
|
+
end
|
218
|
+
|
219
|
+
# This class represents a Value with a numeric value and a Unit.
|
220
|
+
# The numeric value can be any Numeric, though it is not recommended
|
221
|
+
# to use Values.
|
222
|
+
#
|
223
|
+
# A Value can be added to, subtracted from and multiplied with another value,
|
224
|
+
# though only when both Values are using the same Converter. While multiplication
|
225
|
+
# is always possible, adding or subtracting values with incompatible Units
|
226
|
+
# results in a TypeError. When two Units are compatible but not the same,
|
227
|
+
# the Value with the larger of the Units is converted to the smaller of the Units.
|
228
|
+
# For example adding 100 seconds and 1 minute, the latter is converted to 60 seconds
|
229
|
+
# because a second is smaller than a minute. The result is 160 seconds.
|
230
|
+
class Value < Numeric
|
231
|
+
|
232
|
+
# The numeric value of this Value.
|
233
|
+
attr_reader :value
|
234
|
+
# The Unit of this value.
|
235
|
+
attr_reader :unit
|
236
|
+
|
237
|
+
# Creates a new Value with the given numeric value and the given unit.
|
238
|
+
# Simply returns the given value if the given unit is valueless,
|
239
|
+
# <i>i.e.,</i> when <code>unit.unitless?</code> is true.
|
240
|
+
def self.new(value, unit)
|
241
|
+
return value if unit.unitless?
|
242
|
+
super
|
243
|
+
end
|
244
|
+
|
245
|
+
def initialize(value, unit) # :nodoc:
|
246
|
+
@value, @unit = value, unit
|
247
|
+
end
|
248
|
+
|
249
|
+
# Returns the Converter used by this Value's Unit.
|
250
|
+
# Equivalent to <code>unit.converter</code>.
|
251
|
+
def converter
|
252
|
+
unit.converter
|
253
|
+
end
|
254
|
+
|
255
|
+
def *(other) # :nodoc:
|
256
|
+
do_multiplicative_op(:*, other)
|
257
|
+
end
|
258
|
+
|
259
|
+
def /(other) # :nodoc:
|
260
|
+
do_multiplicative_op(:/, other)
|
261
|
+
end
|
262
|
+
|
263
|
+
def +(other) # :nodoc:
|
264
|
+
do_additive_op(:+, other)
|
265
|
+
end
|
266
|
+
|
267
|
+
def -(other) # :nodoc:
|
268
|
+
do_additive_op(:-, other)
|
269
|
+
end
|
270
|
+
|
271
|
+
def ==(other) # :nodoc:
|
272
|
+
(self - other).value == 0.0
|
273
|
+
end
|
274
|
+
|
275
|
+
# Converts this Value to the given Unit.
|
276
|
+
# This only works if the Converters used by this Value's Unit
|
277
|
+
# and the given Unit are the same. It obviously fails if the
|
278
|
+
# Units are not compatible (can't add apples and oranges).
|
279
|
+
def to(to_unit)
|
280
|
+
conv1, conv2 = unit.coerce(to_unit)
|
281
|
+
raise TypeError, "incompatible units for operation" if conv1.units != conv2.units
|
282
|
+
mult = conv1.multiplier / conv2.multiplier
|
283
|
+
Value.new(value * mult, to_unit)
|
284
|
+
end
|
285
|
+
|
286
|
+
def coerce(other) # :nodoc:
|
287
|
+
[self, Value.new(other, Units::Unit.new({}, @unit.converter))]
|
288
|
+
end
|
289
|
+
|
290
|
+
# Returns a human readable string representation.
|
291
|
+
def to_s
|
292
|
+
"#{@value} #{@unit}"
|
293
|
+
end
|
294
|
+
|
295
|
+
alias inspect to_s
|
296
|
+
|
297
|
+
def method_missing(m, *args, &blk)
|
298
|
+
if self.converter.registered?(m)
|
299
|
+
raise ArgumentError, "Wrong number of arguments" if args.length != 0
|
300
|
+
return self * Value.new(1, Units::Unit.new({m => 1}, self.converter))
|
301
|
+
end
|
302
|
+
super
|
303
|
+
end
|
304
|
+
|
305
|
+
private
|
306
|
+
|
307
|
+
def do_additive_op(op, other)
|
308
|
+
if other.is_a? Value
|
309
|
+
conv1, conv2 = unit.coerce(other.unit)
|
310
|
+
raise TypeError, "incompatible units for #{op}" if conv1.units != conv2.units
|
311
|
+
mult = conv2.multiplier / conv1.multiplier
|
312
|
+
if mult > 1
|
313
|
+
Value.new(value.send(op, other.value * mult), unit)
|
314
|
+
else
|
315
|
+
Value.new(other.value.send(op, value * mult), other.unit)
|
316
|
+
end
|
317
|
+
else
|
318
|
+
raise TypeError, "incompatible operands for #{op}"
|
319
|
+
end
|
320
|
+
end
|
321
|
+
|
322
|
+
def do_multiplicative_op(op, other)
|
323
|
+
if other.is_a? Value
|
324
|
+
Value.new(value.send(op, other.value), unit.send(op, other.unit))
|
325
|
+
elsif other.is_a? Units::Unit
|
326
|
+
Value.new(value, unit.send(op, other))
|
327
|
+
else
|
328
|
+
Value.new(value.send(op, other), unit)
|
329
|
+
end
|
330
|
+
end
|
331
|
+
|
332
|
+
end
|
333
|
+
|
334
|
+
# This class handles all conversions between Units.
|
335
|
+
#
|
336
|
+
# There are two kinds of Units; those that are not expressed as a function
|
337
|
+
# of other Units --called base units-- and those that are expressed
|
338
|
+
# as a function of other Units --called derived units. The former kind is
|
339
|
+
# registered without specifying how it depends on other Units, while the latter
|
340
|
+
# kind is registered with doing so.
|
341
|
+
#
|
342
|
+
# This class also registers a list of Converters that are generally useable.
|
343
|
+
# The default Converter which is used when none is specified, can be retrieved
|
344
|
+
# with Converter.current. Converters can be registered with Converter.register.
|
345
|
+
#
|
346
|
+
# Converters can be loaded from YAML. This allows Converters to be specified in
|
347
|
+
# configuration files.
|
348
|
+
class Converter
|
349
|
+
|
350
|
+
# Encapsulates a service for retrieving exchange rates.
|
351
|
+
# This is used by Converter.register_currency.
|
352
|
+
# An instance of this class behaves like a Numeric when used
|
353
|
+
# in calculations. This class is used to look up exchange rates
|
354
|
+
# lazily.
|
355
|
+
#
|
356
|
+
# This class is not supposed to be instantiated by itself. Instead a
|
357
|
+
# subclass should be created that defines the method +get_rate+.
|
358
|
+
# The only subclass provided is currently XMethods.
|
359
|
+
#
|
360
|
+
# To be found automatically from YAML files, exchange services should
|
361
|
+
# be located under Units::Converter::ExchangeRate.
|
362
|
+
class ExchangeRate
|
363
|
+
|
364
|
+
def self.create_conversion(curr, converter) # :nodoc:
|
365
|
+
{:unit => Units::Unit.new({%s{--base-currency--} => 1}, converter), :multiplier => self.new(curr)}
|
366
|
+
end
|
367
|
+
|
368
|
+
def initialize(curr) # :nodoc:
|
369
|
+
@curr = curr
|
370
|
+
end
|
371
|
+
|
372
|
+
# This method should be overridden in subclasses to return the
|
373
|
+
# exchange rate represented by this object. The unit in question
|
374
|
+
# is available as a String in the instance variable <code>@curr</code>.
|
375
|
+
# The rate should be calculated against an arbitrary but fixed base currency.
|
376
|
+
# The rate should be such that the following would be true
|
377
|
+
# 1 * curr = rate * base_curr
|
378
|
+
# This function should return +nil+ if the currency is not supported by this
|
379
|
+
# retrieval service. It should <em>not</em> raise an exception.
|
380
|
+
def get_rate
|
381
|
+
raise NoMethodError, "undefined method `get_rate' for #{to_s}:#{self.class}"
|
382
|
+
end
|
383
|
+
|
384
|
+
def value # :nodoc:
|
385
|
+
@value ||= get_rate or raise TypeError, "currency not supported by current service: #{@curr.to_s.dump}"
|
386
|
+
end
|
387
|
+
|
388
|
+
def coerce(other) # :nodoc:
|
389
|
+
[value, other]
|
390
|
+
end
|
391
|
+
|
392
|
+
def *(other) # :nodoc:
|
393
|
+
value * other
|
394
|
+
end
|
395
|
+
|
396
|
+
def /(other) # :nodoc:
|
397
|
+
value / other
|
398
|
+
end
|
399
|
+
|
400
|
+
def +(other) # :nodoc:
|
401
|
+
value + other
|
402
|
+
end
|
403
|
+
|
404
|
+
def -(other) # :nodoc:
|
405
|
+
value - other
|
406
|
+
end
|
407
|
+
|
408
|
+
def **(other) # :nodoc:
|
409
|
+
value ** other
|
410
|
+
end
|
411
|
+
|
412
|
+
# Exchange rate retrieval service that uses a service from http://www.xmethods.com.
|
413
|
+
# See http://rubyurl.com/7uq.
|
414
|
+
class XMethods < ExchangeRate
|
415
|
+
|
416
|
+
# This is the only method that a subclass of ExchangeRate
|
417
|
+
# needs to implement. This is a good example to follow.
|
418
|
+
def get_rate
|
419
|
+
driver.getRate(country_mapping[@curr], country_mapping[base])
|
420
|
+
rescue
|
421
|
+
nil
|
422
|
+
end
|
423
|
+
|
424
|
+
private
|
425
|
+
|
426
|
+
def data
|
427
|
+
@@data ||= YAML.load_file(File.join(Units::Config::DATADIR, 'xmethods', 'mapping.yaml'))
|
428
|
+
end
|
429
|
+
|
430
|
+
def country_mapping
|
431
|
+
@@country_mapping ||= data[:mapping]
|
432
|
+
end
|
433
|
+
|
434
|
+
def base
|
435
|
+
@@base ||= data[:base]
|
436
|
+
end
|
437
|
+
|
438
|
+
def driver
|
439
|
+
@@driver ||= SOAP::WSDLDriverFactory.new("http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl").create_rpc_driver #.createDriver
|
440
|
+
end
|
441
|
+
|
442
|
+
end
|
443
|
+
|
444
|
+
# Cached values for the XMethods exchange rate service.
|
445
|
+
class CachedXMethods < ExchangeRate
|
446
|
+
|
447
|
+
# This is the only method that a subclass of ExchangeRate
|
448
|
+
# needs to implement. This is a good example to follow.
|
449
|
+
def get_rate
|
450
|
+
data[@curr]
|
451
|
+
end
|
452
|
+
|
453
|
+
private
|
454
|
+
|
455
|
+
def data
|
456
|
+
@@data ||= YAML.load_file(File.join(Units::Config::DATADIR, 'xmethods', 'cached.yaml'))
|
457
|
+
end
|
458
|
+
|
459
|
+
end
|
460
|
+
|
461
|
+
end
|
462
|
+
|
463
|
+
Conversion = Struct.new(:units, :multiplier) # :nodoc:
|
464
|
+
|
465
|
+
class Conversion # :nodoc:
|
466
|
+
|
467
|
+
def **(p)
|
468
|
+
Conversion.new(units ** p, multiplier ** p)
|
469
|
+
end
|
470
|
+
|
471
|
+
def *(m)
|
472
|
+
Conversion.new(units * m.units, multiplier * m.multiplier)
|
473
|
+
end
|
474
|
+
|
475
|
+
def /(m)
|
476
|
+
Conversion.new(units / m.units, multiplier / m.multiplier)
|
477
|
+
end
|
478
|
+
|
479
|
+
end
|
480
|
+
|
481
|
+
# The prefixes used for SI units. See also Converter#register_si_unit.
|
482
|
+
SI_PREFIXES = {
|
483
|
+
'yotta' => {:abbrev => 'Y', :multiplier => 1e24},
|
484
|
+
'zetta' => {:abbrev => 'Z', :multiplier => 1e21},
|
485
|
+
'exa' => {:abbrev => 'E', :multiplier => 1e18},
|
486
|
+
'peta' => {:abbrev => 'P', :multiplier => 1e14},
|
487
|
+
'tera' => {:abbrev => 'T', :multiplier => 1e12},
|
488
|
+
'giga' => {:abbrev => 'G', :multiplier => 1e9},
|
489
|
+
'mega' => {:abbrev => 'M', :multiplier => 1e6},
|
490
|
+
'kilo' => {:abbrev => 'k', :multiplier => 1e3},
|
491
|
+
'hecto' => {:abbrev => 'h', :multiplier => 1e2},
|
492
|
+
'deca' => {:abbrev => 'da', :multiplier => 1e1},
|
493
|
+
'deci' => {:abbrev => 'd', :multiplier => 1e-1},
|
494
|
+
'centi' => {:abbrev => 'c', :multiplier => 1e-2},
|
495
|
+
'milli' => {:abbrev => 'm', :multiplier => 1e-3},
|
496
|
+
'micro' => {:abbrev => 'u', :multiplier => 1e-6},
|
497
|
+
'nano' => {:abbrev => 'n', :multiplier => 1e-9},
|
498
|
+
'pico' => {:abbrev => 'p', :multiplier => 1e-12},
|
499
|
+
'femto' => {:abbrev => 'f', :multiplier => 1e-15},
|
500
|
+
'atto' => {:abbrev => 'a', :multiplier => 1e-18},
|
501
|
+
'zepto' => {:abbrev => 'z', :multiplier => 1e-21},
|
502
|
+
'yocto' => {:abbrev => 'y', :multiplier => 1e-24}
|
503
|
+
}
|
504
|
+
|
505
|
+
# The prefixes used for binary units. See also Converter#register_binary_unit.
|
506
|
+
BINARY_PREFIXES = {
|
507
|
+
'yotta' => {:abbrev => 'Y', :multiplier => 1024.0 ** 8},
|
508
|
+
'zetta' => {:abbrev => 'Z', :multiplier => 1024.0 ** 7},
|
509
|
+
'exa' => {:abbrev => 'E', :multiplier => 1024.0 ** 6},
|
510
|
+
'peta' => {:abbrev => 'P', :multiplier => 1024.0 ** 5},
|
511
|
+
'tera' => {:abbrev => 'T', :multiplier => 1024.0 ** 4},
|
512
|
+
'giga' => {:abbrev => 'G', :multiplier => 1024.0 ** 3},
|
513
|
+
'mega' => {:abbrev => 'M', :multiplier => 1024.0 ** 2},
|
514
|
+
'kilo' => {:abbrev => 'k', :multiplier => 1024.0},
|
515
|
+
}
|
516
|
+
|
517
|
+
# The prefixes used for length units. See also Converter#register_length_unit.
|
518
|
+
LENGTH_PREFIXES = {
|
519
|
+
'square_' => {:abbrev => 'sq_', :power => 2},
|
520
|
+
'cubic_' => {:abbrev => 'cu_', :power => 3}
|
521
|
+
}
|
522
|
+
|
523
|
+
# Creates a new Converter, where the data is loaded from the
|
524
|
+
# file with the given file name.
|
525
|
+
def self.from_yaml(file)
|
526
|
+
Units::Converter.new {
|
527
|
+
load_yaml(file)
|
528
|
+
}
|
529
|
+
end
|
530
|
+
|
531
|
+
# Creates a new Converter. If a block is given,
|
532
|
+
# it is executed in the newly created Converter's context.
|
533
|
+
def initialize(&blk)
|
534
|
+
@loaded_yaml = []
|
535
|
+
@conversions = {}
|
536
|
+
instance_eval(&blk) if blk
|
537
|
+
end
|
538
|
+
|
539
|
+
# Checks whether the unit with the given name is registered.
|
540
|
+
# The name can be a symbol or a string.
|
541
|
+
def registered?(unit)
|
542
|
+
unit = unit.to_sym
|
543
|
+
conversions(unit) != nil
|
544
|
+
end
|
545
|
+
|
546
|
+
# Returns the list of registered unit names as symbols.
|
547
|
+
def registered_units
|
548
|
+
@conversions.keys
|
549
|
+
end
|
550
|
+
|
551
|
+
# Registers a new Unit with the given name. The +data+ parameter
|
552
|
+
# is a Hash with some extra parameters (can be Strings or Symbols):
|
553
|
+
# +alias+:: Specifies possible aliases for the Unit.
|
554
|
+
# +abbrev+:: Specifies possible abbreviations or symbols for the Unit.
|
555
|
+
# The differences with aliases is that prefixes work differently;
|
556
|
+
# see +register_si_unit+ and +register_binary_unit+.
|
557
|
+
# +equals+:: If present, specifies how the Unit depends on other Units.
|
558
|
+
# The value for this key can either be a Hash with +unit+ mapping to
|
559
|
+
# a Unit and +multiplier+ mapping to a numeric multiplier, or
|
560
|
+
# a String containing the multiplier, followed by a space, followed by
|
561
|
+
# a representation of the Unit as returned by Unit#to_s.
|
562
|
+
#
|
563
|
+
# Examples:
|
564
|
+
#
|
565
|
+
# converter.register_unit(:pint, :alias => :pints, :abbrev => [:pt, :pts]))
|
566
|
+
# converter.register_unit(:quart, 'alias' => :quarts, :abbrev => ['qt', :qts], :equals => '2.0 pt'))
|
567
|
+
# converter.register_unit(:gallon, :alias => :gallons, :abbrev => :gal, 'equals' => {:unit => Unit.new('qt' => 1, converter), 'multiplier' => 4.0))
|
568
|
+
#
|
569
|
+
# Note that Symbols and Strings are generally exchangeable within this
|
570
|
+
# library (internally they are converted to Symbols). The number one reason
|
571
|
+
# for this is that String look better in YAML.
|
572
|
+
#
|
573
|
+
# See also +register_si_unit+, +register_binary_unit+, +register_length_unit+
|
574
|
+
# and +register_currency+.
|
575
|
+
def register_unit(name, data = {})
|
576
|
+
unit, aliases, abbrevs = extract_data(name, data, :to_sym)
|
577
|
+
conversion = data[:equals]
|
578
|
+
conversion = decode_conversion(conversion) if conversion
|
579
|
+
conversion = convert_conversion(conversion[:unit].units, conversion[:multiplier]) if conversion
|
580
|
+
register_unit_internal(unit, conversion)
|
581
|
+
conversion = convert_conversion({unit => 1}, 1) if not conversion
|
582
|
+
(aliases + abbrevs).each do |u|
|
583
|
+
register_unit_internal(u, conversion)
|
584
|
+
end
|
585
|
+
end
|
586
|
+
|
587
|
+
# Registers a new SI unit. The Unit and its aliases are registered with
|
588
|
+
# all available SI prefixes (see SI_PREFIXES) as well, while the
|
589
|
+
# abbreviations are registered with the abbreviated version of the
|
590
|
+
# prefixes.
|
591
|
+
#
|
592
|
+
# For the syntax of the options, see +register_unit+.
|
593
|
+
def register_si_unit(unit, data = {})
|
594
|
+
register_prefixed_unit(unit, SI_PREFIXES, data)
|
595
|
+
end
|
596
|
+
|
597
|
+
# Registers a new binary unit. The Unit and its aliases are registered with
|
598
|
+
# all available binary prefixes (see BINARY_PREFIXES) as well, while the
|
599
|
+
# abbreviations are registered with the abbreviated version of the
|
600
|
+
# prefixes.
|
601
|
+
#
|
602
|
+
# For the syntax of the options, see +register_unit+.
|
603
|
+
def register_binary_unit(unit, data = {})
|
604
|
+
register_prefixed_unit(unit, BINARY_PREFIXES, data)
|
605
|
+
end
|
606
|
+
|
607
|
+
# Registers a new length unit. The Unit and its aliases are registered with
|
608
|
+
# all available length prefixes (see LENGTH_PREFIXES) as well, while the
|
609
|
+
# abbreviations are registered with the abbreviated version of the
|
610
|
+
# prefixes.
|
611
|
+
#
|
612
|
+
# For the syntax of the options, see +register_unit+.
|
613
|
+
def register_length_unit(unit, data = {})
|
614
|
+
register_prefixed_unit(unit, LENGTH_PREFIXES, data)
|
615
|
+
end
|
616
|
+
|
617
|
+
# Registers a given currency, with a given servive to find the exchange
|
618
|
+
# rate between the given currency and a given base currency. The service
|
619
|
+
# should be a subclass of ExchangeRate. This is not strictly necessary,
|
620
|
+
# but ExchangeRate handles all of the magic.
|
621
|
+
def register_currency(curr, service = nil)
|
622
|
+
service ||= Units::Config::DEFAULT_CURRENCY_SERVICE
|
623
|
+
register_unit(curr, :equals => service.create_conversion(curr, self))
|
624
|
+
end
|
625
|
+
|
626
|
+
def coerce_units(unit1, unit2) # :nodoc:
|
627
|
+
[convert_conversion(unit1.units), convert_conversion(unit2.units)]
|
628
|
+
end
|
629
|
+
|
630
|
+
def method_missing(m, *args, &blk)
|
631
|
+
if registered?(m)
|
632
|
+
raise ArgumentError, "Wrong number of arguments" if args.length != 0
|
633
|
+
return Units::Unit.new({m => 1}, self)
|
634
|
+
end
|
635
|
+
super
|
636
|
+
end
|
637
|
+
|
638
|
+
# Loads data from the YAML file with the given name.
|
639
|
+
# Returns +self+.
|
640
|
+
def load_yaml(file)
|
641
|
+
return if @loaded_yaml.include? file
|
642
|
+
data = YAML.load_file(file)
|
643
|
+
@loaded_yaml << file
|
644
|
+
old_service = Thread.current['current_currency_exchange_service']
|
645
|
+
data.each do |r|
|
646
|
+
rule = {}
|
647
|
+
r.each_pair do |k, v|
|
648
|
+
rule[k.to_sym] = v
|
649
|
+
end
|
650
|
+
case rule[:type]
|
651
|
+
when 'import'
|
652
|
+
load_yaml(File.join(Units::Config::DATADIR, rule[:file] + '.yaml'))
|
653
|
+
when 'si'
|
654
|
+
register_si_unit(rule[:name], rule)
|
655
|
+
when 'unit'
|
656
|
+
register_unit(rule[:name], rule)
|
657
|
+
when 'length'
|
658
|
+
register_length_unit(rule[:name], rule)
|
659
|
+
when 'binary'
|
660
|
+
register_binary_unit(rule[:name], rule)
|
661
|
+
when 'currency'
|
662
|
+
register_currency(rule[:name], Thread.current['current_currency_exchange_service'])
|
663
|
+
when 'service'
|
664
|
+
begin
|
665
|
+
Thread.current['current_currency_exchange_service'] = Units::Converter::ExchangeRate.const_get(rule[:name])
|
666
|
+
rescue NameError
|
667
|
+
raise NameError, "Exchange service not found: #{rule[:name].to_s.dump}"
|
668
|
+
end
|
669
|
+
else
|
670
|
+
raise "unknown rule type #{rule[:type].to_s.dump}"
|
671
|
+
end
|
672
|
+
end
|
673
|
+
Thread.current['current_currency_exchange_service'] = old_service
|
674
|
+
self
|
675
|
+
end
|
676
|
+
|
677
|
+
# Returns the current Converter in the current Thread.
|
678
|
+
# The default converter is the one returned by <code>converter(:default)</code>.
|
679
|
+
# See also Units#with_converter and Converter.converter.
|
680
|
+
def self.current
|
681
|
+
Thread.current["current_unit_converter"] ||= converter(:default)
|
682
|
+
end
|
683
|
+
|
684
|
+
def self.with_converter(conv) # :nodoc:
|
685
|
+
old_conv = Thread.current["current_unit_converter"]
|
686
|
+
if conv.is_a? Units::Converter
|
687
|
+
Thread.current["current_unit_converter"] = conv
|
688
|
+
else
|
689
|
+
Thread.current["current_unit_converter"] = converter(conv)
|
690
|
+
end
|
691
|
+
yield
|
692
|
+
ensure
|
693
|
+
Thread.current["current_unit_converter"] = old_conv
|
694
|
+
end
|
695
|
+
|
696
|
+
# Returns the converter with the given name.
|
697
|
+
# This name can be a Symbol or a String.
|
698
|
+
def self.converter(name)
|
699
|
+
converters[name.to_sym] or raise ArgumentError, "No converter #{name.to_s.dump} found"
|
700
|
+
end
|
701
|
+
|
702
|
+
# Registers the given Converter under the given name.
|
703
|
+
# This name can be a Symbol or a String.
|
704
|
+
def self.register(name, converter)
|
705
|
+
converters[name.to_sym] = converter
|
706
|
+
end
|
707
|
+
|
708
|
+
# Returns the list of names of registered converters.
|
709
|
+
def self.registered_converters
|
710
|
+
converters.keys
|
711
|
+
end
|
712
|
+
|
713
|
+
private
|
714
|
+
|
715
|
+
def clean_eval(some_name_noone_uses)
|
716
|
+
eval some_name_noone_uses, binding, __FILE__, __LINE__
|
717
|
+
end
|
718
|
+
|
719
|
+
def register_unit_internal(unit, conversion)
|
720
|
+
raise "unit #{unit.to_s.dump} already registered with #{self}" if registered? unit
|
721
|
+
@conversions[unit] = conversion || :none
|
722
|
+
end
|
723
|
+
|
724
|
+
def register_prefixed_unit(unit, prefixes, data = {})
|
725
|
+
unit, aliases, abbrevs = extract_data(unit, data, :to_s)
|
726
|
+
register_unit(unit, :equals => data[:equals], :alias => aliases, :abbrev => abbrevs)
|
727
|
+
unit_sym = unit.to_sym
|
728
|
+
prefixes.each_pair do |pre,info|
|
729
|
+
abbrev = info[:abbrev]
|
730
|
+
multiplier = info[:multiplier] || 1
|
731
|
+
power = info[:power] || 1
|
732
|
+
register_unit(pre + unit, :equals => {:unit => Units::Unit.new({unit_sym => power}, self), :multiplier => multiplier})
|
733
|
+
aliases.each do |a|
|
734
|
+
register_unit(pre + a, :equals => {:unit => Units::Unit.new({unit_sym => power}, self), :multiplier => multiplier})
|
735
|
+
end
|
736
|
+
abbrevs.each do |a|
|
737
|
+
register_unit(abbrev + a, :equals => {:unit => Units::Unit.new({unit_sym => power}, self), :multiplier => multiplier})
|
738
|
+
end
|
739
|
+
end
|
740
|
+
end
|
741
|
+
|
742
|
+
def extract_data(unit, data, conv)
|
743
|
+
sarray = proc do |k|
|
744
|
+
list = data[k] || []
|
745
|
+
list = [list] if not list.is_a? Array
|
746
|
+
list.map { |a| a.send(conv) }
|
747
|
+
end
|
748
|
+
unit = unit.send(conv)
|
749
|
+
return unit, sarray[:alias], sarray[:abbrev].select { |a| a != unit }
|
750
|
+
end
|
751
|
+
|
752
|
+
def decode_conversion(data)
|
753
|
+
if not data.is_a? String
|
754
|
+
return {:unit => data[:unit] || data['unit'],
|
755
|
+
:multiplier => data[:multiplier] || data['multiplier']}
|
756
|
+
end
|
757
|
+
data.rstrip!
|
758
|
+
if m = /^(\d+(\.\d+(?:[eE][-+]?\d+)?)?(?:\s+|$))?(.+)?$/.match(data)
|
759
|
+
unit = m[3] ? clean_eval(m[3].gsub(/\b([A-Z]\w*|in)\b/, 'self.\1')) : Units::Unit.new({}, self)
|
760
|
+
if m[1]
|
761
|
+
multiplier = m[2] ? Float(m[1]) : Integer(m[1])
|
762
|
+
{:unit => unit, :multiplier => multiplier}
|
763
|
+
else
|
764
|
+
{:unit => unit}
|
765
|
+
end
|
766
|
+
else
|
767
|
+
raise ArgumentError, "Wrong unit string"
|
768
|
+
end
|
769
|
+
end
|
770
|
+
|
771
|
+
def convert_conversion(units, multiplier = nil)
|
772
|
+
multiplier ||= 1
|
773
|
+
base_units = {}
|
774
|
+
other_units = {}
|
775
|
+
units.each_pair do |u, e|
|
776
|
+
(conversions(u) != :none ? other_units : base_units)[u] = e
|
777
|
+
end
|
778
|
+
result = Conversion.new(Units::Unit.new(base_units, self), multiplier)
|
779
|
+
other_units.each_pair do |u, e|
|
780
|
+
result *= (conversions(u) ** e)
|
781
|
+
end
|
782
|
+
result
|
783
|
+
end
|
784
|
+
|
785
|
+
def conversions(unit)
|
786
|
+
@conversions[unit] || (unit == %s{--base-currency--} ? :none : nil)
|
787
|
+
end
|
788
|
+
|
789
|
+
def self.converters
|
790
|
+
@converters ||= {}
|
791
|
+
end
|
792
|
+
|
793
|
+
end
|
794
|
+
|
795
|
+
# Contains some configuration related constants
|
796
|
+
module Config
|
797
|
+
|
798
|
+
# The directory in which the data files are searched for
|
799
|
+
begin
|
800
|
+
if ::Gem.active?('calibre')
|
801
|
+
DATADIR = File.join( ::Gem.gempath('calibre'), 'data', 'calibre', 'units' )
|
802
|
+
else
|
803
|
+
DATADIR = File.join( ::Config::CONFIG['datadir'], 'calibre', 'units' )
|
804
|
+
end
|
805
|
+
rescue
|
806
|
+
DATADIR = File.join( ::Config::CONFIG['datadir'], 'calibre', 'units' )
|
807
|
+
end
|
808
|
+
|
809
|
+
# The standard service used for looking up currency exchange rates
|
810
|
+
DEFAULT_CURRENCY_SERVICE = Units::Converter::ExchangeRate::XMethods
|
811
|
+
|
812
|
+
end
|
813
|
+
|
814
|
+
#-- Initialization after everything is set up
|
815
|
+
class Converter
|
816
|
+
standard_converters = YAML.load_file(File.join(Units::Config::DATADIR, 'standard.yaml'))
|
817
|
+
standard_converters.each do |name|
|
818
|
+
register(name, Units::Converter.from_yaml(File.join(Units::Config::DATADIR, name + ".yaml")))
|
819
|
+
end
|
820
|
+
end
|
821
|
+
|
822
|
+
end
|
823
|
+
|
824
|
+
class Numeric
|
825
|
+
|
826
|
+
def method_missing(m, *args, &blk)
|
827
|
+
if args.length == 1
|
828
|
+
args[0] = Units::Converter.converter(args[0]) if not args[0].is_a? Units::Converter
|
829
|
+
return Value.new(self, Units::Unit.new({m => 1}, args[0])) if args[0].registered?(m)
|
830
|
+
elsif Units::Converter.current.registered?(m)
|
831
|
+
raise ArgumentError, "Wrong number of arguments" if args.length != 0
|
832
|
+
return Units::Value.new(self, Units::Unit.new({m => 1}, Units::Converter.current))
|
833
|
+
end
|
834
|
+
super
|
835
|
+
end
|
836
|
+
|
837
|
+
end
|
838
|
+
|
839
|
+
|
840
|
+
|
841
|
+
# _____ _
|
842
|
+
# |_ _|__ ___| |_
|
843
|
+
# | |/ _ \/ __| __|
|
844
|
+
# | | __/\__ \ |_
|
845
|
+
# |_|\___||___/\__|
|
846
|
+
#
|
847
|
+
|
848
|
+
=begin testing
|
849
|
+
|
850
|
+
require 'test/unit'
|
851
|
+
|
852
|
+
include Units
|
853
|
+
|
854
|
+
class TC01 < Test::Unit::TestCase
|
855
|
+
|
856
|
+
def test_01_001
|
857
|
+
assert_equal( "65.0 bit/s", (1.bit/s + 8.bytes/s).to_s )
|
858
|
+
end
|
859
|
+
|
860
|
+
def test_01_002
|
861
|
+
assert_equal( "0.125 byte/s", ((1.bit/s).to(byte/s)).to_s )
|
862
|
+
end
|
863
|
+
|
864
|
+
def test_01_003
|
865
|
+
assert_equal( "5280.0 feet", (1.mile.to(feet)).to_s )
|
866
|
+
end
|
867
|
+
|
868
|
+
def test_01_004
|
869
|
+
assert_equal( "4840.0 yd**2", (1.acre.to(yd**2)).to_s )
|
870
|
+
end
|
871
|
+
|
872
|
+
def test_01_005
|
873
|
+
assert_equal( "4840.0 sq_yd", (1.acre.to(sq_yd)).to_s )
|
874
|
+
end
|
875
|
+
|
876
|
+
def test_01_006
|
877
|
+
assert_equal( "3.785411784 L", (1.gallon.to(self.L)).to_s )
|
878
|
+
end
|
879
|
+
|
880
|
+
def test_01_007
|
881
|
+
assert_equal( "0.45359237 kg", (1.lb.to(kg)).to_s )
|
882
|
+
end
|
883
|
+
|
884
|
+
def test_01_008
|
885
|
+
assert_equal( "1 s m", (1.m.s.to(m.s)).to_s )
|
886
|
+
end
|
887
|
+
|
888
|
+
def test_01_009
|
889
|
+
assert_equal( "2.589988110336 km**2", (1.sq_mi.to(km**2)).to_s )
|
890
|
+
end
|
891
|
+
|
892
|
+
def test_01_010
|
893
|
+
assert_equal( "1.609344 km", (1.mile.to(km)).to_s )
|
894
|
+
end
|
895
|
+
|
896
|
+
# currency rates change frequently, temp commented
|
897
|
+
#def test_01_011
|
898
|
+
# assert_equal( "33.2750003803837 twd", (1.usd.to(twd)).to_s )
|
899
|
+
#end
|
900
|
+
|
901
|
+
def test_01_012
|
902
|
+
with_unit_converter(:uk) {
|
903
|
+
assert_equal( "112.0 lb", 1.cwt.to(lb).to_s )
|
904
|
+
}
|
905
|
+
end
|
906
|
+
|
907
|
+
def test_01_013
|
908
|
+
with_unit_converter(:us) {
|
909
|
+
assert_equal( "100.0 lb", 1.cwt.to(lb).to_s )
|
910
|
+
}
|
911
|
+
end
|
912
|
+
|
913
|
+
def test_01_014
|
914
|
+
assert_equal( "112.0 lb", (1.cwt(:uk).to(lb(:uk))).to_s )
|
915
|
+
assert_equal( "100.0 lb", (1.cwt(:us).to(lb(:us))).to_s )
|
916
|
+
end
|
917
|
+
|
918
|
+
def test_01_015
|
919
|
+
assert_equal( "lb", (Converter.current.lb).to_s )
|
920
|
+
end
|
921
|
+
|
922
|
+
def test_01_016
|
923
|
+
assert_equal( [:us, :cex, :default, :uk], Converter.registered_converters )
|
924
|
+
end
|
925
|
+
|
926
|
+
def test_01_017
|
927
|
+
assert_raises(TypeError) {
|
928
|
+
1.try.to(usd)
|
929
|
+
}
|
930
|
+
end
|
931
|
+
|
932
|
+
def test_01_016
|
933
|
+
assert_equal( "33.2190007563597 twd", (1.usd(:cex).to(twd(:cex))).to_s )
|
934
|
+
end
|
935
|
+
|
936
|
+
end
|
937
|
+
|
938
|
+
=end
|