Class: Array

Inherits:
Object show all
Defined in:
lib/justools/core_ext/array/filters.rb,
lib/justools/core_ext/array/inquiry.rb,
lib/justools/core_ext/array/args_and_opts.rb,
lib/justools/core_ext/array/flatten_splat.rb,
lib/justools/core_ext/array/merge_options.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_symbol, *args, &block) ⇒ Object (private)



33
34
35
36
37
38
39
40
# File 'lib/justools/core_ext/array/args_and_opts.rb', line 33

def method_missing(method_symbol, *args, &block)
  return super unless method_symbol =~ Justools::ARGS_AND_OPTS_REGEXP

  [$1, $2].zip(args_and_opts).map do |m, obj|
    next if m.respond_to?(:empty?) ? empty? : !m
    obj.send(m.to_sym, *args.send(:constrained_by_method, obj.method(m.to_sym)))
  end
end

Instance Method Details

#arguments_and_options(update_hash = {}) ⇒ Object



16
17
18
# File 'lib/justools/core_ext/array/args_and_opts.rb', line 16

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

#arguments_and_options!(update_hash = {}) ⇒ Object



20
21
22
# File 'lib/justools/core_ext/array/args_and_opts.rb', line 20

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

#extract_options_with_merge(update_hash = {}) ⇒ Object Also known as: extract_options



23
24
25
# File 'lib/justools/core_ext/array/merge_options.rb', line 23

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

#extract_options_with_merge!(update_hash = {}) ⇒ Object Also known as: extract_options!



28
29
30
# File 'lib/justools/core_ext/array/merge_options.rb', line 28

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

#extract_options_without_mergeObject



11
12
13
# File 'lib/justools/core_ext/array/merge_options.rb', line 11

def extract_options_without_merge
  options_extractable? ? last : {}
end

#extract_options_without_merge!Object



15
16
17
# File 'lib/justools/core_ext/array/merge_options.rb', line 15

def extract_options_without_merge!
  options_extractable? ? pop  : {}
end

#flatten_splat(with_bang = false) ⇒ Object



19
20
21
# File 'lib/justools/core_ext/array/flatten_splat.rb', line 19

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

#flatten_splat!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.

Examples

Before:                                 Now:
def do_something(*args)                 def do_something(*args)
  if args.one? && args[0].is_a?(Array)    args.flatten_splat!
    args = args.shift                   end
  end
end


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

def flatten_splat!
  flatten_splat(true)
end

#include_all?(*other_ary) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/justools/core_ext/array/inquiry.rb', line 37

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

#is_included_in?(other_ary) ⇒ Boolean Also known as: in?

Returns:

  • (Boolean)


22
23
24
# File 'lib/justools/core_ext/array/inquiry.rb', line 22

def is_included_in?(other_ary)
  super || subset?(other_ary)
end

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

Merging options



35
36
37
38
# File 'lib/justools/core_ext/array/merge_options.rb', line 35

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

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



41
42
43
# File 'lib/justools/core_ext/array/merge_options.rb', line 41

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

#options_extractable?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/justools/core_ext/array/merge_options.rb', line 19

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

#out_of?(other_ary) ⇒ Boolean Also known as: is_excluded_from?

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)


31
32
33
# File 'lib/justools/core_ext/array/inquiry.rb', line 31

def out_of?(other_ary)
  super || not_subset?(other_ary)
end

#rearranged?(*other_ary) ⇒ Boolean Also known as: rearranges?

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

Parameters

  • other_ary - An Array or a list of its elements.

Returns:

  • (Boolean)


12
13
14
# File 'lib/justools/core_ext/array/inquiry.rb', line 12

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

#reject_kind_of(klass) ⇒ Object



4
# File 'lib/justools/core_ext/array/filters.rb', line 4

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

#reject_kind_of!(klass) ⇒ Object



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

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

#rotateObject



11
12
13
# File 'lib/justools/core_ext/array/args_and_opts.rb', line 11

def rotate
  self[0..-2].unshift(self[-1])
end

#select_kind_of(klass) ⇒ Object

Quick selectors/filters



3
# File 'lib/justools/core_ext/array/filters.rb', line 3

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

#select_kind_of!(klass) ⇒ Object



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

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

#subset_of?(other_ary) ⇒ Boolean Also known as: subset?

Returns:

  • (Boolean)


17
18
19
# File 'lib/justools/core_ext/array/inquiry.rb', line 17

def subset_of?(other_ary)
  Set.new(self).subset?(Set.new(other_ary))
end