class ConfigPlus::Collection
Attributes
array_data[R]
hash_data[R]
Private Class Methods
generate_for(collection)
click to toggle source
# File lib/config_plus/collection.rb, line 186 def generate_for(collection) new(collection) end
new(collection)
click to toggle source
# 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
Public Instance Methods
[](key)
click to toggle source
# File lib/config_plus/collection.rb, line 32 def [](key) self.fetch(key, nil) end
array?()
click to toggle source
# File lib/config_plus/collection.rb, line 160 def array? !!array_data end
data()
click to toggle source
# File lib/config_plus/collection.rb, line 152 def data hash_data || array_data end
each_key()
click to toggle source
# 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_pair()
click to toggle source
# 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)
click to toggle source
# 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?()
click to toggle source
# File lib/config_plus/collection.rb, line 156 def hash? !!hash_data end
key?(val)
click to toggle source
# 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
Also aliased as: has_key?
keys()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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_hash()
click to toggle source
# 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)
click to toggle source
# File lib/config_plus/collection.rb, line 135 def value?(val) if hash? hash_data.value?(val) else array_data.include?(val) end end
Also aliased as: has_value?
values()
click to toggle source
# File lib/config_plus/collection.rb, line 144 def values if hash? hash_data.values else array_data end end
Protected Instance Methods
miss_match?(collection)
click to toggle source
# File lib/config_plus/collection.rb, line 166 def miss_match?(collection) data.class != collection.class && !collection.is_a?(self.class) end
Private Instance Methods
retrieve_data_out_of(object)
click to toggle source
# File lib/config_plus/collection.rb, line 174 def retrieve_data_out_of(object) case object when Hash, Array object when self.class object.data else raise TypeError, "#{object.class.name} could not be acceptable" end end