Class: Harshed::Harsh

Inherits:
Object
  • Object
show all
Includes:
Yamlable
Defined in:
lib/harshed/harsh.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key_to_map, items = [], storage_base: Harshed.default_base_folder, storage_folder: '') ⇒ Harsh

Returns a new instance of Harsh.



8
9
10
11
12
13
14
15
16
# File 'lib/harshed/harsh.rb', line 8

def initialize(key_to_map, items = [],
               storage_base: Harshed.default_base_folder,
               storage_folder: '')
  @key_to_map = key_to_map
  @hash = {}
  @storage_folder = storage_folder
  @storage_base = storage_base
  store(items)
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



6
7
8
# File 'lib/harshed/harsh.rb', line 6

def hash
  @hash
end

#key_to_mapObject (readonly)

Returns the value of attribute key_to_map.



6
7
8
# File 'lib/harshed/harsh.rb', line 6

def key_to_map
  @key_to_map
end

Instance Method Details

#filename(key) ⇒ Object



91
92
93
# File 'lib/harshed/harsh.rb', line 91

def filename(key)
  File.join(folder_path, "#{key}.yml")
end

#folder_pathObject



83
84
85
86
87
88
89
# File 'lib/harshed/harsh.rb', line 83

def folder_path
  if @storage_folder.empty?
    File.join(@storage_base, @hash.values.first.class.to_s)
  else
    File.join(@storage_base, @storage_folder)
  end
end

#from_diskObject



59
60
61
62
63
64
65
# File 'lib/harshed/harsh.rb', line 59

def from_disk
  Dir["#{folder_path}/*.yml"].each do |filename|
    yml = File.read(filename)
    store(Psych.load(yml, filename))
  end
  self
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/harshed/harsh.rb', line 47

def key?(key)
  @hash.key?(key)
end

#mkdirObject



75
76
77
# File 'lib/harshed/harsh.rb', line 75

def mkdir
  Dir.mkdir(folder_path) unless Dir.exist?(folder_path)
end

#rm_rObject



79
80
81
# File 'lib/harshed/harsh.rb', line 79

def rm_r
  FileUtils.remove_dir folder_path
end

#sampleObject



55
56
57
# File 'lib/harshed/harsh.rb', line 55

def sample
  @hash.values.sample
end

#store(items) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/harshed/harsh.rb', line 18

def store(items)
  # Hack to get around how Array() mangles Hashes and Structs
  if items.is_a?(Array)
    items.each { |item| store_item(item) }
  else
    store_item(items)
  end
end

#store_item(item) ⇒ Object



27
28
29
30
# File 'lib/harshed/harsh.rb', line 27

def store_item(item)
  validate_type(item)
  @hash[item.send(@key_to_map)] = item
end

#to_aObject



43
44
45
# File 'lib/harshed/harsh.rb', line 43

def to_a
  @hash.values
end

#to_diskObject

This method smells of :reek:NestedIterators



68
69
70
71
72
73
# File 'lib/harshed/harsh.rb', line 68

def to_disk
  mkdir
  @hash.each do |key, value|
    File.open(filename(key), 'w') { |file| file.write(value.to_yaml) }
  end
end

#valid_type?(item) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/harshed/harsh.rb', line 32

def valid_type?(item)
  @hash.values.last.class == item.class
end

#validate_type(item) ⇒ Object



36
37
38
39
40
41
# File 'lib/harshed/harsh.rb', line 36

def validate_type(item)
  return if @hash.empty?
  unless valid_type?(item)
    raise TypeError "Harshed Object class types (#{last_class}/#{item_class}) must be the same."
  end
end

#value(my_key) ⇒ Object



51
52
53
# File 'lib/harshed/harsh.rb', line 51

def value(my_key)
  @hash[my_key]
end