facets 1.8.8 → 1.8.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/{doc/COPYING → COPYING} +0 -0
- data/PROJECT +18 -11
- data/README +8 -14
- data/Rakefile +3 -7
- data/VERSION +1 -1
- data/doc/AUTHORS +6 -1
- data/doc/{HISTORY.txt → ChangeLog} +96 -0
- data/doc/{HISTORY.1 → History} +0 -0
- data/lib/facet/array/pad.rb +1 -0
- data/lib/facet/dir/self/multiglob_r.rb +1 -0
- data/lib/facet/dir/self/multiglob_with_default.rb +1 -0
- data/lib/facet/string/capitalize_all.rb +1 -0
- data/lib/facet/string/cleave.rb +1 -0
- data/lib/facets/core/array/pad.rb +76 -0
- data/lib/facets/core/dir/self/ls_r.rb +2 -3
- data/lib/facets/core/dir/self/multiglob.rb +90 -12
- data/lib/facets/core/dir/self/{multiglob_recurse.rb → multiglob_r.rb} +1 -1
- data/lib/facets/core/dir/self/multiglob_with_default.rb +3 -0
- data/lib/facets/core/enumerable/accumulate.rb +1 -3
- data/lib/facets/core/enumerable/cart.rb +28 -53
- data/lib/facets/core/enumerable/graph.rb +1 -4
- data/lib/facets/core/enumerable/self/cart.rb +5 -117
- data/lib/facets/core/enumerable/self/cartesian_product.rb +3 -3
- data/lib/facets/core/file/self/create.rb +1 -3
- data/lib/facets/core/hash/rekey.rb +1 -5
- data/lib/facets/core/kernel/ask.rb +2 -2
- data/lib/facets/core/kernel/autoreload.rb +3 -5
- data/lib/facets/core/module/attr_tester.rb +40 -12
- data/lib/facets/core/module/on_included.rb +1 -3
- data/lib/facets/core/nilclass/op_cmp.rb +2 -1
- data/lib/facets/core/string/capitalize_all.rb +30 -0
- data/lib/facets/core/string/capitalized.rb +2 -4
- data/lib/facets/core/string/cleave.rb +66 -0
- data/lib/facets/core/symbol/self/generate.rb +5 -4
- data/lib/facets/core/symbol/to_s.rb +3 -1
- data/lib/facets/more/aspects.rb +4 -8
- data/lib/facets/more/expirable.rb +2 -2
- data/lib/facets/more/lrucache.rb +173 -173
- data/lib/facets/more/openobject.rb +1 -1
- data/lib/facets/more/pool.rb +2 -2
- data/lib/facets/more/syncarray.rb +3 -3
- data/lib/facets/more/synchash.rb +3 -3
- data/lib/facets/more/uploadutils.rb +14 -16
- data/lib/facets/yore/enumerable/cross.rb +117 -0
- data/test/lib/facets/core/array/test_at_rand.rb +34 -0
- data/test/lib/facets/core/array/test_delete_unless.rb +28 -0
- data/test/lib/facets/core/array/test_delete_values.rb +28 -0
- data/test/lib/facets/core/array/test_delete_values_at.rb +31 -0
- data/test/lib/facets/core/array/test_first.rb +46 -0
- data/test/lib/facets/core/array/test_head.rb +42 -0
- data/test/lib/facets/core/array/test_last_index.rb +26 -0
- data/test/lib/facets/core/array/test_merge.rb +35 -0
- data/test/lib/facets/core/array/test_mid.rb +33 -0
- data/test/lib/facets/core/array/test_middle.rb +29 -0
- data/test/lib/facets/core/array/test_op_fetch.rb +35 -0
- data/test/lib/facets/core/array/test_pad.rb +48 -0
- data/test/lib/facets/core/array/test_pick.rb +40 -0
- data/test/lib/facets/core/array/test_pos.rb +28 -0
- data/test/lib/facets/core/array/test_pot.rb +28 -0
- data/test/lib/facets/core/array/test_pull.rb +28 -0
- data/test/lib/facets/core/array/test_rand_index.rb +29 -0
- data/test/lib/facets/core/array/test_rand_subset.rb +29 -0
- data/test/lib/facets/core/array/test_range.rb +32 -0
- data/test/lib/facets/core/array/test_rotate.rb +36 -0
- data/test/lib/facets/core/array/test_select.rb +28 -0
- data/test/lib/facets/core/array/test_shuffle.rb +35 -0
- data/test/lib/facets/core/array/test_thru.rb +27 -0
- data/test/lib/facets/core/array/test_to_b.rb +27 -0
- data/test/lib/facets/core/array/test_to_h.rb +33 -0
- data/test/lib/facets/core/array/test_to_hash.rb +27 -0
- data/test/lib/facets/core/binding/self/test_of_caller.rb +35 -0
- data/test/lib/facets/core/binding/test___LINE__.rb +43 -0
- data/test/lib/facets/core/binding/test_call_stack.rb +34 -0
- data/test/lib/facets/core/binding/test_called.rb +30 -0
- data/test/lib/facets/core/binding/test_caller.rb +35 -0
- data/test/lib/facets/core/binding/test_defined.rb +34 -0
- data/test/lib/facets/core/binding/test_eval.rb +34 -0
- data/test/lib/facets/core/binding/test_local_variables.rb +34 -0
- data/test/lib/facets/core/binding/test_method_name.rb +34 -0
- data/test/lib/facets/core/binding/test_op_fetch.rb +34 -0
- data/test/lib/facets/core/binding/test_op_store.rb +35 -0
- data/test/lib/facets/core/binding/test_self.rb +34 -0
- data/test/lib/facets/core/class/test_cattr.rb +63 -0
- data/test/lib/facets/core/class/test_descendents.rb +30 -0
- data/test/lib/facets/core/class/test_method_name.rb +26 -0
- data/test/lib/facets/core/class/test_remove_descendents.rb +32 -0
- data/test/lib/facets/core/class/test_unix_path.rb +26 -0
- data/test/lib/facets/core/comparable/test_at_least.rb +34 -0
- data/test/lib/facets/core/comparable/test_clip.rb +44 -0
- data/test/lib/facets/core/comparable/test_cmp.rb +28 -0
- data/test/lib/facets/core/continuation/self/test_create.rb +26 -0
- data/test/lib/facets/core/date/test_days_in_month.rb +30 -0
- data/test/lib/facets/core/date/test_days_of_month.rb +30 -0
- data/test/lib/facets/core/date/test_stamp.rb +38 -0
- data/test/lib/facets/core/date/test_to_date.rb +30 -0
- data/test/lib/facets/core/date/test_to_s.rb +30 -0
- data/test/lib/facets/core/date/test_to_time.rb +30 -0
- data/test/lib/facets/core/dir/self/test_ancestor.rb +26 -0
- data/test/lib/facets/core/dir/self/test_ascend.rb +41 -0
- data/test/lib/facets/core/enumerable/self/test_cart.rb +44 -0
- data/test/lib/facets/core/enumerable/self/test_combinations.rb +46 -0
- data/test/lib/facets/core/enumerable/self/test_cross.rb +31 -0
- data/test/lib/facets/core/enumerable/test_cart.rb +49 -0
- data/test/lib/facets/core/enumerable/test_collect_with_index.rb +27 -0
- data/test/lib/facets/core/enumerable/test_commonality.rb +31 -0
- data/test/lib/facets/core/enumerable/test_compact_collect.rb +27 -0
- data/test/lib/facets/core/enumerable/test_count.rb +38 -0
- data/test/lib/facets/core/enumerable/test_cross.rb +30 -0
- data/test/lib/facets/core/enumerable/test_each_by.rb +71 -0
- data/test/lib/facets/core/enumerable/test_each_combination.rb +29 -0
- data/test/lib/facets/core/enumerable/test_each_pair.rb +29 -0
- data/test/lib/facets/core/enumerable/test_each_slice.rb +57 -0
- data/test/lib/facets/core/enumerable/test_each_unique_pair.rb +29 -0
- data/test/lib/facets/core/enumerable/test_eachn.rb +34 -0
- data/test/lib/facets/core/enumerable/test_entropy.rb +26 -0
- data/test/lib/facets/core/enumerable/test_every.rb +35 -0
- data/test/lib/facets/core/enumerable/test_ew.rb +31 -0
- data/test/lib/facets/core/enumerable/test_filter_collect.rb +31 -0
- data/test/lib/facets/core/enumerable/test_find_collisions.rb +30 -0
- data/test/lib/facets/core/enumerable/test_frequency.rb +27 -0
- data/test/lib/facets/core/enumerable/test_graph.rb +30 -0
- data/test/lib/facets/core/enumerable/test_ideal_entropy.rb +27 -0
- data/test/lib/facets/core/enumerable/test_none.rb +33 -0
- data/test/lib/facets/core/enumerable/test_occur.rb +30 -0
- data/test/lib/facets/core/enumerable/test_one.rb +35 -0
- data/test/lib/facets/core/enumerable/test_op_pow.rb +27 -0
- data/test/lib/facets/core/enumerable/test_partition_by.rb +31 -0
- data/test/lib/facets/core/enumerable/test_permutation.rb +29 -0
- data/test/lib/facets/core/enumerable/test_probability.rb +27 -0
- data/test/lib/facets/core/enumerable/test_to_h.rb +27 -0
- data/test/lib/facets/core/enumerable/test_uniq_by.rb +28 -0
- data/test/lib/facets/core/file/self/test_create.rb +44 -0
- data/test/lib/facets/core/file/self/test_open_as_string.rb +50 -0
- data/test/lib/facets/core/file/self/test_read_list.rb +40 -0
- data/test/lib/facets/core/file/self/test_sanitize.rb +46 -0
- data/test/lib/facets/core/file/self/test_split_all.rb +27 -0
- data/test/lib/facets/core/float/test_round_at.rb +46 -0
- data/test/lib/facets/core/float/test_round_to.rb +46 -0
- data/test/lib/facets/core/hash/self/test_zipnew.rb +28 -0
- data/test/lib/facets/core/hash/test_alias.rb +31 -0
- data/test/lib/facets/core/hash/test_assert_has_keys.rb +27 -0
- data/test/lib/facets/core/hash/test_assert_has_only_keys.rb +27 -0
- data/test/lib/facets/core/hash/test_at.rb +28 -0
- data/test/lib/facets/core/hash/test_collate.rb +35 -0
- data/test/lib/facets/core/hash/test_each_with_index.rb +29 -0
- data/test/lib/facets/core/hash/test_each_with_key.rb +29 -0
- data/test/lib/facets/core/hash/test_graph.rb +28 -0
- data/test/lib/facets/core/hash/test_has_keys.rb +27 -0
- data/test/lib/facets/core/hash/test_has_only_keys.rb +27 -0
- data/test/lib/facets/core/hash/test_inverse.rb +30 -0
- data/test/lib/facets/core/hash/test_normalize_keys.rb +34 -0
- data/test/lib/facets/core/hash/test_op_fetch.rb +111 -0
- data/test/lib/facets/core/hash/test_op_lshift.rb +29 -0
- data/test/lib/facets/core/hash/test_rand_key.rb +27 -0
- data/test/lib/facets/core/hash/test_rand_pair.rb +27 -0
- data/test/lib/facets/core/hash/test_rand_value.rb +27 -0
- data/test/lib/facets/core/hash/test_rekey.rb +52 -0
- data/test/lib/facets/core/hash/test_replace_each.rb +29 -0
- data/test/lib/facets/core/hash/test_shuffle.rb +32 -0
- data/test/lib/facets/core/hash/test_slice.rb +27 -0
- data/test/lib/facets/core/hash/test_stringify_keys.rb +34 -0
- data/test/lib/facets/core/hash/test_swap.rb +27 -0
- data/test/lib/facets/core/hash/test_swapkey.rb +29 -0
- data/test/lib/facets/core/hash/test_symbolize_keys.rb +34 -0
- data/test/lib/facets/core/hash/test_to_h.rb +27 -0
- data/test/lib/facets/core/hash/test_to_ostruct.rb +30 -0
- data/test/lib/facets/core/hash/test_to_ostruct_recurse.rb +44 -0
- data/test/lib/facets/core/hash/test_traverse.rb +37 -0
- data/test/lib/facets/core/hash/test_update_each.rb +29 -0
- data/test/lib/facets/core/hash/test_update_keys.rb +28 -0
- data/test/lib/facets/core/hash/test_update_values.rb +28 -0
- data/test/lib/facets/core/hash/test_weave.rb +29 -0
- data/test/lib/facets/core/integer/test_factorial.rb +30 -0
- data/test/lib/facets/core/integer/test_multiple.rb +43 -0
- data/test/lib/facets/core/integer/test_of.rb +28 -0
- data/test/lib/facets/core/integer/test_ordinal.rb +29 -0
- data/test/lib/facets/core/integer/test_times_collect.rb +28 -0
- data/test/lib/facets/core/kernel/test___class__.rb +26 -0
- data/test/lib/facets/core/kernel/test_as.rb +42 -0
- data/test/lib/facets/core/kernel/test_assign_from.rb +31 -0
- data/test/lib/facets/core/kernel/test_assign_with.rb +31 -0
- data/test/lib/facets/core/kernel/test_bool.rb +41 -0
- data/test/lib/facets/core/kernel/test_bug.rb +26 -0
- data/test/lib/facets/core/kernel/test_call_stack.rb +26 -0
- data/test/lib/facets/core/kernel/test_called.rb +26 -0
- data/test/lib/facets/core/kernel/test_callee.rb +26 -0
- data/test/lib/facets/core/kernel/test_constant.rb +33 -0
- data/test/lib/facets/core/kernel/test_copy.rb +40 -0
- data/test/lib/facets/core/kernel/test_deep_copy.rb +40 -0
- data/test/lib/facets/core/kernel/test_demo.rb +28 -0
- data/test/lib/facets/core/kernel/test_fn.rb +26 -0
- data/test/lib/facets/core/kernel/test_get_by_id.rb +28 -0
- data/test/lib/facets/core/kernel/test_here.rb +26 -0
- data/test/lib/facets/core/kernel/test_in.rb +27 -0
- data/test/lib/facets/core/kernel/test_instance_class.rb +27 -0
- data/test/lib/facets/core/kernel/test_instance_exec.rb +58 -0
- data/test/lib/facets/core/kernel/test_maybe.rb +26 -0
- data/test/lib/facets/core/kernel/test_meta_class.rb +27 -0
- data/test/lib/facets/core/kernel/test_metaclass.rb +27 -0
- data/test/lib/facets/core/kernel/test_method.rb +32 -0
- data/test/lib/facets/core/kernel/test_methods.rb +50 -0
- data/test/lib/facets/core/kernel/test_new.rb +30 -0
- data/test/lib/facets/core/kernel/test_object_class.rb +26 -0
- data/test/lib/facets/core/kernel/test_object_hexid.rb +27 -0
- data/test/lib/facets/core/kernel/test_qua_class.rb +27 -0
- data/test/lib/facets/core/kernel/test_require_all.rb +26 -0
- data/test/lib/facets/core/kernel/test_require_esc.rb +27 -0
- data/test/lib/facets/core/kernel/test_resc.rb +27 -0
- data/test/lib/facets/core/kernel/test_send_as.rb +26 -0
- data/test/lib/facets/core/kernel/test_set_from.rb +33 -0
- data/test/lib/facets/core/kernel/test_set_with.rb +42 -0
- data/test/lib/facets/core/kernel/test_silently.rb +28 -0
- data/test/lib/facets/core/kernel/test_singleton.rb +40 -0
- data/test/lib/facets/core/kernel/test_singleton_class.rb +27 -0
- data/test/lib/facets/core/kernel/test_super_at.rb +30 -0
- data/test/lib/facets/core/kernel/test_supermethod.rb +33 -0
- data/test/lib/facets/core/kernel/test_this.rb +26 -0
- data/test/lib/facets/core/kernel/test_to_b.rb +28 -0
- data/test/lib/facets/core/kernel/test_to_bool.rb +28 -0
- data/test/lib/facets/core/kernel/test_to_data.rb +59 -0
- data/test/lib/facets/core/kernel/test_uri.rb +30 -0
- data/test/lib/facets/core/kernel/test_val.rb +43 -0
- data/test/lib/facets/core/kernel/test_with.rb +30 -0
- data/test/lib/facets/core/kernel/test_with_accessor.rb +51 -0
- data/test/lib/facets/core/matchdata/test_match.rb +29 -0
- data/test/lib/facets/core/matchdata/test_matchtree.rb +37 -0
- data/test/lib/facets/core/module/self/test_op_add.rb +48 -0
- data/test/lib/facets/core/module/test_abstract.rb +36 -0
- data/test/lib/facets/core/module/test_alias_method_chain.rb +44 -0
- data/test/lib/facets/core/module/test_alias_module_function.rb +34 -0
- data/test/lib/facets/core/module/test_ancestor.rb +26 -0
- data/test/lib/facets/core/module/test_attr_tester.rb +39 -0
- data/test/lib/facets/core/module/test_basename.rb +26 -0
- data/test/lib/facets/core/module/test_by_name.rb +29 -0
- data/test/lib/facets/core/module/test_class_extension.rb +70 -0
- data/test/lib/facets/core/module/test_clone_using.rb +55 -0
- data/test/lib/facets/core/module/test_dirname.rb +26 -0
- data/test/lib/facets/core/module/test_equate_on.rb +37 -0
- data/test/lib/facets/core/module/test_include_as.rb +45 -0
- data/test/lib/facets/core/module/test_initializer.rb +32 -0
- data/test/lib/facets/core/module/test_instance_methods.rb +59 -0
- data/test/lib/facets/core/module/test_integrate.rb +35 -0
- data/test/lib/facets/core/module/test_memoize.rb +50 -0
- data/test/lib/facets/core/module/test_modspace.rb +27 -0
- data/test/lib/facets/core/module/test_namespace.rb +66 -0
- data/test/lib/facets/core/module/test_nesting.rb +36 -0
- data/test/lib/facets/core/module/test_new.rb +34 -0
- data/test/lib/facets/core/module/test_nodef.rb +30 -0
- data/test/lib/facets/core/module/test_on_included.rb +36 -0
- data/test/lib/facets/core/module/test_redef.rb +30 -0
- data/test/lib/facets/core/module/test_redefine_method.rb +30 -0
- data/test/lib/facets/core/module/test_redirect.rb +30 -0
- data/test/lib/facets/core/module/test_redirect_method.rb +30 -0
- data/test/lib/facets/core/module/test_remove.rb +30 -0
- data/test/lib/facets/core/module/test_rename.rb +31 -0
- data/test/lib/facets/core/module/test_rename_method.rb +31 -0
- data/test/lib/facets/core/module/test_revisal.rb +35 -0
- data/test/lib/facets/core/module/test_shadow_method.rb +30 -0
- data/test/lib/facets/core/module/test_sort_on.rb +42 -0
- data/test/lib/facets/core/module/test_this.rb +31 -0
- data/test/lib/facets/core/module/test_wrap.rb +30 -0
- data/test/lib/facets/core/module/test_wrap_method.rb +30 -0
- data/test/lib/facets/core/nilclass/test_blank.rb +26 -0
- data/test/lib/facets/core/nilclass/test_empty.rb +26 -0
- data/test/lib/facets/core/nilclass/test_include.rb +26 -0
- data/test/lib/facets/core/nilclass/test_op_fetch.rb +28 -0
- data/test/lib/facets/core/nilclass/test_size.rb +30 -0
- data/test/lib/facets/core/nilclass/test_to_h.rb +26 -0
- data/test/lib/facets/core/numeric/test_approx.rb +29 -0
- data/test/lib/facets/core/numeric/test_ceil_multiple.rb +31 -0
- data/test/lib/facets/core/numeric/test_distance.rb +28 -0
- data/test/lib/facets/core/numeric/test_succ.rb +40 -0
- data/test/lib/facets/core/numeric/test_to_b.rb +27 -0
- data/test/lib/facets/core/ostruct/test___update__.rb +45 -0
- data/test/lib/facets/core/ostruct/test_instance_delegate.rb +34 -0
- data/test/lib/facets/core/ostruct/test_op_fetch.rb +33 -0
- data/test/lib/facets/core/proc/test_compose.rb +29 -0
- data/test/lib/facets/core/proc/test_op_mul.rb +34 -0
- data/test/lib/facets/core/proc/test_to_method.rb +44 -0
- data/test/lib/facets/core/range/test_to_r.rb +27 -0
- data/test/lib/facets/core/range/test_to_range.rb +27 -0
- data/test/lib/facets/core/range/test_umbrella.rb +34 -0
- data/test/lib/facets/core/range/test_within.rb +29 -0
- data/test/lib/facets/core/regexp/test_arity.rb +33 -0
- data/test/lib/facets/core/regexp/test_to_re.rb +27 -0
- data/test/lib/facets/core/regexp/test_to_regexp.rb +27 -0
- data/test/lib/facets/core/string/self/test_interpolate.rb +27 -0
- data/test/lib/facets/core/string/self/test_patterns.rb +28 -0
- data/test/lib/facets/core/string/self/test_rand_letter.rb +26 -0
- data/test/lib/facets/core/string/test_align_center.rb +34 -0
- data/test/lib/facets/core/string/test_at_rand.rb +35 -0
- data/test/lib/facets/core/string/test_basename.rb +41 -0
- data/test/lib/facets/core/string/test_blank.rb +27 -0
- data/test/lib/facets/core/string/test_bracket.rb +49 -0
- data/test/lib/facets/core/string/test_camelcase.rb +39 -0
- data/test/lib/facets/core/string/test_camelize.rb +26 -0
- data/test/lib/facets/core/string/test_capitalize_all.rb +26 -0
- data/test/lib/facets/core/string/test_capitalized.rb +26 -0
- data/test/lib/facets/core/string/test_chars.rb +27 -0
- data/test/lib/facets/core/string/test_cleave.rb +43 -0
- data/test/lib/facets/core/string/test_cmp.rb +30 -0
- data/test/lib/facets/core/string/test_demodulize.rb +38 -0
- data/test/lib/facets/core/string/test_divide.rb +29 -0
- data/test/lib/facets/core/string/test_downcase.rb +26 -0
- data/test/lib/facets/core/string/test_dresner.rb +32 -0
- data/test/lib/facets/core/string/test_each_char.rb +29 -0
- data/test/lib/facets/core/string/test_each_word.rb +29 -0
- data/test/lib/facets/core/string/test_first.rb +43 -0
- data/test/lib/facets/core/string/test_fold.rb +34 -0
- data/test/lib/facets/core/string/test_humanize.rb +26 -0
- data/test/lib/facets/core/string/test_indent.rb +40 -0
- data/test/lib/facets/core/string/test_index_all.rb +26 -0
- data/test/lib/facets/core/string/test_last.rb +46 -0
- data/test/lib/facets/core/string/test_line_wrap.rb +27 -0
- data/test/lib/facets/core/string/test_lines.rb +26 -0
- data/test/lib/facets/core/string/test_lowercase.rb +26 -0
- data/test/lib/facets/core/string/test_margin.rb +117 -0
- data/test/lib/facets/core/string/test_methodize.rb +27 -0
- data/test/lib/facets/core/string/test_modulize.rb +29 -0
- data/test/lib/facets/core/string/test_mscan.rb +30 -0
- data/test/lib/facets/core/string/test_natcmp.rb +30 -0
- data/test/lib/facets/core/string/test_nchar.rb +29 -0
- data/test/lib/facets/core/string/test_pathize.rb +29 -0
- data/test/lib/facets/core/string/test_pop.rb +47 -0
- data/test/lib/facets/core/string/test_pot.rb +39 -0
- data/test/lib/facets/core/string/test_push.rb +40 -0
- data/test/lib/facets/core/string/test_quote.rb +49 -0
- data/test/lib/facets/core/string/test_rand_byte.rb +35 -0
- data/test/lib/facets/core/string/test_rand_index.rb +26 -0
- data/test/lib/facets/core/string/test_range.rb +27 -0
- data/test/lib/facets/core/string/test_range_all.rb +27 -0
- data/test/lib/facets/core/string/test_range_of_line.rb +28 -0
- data/test/lib/facets/core/string/test_regesc.rb +28 -0
- data/test/lib/facets/core/string/test_shatter.rb +29 -0
- data/test/lib/facets/core/string/test_shift.rb +40 -0
- data/test/lib/facets/core/string/test_shuffle.rb +27 -0
- data/test/lib/facets/core/string/test_similarity.rb +26 -0
- data/test/lib/facets/core/string/test_singular.rb +86 -0
- data/test/lib/facets/core/string/test_soundex.rb +41 -0
- data/test/lib/facets/core/string/test_succ.rb +29 -0
- data/test/lib/facets/core/string/test_to_a.rb +27 -0
- data/test/lib/facets/core/string/test_to_b.rb +41 -0
- data/test/lib/facets/core/string/test_to_const.rb +28 -0
- data/test/lib/facets/core/string/test_to_date.rb +30 -0
- data/test/lib/facets/core/string/test_to_proc.rb +33 -0
- data/test/lib/facets/core/string/test_to_re.rb +28 -0
- data/test/lib/facets/core/string/test_to_time.rb +30 -0
- data/test/lib/facets/core/string/test_unix_crypt.rb +30 -0
- data/test/lib/facets/core/string/test_unpack.rb +34 -0
- data/test/lib/facets/core/string/test_unshift.rb +39 -0
- data/test/lib/facets/core/string/test_upcase.rb +30 -0
- data/test/lib/facets/core/string/test_whitespace.rb +27 -0
- data/test/lib/facets/core/string/test_word_filter.rb +34 -0
- data/test/lib/facets/core/string/test_word_wrap.rb +51 -0
- data/test/lib/facets/core/string/test_words.rb +37 -0
- data/test/lib/facets/core/symbol/test_camelcase.rb +26 -0
- data/test/lib/facets/core/symbol/test_camelize.rb +27 -0
- data/test/lib/facets/core/symbol/test_capitalize.rb +26 -0
- data/test/lib/facets/core/symbol/test_capitalized.rb +27 -0
- data/test/lib/facets/core/symbol/test_downcase.rb +27 -0
- data/test/lib/facets/core/symbol/test_not.rb +29 -0
- data/test/lib/facets/core/symbol/test_pad.rb +27 -0
- data/test/lib/facets/core/symbol/test_succ.rb +28 -0
- data/test/lib/facets/core/symbol/test_to_const.rb +28 -0
- data/test/lib/facets/core/symbol/test_to_proc.rb +33 -0
- data/test/lib/facets/core/symbol/test_to_str.rb +28 -0
- data/test/lib/facets/core/symbol/test_underscore.rb +26 -0
- data/test/lib/facets/core/symbol/test_upcase.rb +27 -0
- data/test/lib/facets/core/time/test_change.rb +33 -0
- data/test/lib/facets/core/time/test_elapse.rb +27 -0
- data/test/lib/facets/core/time/test_stamp.rb +39 -0
- data/test/lib/facets/core/time/test_to_date.rb +31 -0
- data/test/lib/facets/core/time/test_to_s.rb +31 -0
- data/test/lib/facets/core/time/test_to_time.rb +31 -0
- data/test/lib/facets/more/test_ann.rb +112 -0
- data/test/lib/facets/more/test_ann_attr.rb +39 -0
- data/test/lib/facets/more/test_ansicode.rb +36 -0
- data/test/lib/facets/more/test_arguments.rb +83 -0
- data/test/lib/facets/more/test_association.rb +51 -0
- data/test/lib/facets/more/test_autoarray.rb +32 -0
- data/test/lib/facets/more/test_basicobject.rb +67 -0
- data/test/lib/facets/more/test_bbcode.rb +34 -0
- data/test/lib/facets/more/test_binaryreader.rb +64 -0
- data/test/lib/facets/more/test_bitmask.rb +47 -0
- data/test/lib/facets/more/test_buildingblock.rb +47 -0
- data/test/lib/facets/more/test_bytes.rb +84 -0
- data/test/lib/facets/more/test_classmethods.rb +69 -0
- data/test/lib/facets/more/test_command.rb +69 -0
- data/test/lib/facets/more/test_coroutine.rb +60 -0
- data/test/lib/facets/more/test_crypt.rb +46 -0
- data/test/lib/facets/more/test_cut.rb +198 -0
- data/test/lib/facets/more/test_dependency.rb +82 -0
- data/test/lib/facets/more/test_dictionary.rb +107 -0
- data/test/lib/facets/more/test_elementor.rb +52 -0
- data/test/lib/facets/more/test_enumerablepass.rb +86 -0
- data/test/lib/facets/more/test_floatstring.rb +36 -0
- data/test/lib/facets/more/test_functor.rb +39 -0
- data/test/lib/facets/more/test_htmlbuilder.rb +38 -0
- data/test/lib/facets/more/test_htmlfilter.rb +86 -0
- data/test/lib/facets/more/test_infinity.rb +50 -0
- data/test/lib/facets/more/test_inheritor.rb +155 -0
- data/test/lib/facets/more/test_instance_intercept.rb +50 -0
- data/test/lib/facets/more/test_interval.rb +119 -0
- data/test/lib/facets/more/test_json.rb +232 -0
- data/test/lib/facets/more/test_linkedlist.rb +53 -0
- data/test/lib/facets/more/test_lisp.rb +47 -0
- data/test/lib/facets/more/test_lisp_format.rb +37 -0
- data/test/lib/facets/more/test_lrucache.rb +25 -0
- data/test/lib/facets/more/test_mathconstants.rb +18 -0
- data/test/lib/facets/more/test_methodfilter.rb +45 -0
- data/test/lib/facets/more/test_methodprobe.rb +53 -0
- data/test/lib/facets/more/test_multipliers.rb +114 -0
- data/test/lib/facets/more/test_multiton.rb +199 -0
- data/test/lib/facets/more/test_nackclass.rb +45 -0
- data/test/lib/facets/more/test_nilcomparable.rb +47 -0
- data/test/lib/facets/more/test_opencascade.rb +75 -0
- data/test/lib/facets/more/test_openhash.rb +126 -0
- data/test/lib/facets/more/test_openobject.rb +126 -0
- data/test/lib/facets/more/test_overload.rb +54 -0
- data/test/lib/facets/more/test_paramix.rb +100 -0
- data/test/lib/facets/more/test_pqueue.rb +39 -0
- data/test/lib/facets/more/test_preinitialize.rb +49 -0
- data/test/lib/facets/more/test_promoteself.rb +45 -0
- data/test/lib/facets/more/test_recorder.rb +44 -0
- data/test/lib/facets/more/test_snapshot.rb +34 -0
- data/test/lib/facets/more/test_statichash.rb +31 -0
- data/test/lib/facets/more/test_syncarray.rb +28 -0
- data/test/lib/facets/more/test_synchash.rb +28 -0
- data/test/lib/facets/more/test_tagiterator.rb +93 -0
- data/test/lib/facets/more/test_timer.rb +66 -0
- data/test/lib/facets/more/test_times.rb +103 -0
- data/test/lib/facets/more/test_tuple.rb +64 -0
- data/test/lib/facets/more/test_typecast.rb +68 -0
- data/test/lib/facets/more/test_uninheritable.rb +42 -0
- data/test/lib/facets/more/test_units.rb +111 -0
- data/test/lib/facets/more/test_xmlbuilder.rb +54 -0
- data/test/lib/facets/more/test_xmlhelper.rb +40 -0
- data/test/lib/facets/more/test_xoxo.rb +288 -0
- data/test/lib/facets/more/test_yamlstruct.rb +37 -0
- data/test/lib/facets/yore/enumerable/test_cross.rb +44 -0
- data/test/lib/facets/yore/kernel/test_require_facet.rb +27 -0
- data/test/lib/facets/yore/module/test_namespace.rb +33 -0
- data/test/lib/facets/yore/test_annattr.rb +39 -0
- data/test/lib/facets/yore/test_annotation.rb +318 -0
- metadata +589 -9
- data/lib/facet/dir/self/multiglob_recurse.rb +0 -1
- data/lib/facet/dir/self/multiglob_sum.rb +0 -1
- data/lib/facets/core/dir/self/multiglob_sum.rb +0 -42
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# CREDIT Richard Laugesen
|
|
2
|
+
|
|
3
|
+
class String
|
|
4
|
+
|
|
5
|
+
# Capitalize all words (or other patterned divisions) of a string.
|
|
6
|
+
#
|
|
7
|
+
# "this is a test".capitalize_all #=> "This Is A Test"
|
|
8
|
+
|
|
9
|
+
def capitalize_all( pattern=$;, *limit )
|
|
10
|
+
split(pattern, *limit).select{ |w| w.capitalize! || w }.join(" ")
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
=begin test
|
|
17
|
+
|
|
18
|
+
require 'test/unit'
|
|
19
|
+
|
|
20
|
+
class TestCapitalizeAll < Test::Unit::TestCase
|
|
21
|
+
def test_01
|
|
22
|
+
r = "try this out".capitalize_all
|
|
23
|
+
x = "Try This Out"
|
|
24
|
+
assert_equal(x,r)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
=end
|
|
29
|
+
|
|
30
|
+
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
# CREDIT Phil Tomson
|
|
1
2
|
|
|
2
3
|
class String
|
|
3
4
|
|
|
@@ -6,10 +7,7 @@ class String
|
|
|
6
7
|
# "THIS".capitalized? #=> true
|
|
7
8
|
# "This".capitalized? #=> true
|
|
8
9
|
# "this".capitalized? #=> false
|
|
9
|
-
|
|
10
|
-
#--
|
|
11
|
-
# Credit goes to Phil Tomson.
|
|
12
|
-
#++
|
|
10
|
+
|
|
13
11
|
def capitalized?
|
|
14
12
|
self =~ /^[A-Z]/
|
|
15
13
|
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
|
|
2
|
+
class String
|
|
3
|
+
|
|
4
|
+
def cleave(threshold=nil, len=nil)
|
|
5
|
+
l = (len || size / 2)
|
|
6
|
+
t = threshold || size
|
|
7
|
+
|
|
8
|
+
h1 = self[0...l]
|
|
9
|
+
h2 = self[l..-1]
|
|
10
|
+
|
|
11
|
+
i1 = h1.rindex(/\s/) || 0
|
|
12
|
+
d1 = (i1 - l).abs
|
|
13
|
+
|
|
14
|
+
d2 = h2.index(/\s/) || l
|
|
15
|
+
i2 = d2 + l
|
|
16
|
+
|
|
17
|
+
d1 = (i1-l).abs
|
|
18
|
+
d2 = (i2-l).abs
|
|
19
|
+
|
|
20
|
+
if [d1, d2].min > t
|
|
21
|
+
i = t
|
|
22
|
+
elsif d1 < d2
|
|
23
|
+
i = i1
|
|
24
|
+
else
|
|
25
|
+
i = i2
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
#dup.insert(l, "\n").gsub(/^\s+|\s+$/, '')
|
|
29
|
+
return self[0..i].to_s.strip, self[i+1..-1].to_s.strip
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
=begin test
|
|
37
|
+
|
|
38
|
+
require 'test/unit'
|
|
39
|
+
|
|
40
|
+
class StringCleaveTest < Test::Unit::TestCase
|
|
41
|
+
def test_nospaces
|
|
42
|
+
assert_equal [ 'whole', '' ], 'whole'.cleave
|
|
43
|
+
assert_equal [ 'Supercalifragilisticexpialidocious', '' ],
|
|
44
|
+
'Supercalifragilisticexpialidocious'.cleave
|
|
45
|
+
end
|
|
46
|
+
def test_exact_middle
|
|
47
|
+
assert_equal [ 'fancy', 'split' ], 'fancy split'.cleave
|
|
48
|
+
assert_equal [ 'All good Rubyists', 'know how to party' ],
|
|
49
|
+
'All good Rubyists know how to party'.cleave
|
|
50
|
+
end
|
|
51
|
+
def test_closer_to_start
|
|
52
|
+
assert_equal [ 'short', 'splitter' ], 'short splitter'.cleave
|
|
53
|
+
assert_equal [ 'Four score and', 'seven years ago...' ],
|
|
54
|
+
'Four score and seven years ago...'.cleave
|
|
55
|
+
assert_equal [ 'abc def', 'ghijklm nop' ],
|
|
56
|
+
'abc def ghijklm nop'.cleave
|
|
57
|
+
end
|
|
58
|
+
def test_closer_to_end
|
|
59
|
+
assert_equal [ 'extended', 'split' ], 'extended split'.cleave
|
|
60
|
+
assert_equal [ 'abc defghi', 'jklm nop' ],
|
|
61
|
+
'abc defghi jklm nop'.cleave
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
=end
|
|
66
|
+
|
|
@@ -5,13 +5,14 @@ class Symbol
|
|
|
5
5
|
#
|
|
6
6
|
# Symbol.generate => :<1>
|
|
7
7
|
#
|
|
8
|
-
# If +
|
|
8
|
+
# If +suffix+ is given the new symbol will be suffixed with it.
|
|
9
9
|
#
|
|
10
10
|
# Symbol.generate(:this) => :<2>this
|
|
11
11
|
#
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
|
|
13
|
+
def self.generate(suffix=nil)
|
|
14
|
+
@symbol_generate_counter ||= 0
|
|
15
|
+
("<%X>#{suffix}" % @symbol_generate_counter += 1).to_sym
|
|
15
16
|
end
|
|
16
17
|
|
|
17
18
|
end
|
data/lib/facets/more/aspects.rb
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
# Copyright:: Copyright (c) 2005 George Moschovitis
|
|
20
20
|
# License:: Ruby License
|
|
21
21
|
|
|
22
|
-
require '
|
|
22
|
+
require 'facets/more/inheritor'
|
|
23
23
|
|
|
24
24
|
# = Aspect
|
|
25
25
|
#
|
|
@@ -76,15 +76,11 @@ module Aspects
|
|
|
76
76
|
include_advice_modules(target)
|
|
77
77
|
|
|
78
78
|
for m in [methods].flatten
|
|
79
|
-
args = []
|
|
80
|
-
target.instance_method(m).arity.times { |i| args << "a#{i}" }
|
|
81
|
-
args = args.join(',')
|
|
82
|
-
|
|
83
79
|
target.module_eval <<-end_eval, __FILE__, __LINE__
|
|
84
80
|
alias_method :__unwrapped_#{m}, :#{m}
|
|
85
|
-
def #{m}(
|
|
81
|
+
def #{m}(*args,&block)
|
|
86
82
|
#{gen_advice_code(m, target.advices, :pre)}
|
|
87
|
-
__unwrapped_#{m}(
|
|
83
|
+
__unwrapped_#{m}(*args,&block)
|
|
88
84
|
#{gen_advice_code(m, target.advices, :post)}
|
|
89
85
|
end
|
|
90
86
|
end_eval
|
|
@@ -162,7 +158,7 @@ module Aspects
|
|
|
162
158
|
code << "#{advice}.#{m}(self); "
|
|
163
159
|
end
|
|
164
160
|
else
|
|
165
|
-
# Module,
|
|
161
|
+
# Module, already handled.
|
|
166
162
|
end
|
|
167
163
|
else
|
|
168
164
|
if m = o[join] and advice.methods.include?(m.to_s)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# = expirable.rb
|
|
2
2
|
#
|
|
3
|
-
# == Copyright (c) 2004
|
|
3
|
+
# == Copyright (c) 2004 George Moschovitis
|
|
4
4
|
#
|
|
5
5
|
# Ruby License
|
|
6
6
|
#
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
# * George Moschovitis
|
|
17
17
|
|
|
18
18
|
# Author:: George Moschovitis
|
|
19
|
-
# Copyright:: Copyright (c) 2004
|
|
19
|
+
# Copyright:: Copyright (c) 2004 George Moschovitis
|
|
20
20
|
# License:: Ruby License
|
|
21
21
|
|
|
22
22
|
# = Expirable
|
data/lib/facets/more/lrucache.rb
CHANGED
|
@@ -1,173 +1,173 @@
|
|
|
1
|
-
# = lrucache.rb
|
|
2
|
-
#
|
|
3
|
-
#
|
|
4
|
-
#
|
|
5
|
-
# Ruby License
|
|
6
|
-
#
|
|
7
|
-
# This module is free software. You may use, modify, and/or redistribute this
|
|
8
|
-
# software under the same terms as Ruby.
|
|
9
|
-
#
|
|
10
|
-
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
11
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
12
|
-
# FOR A PARTICULAR PURPOSE.
|
|
13
|
-
#
|
|
14
|
-
# == Author(s)
|
|
15
|
-
#
|
|
16
|
-
# * George Moschovitis
|
|
17
|
-
|
|
18
|
-
# Author:: George Moschovitis, Anastasios Koutoumanos
|
|
19
|
-
# Copyright:: Copyright (c) 2005
|
|
20
|
-
# License:: Ruby License
|
|
21
|
-
|
|
22
|
-
# = LRUCache
|
|
23
|
-
#
|
|
24
|
-
# A cache utilizing a simple LRU (Least Recently Used) policy.
|
|
25
|
-
# The items managed by this cache must respond to the #key method.
|
|
26
|
-
# Attempts to optimize reads rather than inserts!
|
|
27
|
-
#
|
|
28
|
-
# LRU semantics are enforced by inserting the items in a queue.
|
|
29
|
-
# The lru item is always at the tail. Two special sentinels
|
|
30
|
-
# (head, tail) are used to simplify (?) the code.
|
|
31
|
-
|
|
32
|
-
class LRUCache < Hash
|
|
33
|
-
|
|
34
|
-
# Mix this in your class to make LRU-managable.
|
|
35
|
-
|
|
36
|
-
module Item
|
|
37
|
-
attr_accessor :lru_key, :lru_prev, :lru_next
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
# head-tail sentinels
|
|
41
|
-
|
|
42
|
-
class Sentinel; include Item; end
|
|
43
|
-
|
|
44
|
-
# the maximum number of items in the cache.
|
|
45
|
-
|
|
46
|
-
attr_accessor :max_items
|
|
47
|
-
|
|
48
|
-
# the head sentinel and the tail sentinel, tail.prev points to the lru item.
|
|
49
|
-
|
|
50
|
-
attr_reader :head, :tail
|
|
51
|
-
|
|
52
|
-
def initialize(max_items)
|
|
53
|
-
@max_items = max_items
|
|
54
|
-
lru_clear()
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
# Lookup an item in the cache.
|
|
58
|
-
|
|
59
|
-
def [](key)
|
|
60
|
-
if item = super
|
|
61
|
-
return lru_touch(item)
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
# The inserted item is considered mru!
|
|
66
|
-
|
|
67
|
-
def []=(key, item)
|
|
68
|
-
item = super
|
|
69
|
-
item.lru_key = key
|
|
70
|
-
lru_insert(item)
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
# Delete an item from the cache.
|
|
74
|
-
|
|
75
|
-
def delete(key)
|
|
76
|
-
if item = super
|
|
77
|
-
lru_delete(item)
|
|
78
|
-
end
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
# Clear the cache.
|
|
82
|
-
|
|
83
|
-
def clear
|
|
84
|
-
super
|
|
85
|
-
lru_clear()
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
# The first (mru) element in the cache.
|
|
89
|
-
|
|
90
|
-
def first
|
|
91
|
-
@head.lru_next
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
# The last (lru) element in the cache.
|
|
95
|
-
|
|
96
|
-
def last
|
|
97
|
-
@tail.lru_prev
|
|
98
|
-
end
|
|
99
|
-
alias_method :lru, :last
|
|
100
|
-
|
|
101
|
-
private
|
|
102
|
-
|
|
103
|
-
# Delete an item from the lru list.
|
|
104
|
-
|
|
105
|
-
def lru_delete(item)
|
|
106
|
-
lru_join(item.lru_prev, item.lru_next)
|
|
107
|
-
return item
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
# Join two items in the lru list.
|
|
111
|
-
# Return y to allow for chaining.
|
|
112
|
-
|
|
113
|
-
def lru_join(x, y)
|
|
114
|
-
x.lru_next = y
|
|
115
|
-
y.lru_prev = x
|
|
116
|
-
return y
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
# Append a child item to a parent item in the lru list
|
|
120
|
-
# (Re)inserts the child in the list.
|
|
121
|
-
|
|
122
|
-
def lru_append(parent, child)
|
|
123
|
-
lru_join(child, parent.lru_next)
|
|
124
|
-
lru_join(parent, child)
|
|
125
|
-
end
|
|
126
|
-
|
|
127
|
-
# Insert an item
|
|
128
|
-
|
|
129
|
-
def lru_insert(item)
|
|
130
|
-
delete(last.lru_key) if size() > @max_items
|
|
131
|
-
lru_append(@head, item)
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
# Touch an item, make mru!
|
|
135
|
-
# Returns the item.
|
|
136
|
-
|
|
137
|
-
def lru_touch(item)
|
|
138
|
-
lru_append(@head, lru_delete(item))
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
# Clear the lru.
|
|
142
|
-
|
|
143
|
-
def lru_clear
|
|
144
|
-
@head = Sentinel.new
|
|
145
|
-
@tail = Sentinel.new
|
|
146
|
-
lru_join(@head, @tail)
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
end
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
# _____ _
|
|
154
|
-
# |_ _|__ ___| |_
|
|
155
|
-
# | |/ _ \/ __| __|
|
|
156
|
-
# | | __/\__ \ |_
|
|
157
|
-
# |_|\___||___/\__|
|
|
158
|
-
#
|
|
159
|
-
|
|
160
|
-
# TODO
|
|
161
|
-
|
|
162
|
-
=begin testing
|
|
163
|
-
|
|
164
|
-
require 'test/unit'
|
|
165
|
-
|
|
166
|
-
class TC_LRUCache < Test::Unit::TestCase
|
|
167
|
-
|
|
168
|
-
def test_01
|
|
169
|
-
end
|
|
170
|
-
|
|
171
|
-
end
|
|
172
|
-
|
|
173
|
-
=end
|
|
1
|
+
# = lrucache.rb
|
|
2
|
+
#
|
|
3
|
+
# = Copyright 2004 George Moschovitis
|
|
4
|
+
#
|
|
5
|
+
# Ruby License
|
|
6
|
+
#
|
|
7
|
+
# This module is free software. You may use, modify, and/or redistribute this
|
|
8
|
+
# software under the same terms as Ruby.
|
|
9
|
+
#
|
|
10
|
+
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
11
|
+
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
12
|
+
# FOR A PARTICULAR PURPOSE.
|
|
13
|
+
#
|
|
14
|
+
# == Author(s)
|
|
15
|
+
#
|
|
16
|
+
# * George Moschovitis
|
|
17
|
+
|
|
18
|
+
# Author:: George Moschovitis, Anastasios Koutoumanos
|
|
19
|
+
# Copyright:: Copyright (c) 2005 George Moschovitis
|
|
20
|
+
# License:: Ruby License
|
|
21
|
+
|
|
22
|
+
# = LRUCache
|
|
23
|
+
#
|
|
24
|
+
# A cache utilizing a simple LRU (Least Recently Used) policy.
|
|
25
|
+
# The items managed by this cache must respond to the #key method.
|
|
26
|
+
# Attempts to optimize reads rather than inserts!
|
|
27
|
+
#
|
|
28
|
+
# LRU semantics are enforced by inserting the items in a queue.
|
|
29
|
+
# The lru item is always at the tail. Two special sentinels
|
|
30
|
+
# (head, tail) are used to simplify (?) the code.
|
|
31
|
+
|
|
32
|
+
class LRUCache < Hash
|
|
33
|
+
|
|
34
|
+
# Mix this in your class to make LRU-managable.
|
|
35
|
+
|
|
36
|
+
module Item
|
|
37
|
+
attr_accessor :lru_key, :lru_prev, :lru_next
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# head-tail sentinels
|
|
41
|
+
|
|
42
|
+
class Sentinel; include Item; end
|
|
43
|
+
|
|
44
|
+
# the maximum number of items in the cache.
|
|
45
|
+
|
|
46
|
+
attr_accessor :max_items
|
|
47
|
+
|
|
48
|
+
# the head sentinel and the tail sentinel, tail.prev points to the lru item.
|
|
49
|
+
|
|
50
|
+
attr_reader :head, :tail
|
|
51
|
+
|
|
52
|
+
def initialize(max_items)
|
|
53
|
+
@max_items = max_items
|
|
54
|
+
lru_clear()
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Lookup an item in the cache.
|
|
58
|
+
|
|
59
|
+
def [](key)
|
|
60
|
+
if item = super
|
|
61
|
+
return lru_touch(item)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# The inserted item is considered mru!
|
|
66
|
+
|
|
67
|
+
def []=(key, item)
|
|
68
|
+
item = super
|
|
69
|
+
item.lru_key = key
|
|
70
|
+
lru_insert(item)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Delete an item from the cache.
|
|
74
|
+
|
|
75
|
+
def delete(key)
|
|
76
|
+
if item = super
|
|
77
|
+
lru_delete(item)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Clear the cache.
|
|
82
|
+
|
|
83
|
+
def clear
|
|
84
|
+
super
|
|
85
|
+
lru_clear()
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# The first (mru) element in the cache.
|
|
89
|
+
|
|
90
|
+
def first
|
|
91
|
+
@head.lru_next
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# The last (lru) element in the cache.
|
|
95
|
+
|
|
96
|
+
def last
|
|
97
|
+
@tail.lru_prev
|
|
98
|
+
end
|
|
99
|
+
alias_method :lru, :last
|
|
100
|
+
|
|
101
|
+
private
|
|
102
|
+
|
|
103
|
+
# Delete an item from the lru list.
|
|
104
|
+
|
|
105
|
+
def lru_delete(item)
|
|
106
|
+
lru_join(item.lru_prev, item.lru_next)
|
|
107
|
+
return item
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Join two items in the lru list.
|
|
111
|
+
# Return y to allow for chaining.
|
|
112
|
+
|
|
113
|
+
def lru_join(x, y)
|
|
114
|
+
x.lru_next = y
|
|
115
|
+
y.lru_prev = x
|
|
116
|
+
return y
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Append a child item to a parent item in the lru list
|
|
120
|
+
# (Re)inserts the child in the list.
|
|
121
|
+
|
|
122
|
+
def lru_append(parent, child)
|
|
123
|
+
lru_join(child, parent.lru_next)
|
|
124
|
+
lru_join(parent, child)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Insert an item
|
|
128
|
+
|
|
129
|
+
def lru_insert(item)
|
|
130
|
+
delete(last.lru_key) if size() > @max_items
|
|
131
|
+
lru_append(@head, item)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Touch an item, make mru!
|
|
135
|
+
# Returns the item.
|
|
136
|
+
|
|
137
|
+
def lru_touch(item)
|
|
138
|
+
lru_append(@head, lru_delete(item))
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
# Clear the lru.
|
|
142
|
+
|
|
143
|
+
def lru_clear
|
|
144
|
+
@head = Sentinel.new
|
|
145
|
+
@tail = Sentinel.new
|
|
146
|
+
lru_join(@head, @tail)
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
# _____ _
|
|
154
|
+
# |_ _|__ ___| |_
|
|
155
|
+
# | |/ _ \/ __| __|
|
|
156
|
+
# | | __/\__ \ |_
|
|
157
|
+
# |_|\___||___/\__|
|
|
158
|
+
#
|
|
159
|
+
|
|
160
|
+
# TODO
|
|
161
|
+
|
|
162
|
+
=begin testing
|
|
163
|
+
|
|
164
|
+
require 'test/unit'
|
|
165
|
+
|
|
166
|
+
class TC_LRUCache < Test::Unit::TestCase
|
|
167
|
+
|
|
168
|
+
def test_01
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
=end
|