Class: ConfigPlus::Collection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/config_plus/collection.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ Collection

Returns a new instance of Collection.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/config_plus/collection.rb', line 16

def initialize(collection)
  @hash_data = nil
  @array_data = nil
  data = retrieve_data_out_of(collection)

  case data
  when Hash
    @hash_data = {}
  when Array
    @array_data = []
  else
    raise TypeError, "An argument should be Hash or Array " \
                     "but #{collection.class.name}"
  end
end

Class Method Details

.generate_for(collection) ⇒ Object



186
187
188
# File 'lib/config_plus/collection.rb', line 186

def generate_for(collection)
  new(collection)
end

Instance Method Details

#[](key) ⇒ Object



32
33
34
# File 'lib/config_plus/collection.rb', line 32

def [](key)
  self.fetch(key, nil)
end

#array?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/config_plus/collection.rb', line 160

def array?
  !!array_data
end

#dataObject



152
153
154
# File 'lib/config_plus/collection.rb', line 152

def data
  hash_data || array_data
end

#each_keyObject



82
83
84
85
86
87
88
# File 'lib/config_plus/collection.rb', line 82

def each_key
  if hash?
    hash_data.each_key
  else
    (0...array_data.size).each
  end
end

#each_pairObject



90
91
92
93
94
95
96
97
98
# File 'lib/config_plus/collection.rb', line 90

def each_pair
  if hash?
    hash_data.each_pair
  else
    array_data.lazy.with_index.map {|v, n|
      [n, v]
    }.each
  end
end

#fetch(*arguments) ⇒ Object

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/config_plus/collection.rb', line 36

def fetch(*arguments)
  raise ArgumentError if arguments.size > 2 or arguments.empty?
  args = arguments.dup
  args[0] = args[0].to_s

  if array? and args[0] =~ /\A\d+\z/
    args[0] = args[0].to_i
    data.fetch(*args)
  elsif hash?
    v = data.fetch(*args)
    if !v and args[0] =~ /\A\d+\z/
      args[0] = args[0].to_i
      v = data.fetch(*args)
    end
    v
  else
    raise
  end
end

#hash?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/config_plus/collection.rb', line 156

def hash?
  !!hash_data
end

#key?(val) ⇒ Boolean Also known as: has_key?

Returns:

  • (Boolean)


108
109
110
111
112
113
114
115
116
# File 'lib/config_plus/collection.rb', line 108

def key?(val)
  if hash?
    hash_data.key?(val.to_s)
  elsif val.to_s =~ /\A\d+\z/
    val.to_i.between?(0, array_data.size - 1)
  else
    false
  end
end

#keysObject



100
101
102
103
104
105
106
# File 'lib/config_plus/collection.rb', line 100

def keys
  if hash?
    hash_data.keys
  else
    Array.new(array_data, &:to_i)
  end
end

#merge(collection) ⇒ Object

Raises:

  • (TypeError)


56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/config_plus/collection.rb', line 56

def merge(collection)
  raise TypeError, "An argument should be an instance of #{data.class.name}" \
                   " but #{collection.class.name}" if
    self.miss_match?(collection)
  data = retrieve_data_out_of collection

  if hash?
    hash_data.merge(data)
  else
    array_data + data
  end
end

#merge!(collection) ⇒ Object

Raises:

  • (TypeError)


69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/config_plus/collection.rb', line 69

def merge!(collection)
  raise TypeError, "An argument should be an instance of #{data.class.name}" \
                   " but #{collection.class.name}" if
    self.miss_match?(collection)
  data = retrieve_data_out_of collection

  if hash?
    hash_data.merge!(data)
  else
    array_data.concat(data)
  end
end

#store(key, val) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/config_plus/collection.rb', line 119

def store(key, val)
  if hash?
    hash_data.store(key.to_s, val)
  elsif key.to_s =~ /\A\d+\z/
    array_data[key.to_i] = val
  end
end

#to_hashObject



127
128
129
130
131
132
133
# File 'lib/config_plus/collection.rb', line 127

def to_hash
  if hash?
    hash_data.to_hash
  else
    Hash.try_convert(data)
  end
end

#value?(val) ⇒ Boolean Also known as: has_value?

Returns:

  • (Boolean)


135
136
137
138
139
140
141
# File 'lib/config_plus/collection.rb', line 135

def value?(val)
  if hash?
    hash_data.value?(val)
  else
    array_data.include?(val)
  end
end

#valuesObject



144
145
146
147
148
149
150
# File 'lib/config_plus/collection.rb', line 144

def values
  if hash?
    hash_data.values
  else
    array_data
  end
end