Class: Array

Inherits:
Object show all
Defined in:
lib/core_utilities/core_ext/array/base.rb,
lib/core_utilities/core_ext/array/filters.rb,
lib/core_utilities/core_ext/array/args_and_opts.rb,
lib/core_utilities/core_ext/array/flatten_splat.rb,
lib/core_utilities/core_ext/array/merge_options.rb,
lib/core_utilities/core_ext/array/extract_options.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object (private)



25
26
27
28
29
30
31
32
33
34
# File 'lib/core_utilities/core_ext/array/args_and_opts.rb', line 25

def method_missing(method_sym, *arguments, &block)
  return super unless Object.const_defined?(:REGEX) and method_sym =~ /^#{%w(arg(?:ument)?s opt(?:ion)?s).zip(Array.new(2, ::REGEX[:with_method_name].source)).map(&:join).join('_and_')}!$/
  args_and_opts.tap do |argopts|
    [ $1, $2 ].each_with_index do |meth, i|
      next unless meth.significant?
      params = (that_meth = (obj = argopts[-i]).method(meth.intern)).parameters
      argopts[-i] = (meth && obj.if?([ :respond_to?, meth.intern ]).send(*(i == 0 ? meth.intern : [ meth.intern, *arguments.constrained_by_archetype_and_method(obj, meth) ])))
    end
  end # end args_and_opts.tap
end

Instance Method Details

#arguments_and_options(update_hash = {}) ⇒ Object



2
3
4
# File 'lib/core_utilities/core_ext/array/args_and_opts.rb', line 2

def arguments_and_options(update_hash={})
  merge_options(update_hash).args_and_opts!
end

#arguments_and_options!(update_hash = {}) ⇒ Object



6
7
8
# File 'lib/core_utilities/core_ext/array/args_and_opts.rb', line 6

def arguments_and_options!(update_hash={})
  [ extract_options_with_merge!(update_hash), self ].rotate
end

#constrained_by_archetype_and_method(archetype, method_name) ⇒ Object

duplicated logic from argumentation



15
16
17
18
19
20
21
22
# File 'lib/core_utilities/core_ext/array/args_and_opts.rb', line 15

def constrained_by_archetype_and_method(archetype, method_name)
  # all_proc = lambda { |type, m| type != :rest }
  limit = [ archetype.method(method_name).parameters.if?(".all? { |type, m| type != :rest }", :else => (1.0/0)).count,
            archetype.method(method_name).arity.abs ].max
  take(limit.unless?(:finite?) { self.size })
rescue
  self
end

#extract_optionsObject



2
3
4
# File 'lib/core_utilities/core_ext/array/extract_options.rb', line 2

def extract_options
  options_extractable? ? last : {}
end

#extract_options!Object



6
7
8
# File 'lib/core_utilities/core_ext/array/extract_options.rb', line 6

def extract_options!
  options_extractable? ? pop  : {}
end

#extract_options_with_merge(update_hash = {}) ⇒ Object



14
15
16
# File 'lib/core_utilities/core_ext/array/extract_options.rb', line 14

def extract_options_with_merge(update_hash={})
  extract_options_without_merge.merge(update_hash  || {})
end

#extract_options_with_merge!(update_hash = {}) ⇒ Object



19
20
21
# File 'lib/core_utilities/core_ext/array/extract_options.rb', line 19

def extract_options_with_merge!(update_hash={})
  extract_options_without_merge!.merge(update_hash || {})
end

#flatten_splat(with_bang = false) ⇒ Object

flatten_splat! is used for dealing conveniently with the common pattern where a method’s arguments has a splat (*) operator, but the developer wants to provide the option of the method accepting either a list or an array for the argument. Typically dealt with in the following manner:

BEFORE: NOW: def do_something(*args) def do_something(*args)

args = args.shift if args.one? && args.first.is_a?(Array)           args.flatten_splat!

end end



11
12
13
# File 'lib/core_utilities/core_ext/array/flatten_splat.rb', line 11

def flatten_splat(with_bang=false)
  flatten_splat_needed? ? with_bang ? flatten! : flatten : self
end

#flatten_splat!Object



15
16
17
# File 'lib/core_utilities/core_ext/array/flatten_splat.rb', line 15

def flatten_splat!
  flatten_splat(true)
end

#include_all?(*other_ary) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/core_utilities/core_ext/array/base.rb', line 25

def include_all?(*other_ary)
  other_ary.flatten_splat!
  include_one(other_ary) || superset?(other_ary)
end

#is_excluded_from?(other_ary) ⇒ Boolean

Careful with this. This means 4 out of 5 elements can be the same but if not all of this self Array object are a part of other_ary, this returns true, as in “Yes, depite having 4 out of 5 in other_ary, I am excluded from the other_ary.”

Returns:

  • (Boolean)


19
20
21
22
# File 'lib/core_utilities/core_ext/array/base.rb', line 19

def is_excluded_from?(other_ary)
  other_ary.is_a?(Array) or raise ArgumentError, "Other array in argument must be an Array"
  super || not_subset?(other_ary)
end

#is_included_in?(other_ary) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/core_utilities/core_ext/array/base.rb', line 12

def is_included_in?(other_ary)
  other_ary.is_a?(Array) or raise ArgumentError, "Other array in argument must be an Array"
  super || subset?(other_ary)
end

#merge_options(update_hash = {}) ⇒ Object Also known as: merge_opts



2
3
4
5
# File 'lib/core_utilities/core_ext/array/merge_options.rb', line 2

def merge_options(update_hash={})
  endex, base_hash = options_extractable? ? [ -2, last ] : [ -1, {} ]
  Array[ *self[0..endex], base_hash.merge(update_hash || {}) ]
end

#merge_options!(update_hash = {}) ⇒ Object Also known as: merge_opts!



8
9
10
# File 'lib/core_utilities/core_ext/array/merge_options.rb', line 8

def merge_options!(update_hash={})
  push(extract_options!(update_hash))
end

#options_extractable?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/core_utilities/core_ext/array/extract_options.rb', line 10

def options_extractable?
  last.is_a?(Hash) && last.extractable_options?
end

#rearranges?(*other_ary) ⇒ Boolean

Compares two arrays to see if the elements are same but simply rearranged.

Returns:

  • (Boolean)


3
4
5
# File 'lib/core_utilities/core_ext/array/base.rb', line 3

def rearranges?(*other_ary)
  Set.new(self) == Set.new(other_ary.flatten_splat)
end

#reject_kind_of(klass) ⇒ Object



6
# File 'lib/core_utilities/core_ext/array/filters.rb', line 6

def reject_kind_of(klass);   reject { |el| el.kind_of?(klass) }; end

#reject_kind_of!(klass) ⇒ Object



8
# File 'lib/core_utilities/core_ext/array/filters.rb', line 8

def reject_kind_of!(klass); reject! { |el| el.kind_of?(klass) }; end

#select_kind_of(klass) ⇒ Object

Quick selectors/filters ##



5
# File 'lib/core_utilities/core_ext/array/filters.rb', line 5

def select_kind_of(klass);   select { |el| el.kind_of?(klass) }; end

#select_kind_of!(klass) ⇒ Object



7
# File 'lib/core_utilities/core_ext/array/filters.rb', line 7

def select_kind_of!(klass); select! { |el| el.kind_of?(klass) }; end

#subset?(other_ary) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
# File 'lib/core_utilities/core_ext/array/base.rb', line 7

def subset?(other_ary)
  other_ary.is_a?(Array) or raise ArgumentError, "Other array in argument must be an Array"
  Set.new(self).subset?(Set.new(other_ary))
end