Class: Array

Inherits:
Object show all
Defined in:
lib/corelib/array/math.rb,
lib/corelib/array/core.rb,
lib/corelib/array/alias.rb,
lib/corelib/array/helpers.rb

Overview

options = => ‘default’.merge(options)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.date_format_options(options = {}) ⇒ Object



22
23
24
25
# File 'lib/corelib/array/helpers.rb', line 22

def self.date_format_options(options={})
  exemplify = options.fetch(:exemplify, true)
  exemplify ? date_format_options_exemplified_raw(options) : date_format_options_raw(options)
end

.date_format_options_exemplified_raw(options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/corelib/array/helpers.rb', line 32

def self.date_format_options_exemplified_raw(options={})
  id = options.fetch(:start_id, 1)
  t = Time.now
  s = options.fetch(:separater, " - ")
  d = options.fetch(:day, t.strftime("%d"))
  m = options.fetch(:month, t.strftime("%m"))
  y = options.fetch(:year, t.year)
  [["MM/DD/YYYY#{s}(#{m}/#{d}/#{y})", id], ["MM-DD-YYYY#{s}(#{m}-#{d}-#{y})", id+1], ["DD/MM/YYYY#{s}(#{d}/#{m}/#{y})", id+2], ["DD-MM-YYYY#{s}(#{d}-#{m}-#{y})", id+3],
   ["YYYY/MM/DD#{s}(#{y}/#{m}/#{d})", id+4], ["YYYY-MM-DD#{s}(#{y}-#{m}-#{d})", id+5], ["DD.MM.YYYY#{s}(#{d}.#{m}.#{y})", id+6], ["YYYY.MM.DD#{s}(#{y}.#{m}.#{d})", id+7]]
end

.date_format_options_raw(options = {}) ⇒ Object



27
28
29
30
# File 'lib/corelib/array/helpers.rb', line 27

def self.date_format_options_raw(options={})
  id = options.fetch(:start_id, 1)
  [["MM/DD/YYYY", id], ["MM-DD-YYYY", id+1], ["DD/MM/YYYY", id+2], ["DD-MM-YYYY", id+3], ["YYYY/MM/DD", id+4], ["YYYY-MM-DD", id+5], ["DD.MM.YYYY", id+6], ["YYYY.MM.DD", id+7]]
end

.month_options(options = {}) ⇒ Object



43
44
45
46
47
# File 'lib/corelib/array/helpers.rb', line 43

def self.month_options(options={})
  abbrev = options.fetch(:abbrev, false)
  list = abbrev ? month_options_abbrev_raw : month_options_raw
  list.format_options_list(options)
end

.month_options_abbrev_raw(start = 1) ⇒ Object



54
55
56
57
# File 'lib/corelib/array/helpers.rb', line 54

def self.month_options_abbrev_raw(start=1)
  [["Jan", start], ["Feb", start + 1], ["Mar", start + 2], ["Apr", start + 3], ["May", start + 4], ["Jun", start + 5],
   ["Jul", start + 6], ["Aug", start + 7], ["Sep", start + 8], ["Oct", start + 9], ["Nov", start + 10], ["Dec", start + 11]]
end

.month_options_raw(start = 1) ⇒ Object



49
50
51
52
# File 'lib/corelib/array/helpers.rb', line 49

def self.month_options_raw(start=1)
  [["January", start], ["February", start + 1], ["March", start + 2], ["April", start + 3], ["May", start + 4], ["June",  start + 5],
   ["July", start + 6], ["August", start + 7], ["September", start + 8], ["October", start + 9], ["November", start + 10], ["December", start + 11]]
end

.time_clock_type_options(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/corelib/array/helpers.rb', line 8

def self.time_clock_type_options(options={})
  exemplify = options.fetch(:exemplify, true)
  id = options.fetch(:start_id, 1)
  return [["12-Hour Clock", id], ["24-Hour Clock", id+1]] if !exemplify

  t = Time.now
  s = options.fetch(:separater, " - ")
  h = options.fetch(:day, t.strftime("%l"))
  m = options.fetch(:month, t.strftime("%m"))
  ap =options.fetch(:am_pm, t.strftime("%P"))
  hm = options.fetch(:day24, t.strftime("%H"))
  [["12-Hour Clock#{s}(#{h}:#{m}#{ap})", id], ["24-Hour Clock#{s}(#{hm}:#{m})", id+1]]
end

.us_state_options(options = {}) ⇒ Object



59
60
61
62
63
# File 'lib/corelib/array/helpers.rb', line 59

def self.us_state_options(options={})
  abbrev = options.fetch(:abbrev, false)
  list =  abbrev ? us_state_options_abbrev_raw : us_state_options_raw
  list.format_options_list(options)
end

.us_state_options_abbrev_rawObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/corelib/array/helpers.rb', line 77

def self.us_state_options_abbrev_raw
  [['AL', 'AL'], ['AK', 'AK'], ['AZ', 'AZ'], ['AR', 'AR'], ['CA', 'CA'], ['CO', 'CO'],
   ['CT', 'CT'], ['DC', 'DC'], ['DE', 'DE'], ['FL', 'FL'], ['GA', 'GA'], ['HI', 'HI'], ['ID', 'ID'],
   ['IL', 'IL'], ['IN', 'IN'], ['IA', 'IA'], ['KS', 'KS'], ['KY', 'KY'], ['LA', 'LA'],
   ['ME', 'ME'], ['MD', 'MD'], ['MA', 'MA'], ['MI', 'MI'], ['MN', 'MN'], ['MS', 'MS'],
   ['MO', 'MO'], ['MT', 'MT'], ['NE', 'NE'], ['NV', 'NV'], ['NH', 'NH'], ['NJ', 'NJ'],
   ['NM', 'NM'], ['NY', 'NY'], ['NC', 'NC'], ['ND', 'ND'], ['OH', 'OH'], ['OK', 'OK'],
   ['OR', 'OR'], ['PA', 'PA'], ['RI', 'RI'], ['SC', 'SC'], ['SD', 'SD'],
   ['TN', 'TN'], ['TX', 'TX'], ['UT', 'UT'], ['VT', 'VT'], ['VA', 'VA'], ['WA', 'WA'],
   ['WV', 'WV'], ['WI', 'WI'], ['WY', 'WY'],]
end

.us_state_options_rawObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/corelib/array/helpers.rb', line 65

def self.us_state_options_raw
  [['Alabama', 'AL'], ['Alaska', 'AK'], ['Arizona', 'AZ'], ['Arkansas', 'AR'], ['California', 'CA'], ['Colorado', 'CO'],
   ['Connecticut', 'CT'], ['D.C.', 'DC'], ['Delaware', 'DE'], ['Florida', 'FL'], ['Georgia', 'GA'], ['Hawaii', 'HI'], ['Idaho', 'ID'],
   ['Illinois', 'IL'], ['Indiana', 'IN'], ['Iowa', 'IA'], ['Kansas', 'KS'], ['Kentucky', 'KY'], ['Louisiana', 'LA'],
   ['Maine', 'ME'], ['Maryland', 'MD'], ['Massachusetts', 'MA'], ['Michigan', 'MI'], ['Minnesota', 'MN'], ['Mississippi', 'MS'],
   ['Missouri', 'MO'], ['Montana', 'MT'], ['Nebraska', 'NE'], ['Nevada', 'NV'], ['New Hampshire', 'NH'], ['New Jersey', 'NJ'],
   ['New Mexico', 'NM'], ['New York', 'NY'], ['North Carolina', 'NC'], ['North Dakota', 'ND'], ['Ohio', 'OH'], ['Oklahoma', 'OK'],
   ['Oregon', 'OR'], ['Pennsylvania', 'PA'], ['Rhode Island', 'RI'], ['South Carolina', 'SC'], ['South Dakota', 'SD'],
   ['Tennessee', 'TN'], ['Texas', 'TX'], ['Utah', 'UT'], ['Vermont', 'VT'], ['Virginia', 'VA'], ['Washington', 'WA'],
   ['West Virginia', 'WV'], ['Wisconsin', 'WI'], ['Wyoming', 'WY']]
end

Instance Method Details

#add_all(arr, options = {}) ⇒ Object



33
34
35
36
37
# File 'lib/corelib/array/core.rb', line 33

def add_all(arr, options={})
  flatten = options.fetch(:flatten, true)
  a = flatten ? arr.flatten : arr
  self.concat(a)
end

#add_blank_option(options = {}) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/corelib/array/core.rb', line 11

def add_blank_option(options={})
  doit = options.fetch(:doit, true)
  value = options.fetch(:value, 0)
  label = options.fetch(:label, "")
  self.insert(0, [label, value]) if doit
  self
end

#each_with_end_flagObject

This method iterates over the Array as normal #each method. For each iteration set two variables in the block, |item, flag|. item will be set tot he current item in the iteration; flag will be set to “false” on all iterations except the last iteration



22
23
24
25
26
27
# File 'lib/corelib/array/core.rb', line 22

def each_with_end_flag
  my_size = self.size
  self.each_with_index do |item, index|
    index + 1 == my_size ? yield(item, true) : yield(item, false)
  end
end

#extract_options!Object



3
4
5
# File 'lib/corelib/array/core.rb', line 3

def extract_options!
  last.is_a?(::Hash) ? pop : {}
end

#format_options_list(options = {}) ⇒ Object



89
90
91
92
93
# File 'lib/corelib/array/helpers.rb', line 89

def format_options_list(options={})
  insert_options_label_blank(options)
  change_options_label_case(options) if options[:format] #Change case if :format is set, otherwise use default case

  self
end

#not_empty?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/corelib/array/core.rb', line 29

def not_empty?
  !self.empty?
end

#options_label_for(value) ⇒ Object



3
4
5
6
# File 'lib/corelib/array/helpers.rb', line 3

def options_label_for(value)
  sub = self.find {|each| each[1] == value}
  sub.nil? ? nil : sub[0]
end

#sum(options = {}) ⇒ Object



5
6
7
8
# File 'lib/corelib/array/math.rb', line 5

def sum (options = {})
  strict = options.fetch(:strict, true)    
  strict ? sum_strict : sum_loose
end

#to_yes_no(options = {}) ⇒ Object



7
8
9
# File 'lib/corelib/array/core.rb', line 7

def to_yes_no(options={})
  self.collect {|e| e.to_yes_no(options)}
end