Class: Harshed::Harsh
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#key_to_map ⇒ Object
readonly
Returns the value of attribute key_to_map.
Instance Method Summary collapse
- #filename(key) ⇒ Object
- #folder_path ⇒ Object
- #from_disk ⇒ Object
-
#initialize(key_to_map, items = [], storage_base: Harshed.default_base_folder, storage_folder: '') ⇒ Harsh
constructor
A new instance of Harsh.
- #key?(key) ⇒ Boolean
- #mkdir ⇒ Object
- #rm_r ⇒ Object
- #sample ⇒ Object
- #store(items) ⇒ Object
- #store_item(item) ⇒ Object
- #to_a ⇒ Object
-
#to_disk ⇒ Object
This method smells of :reek:NestedIterators.
- #valid_type?(item) ⇒ Boolean
- #validate_type(item) ⇒ Object
- #value(my_key) ⇒ Object
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
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
6 7 8 |
# File 'lib/harshed/harsh.rb', line 6 def hash @hash end |
#key_to_map ⇒ Object (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_path ⇒ Object
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_disk ⇒ Object
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
47 48 49 |
# File 'lib/harshed/harsh.rb', line 47 def key?(key) @hash.key?(key) end |
#mkdir ⇒ Object
75 76 77 |
# File 'lib/harshed/harsh.rb', line 75 def mkdir Dir.mkdir(folder_path) unless Dir.exist?(folder_path) end |
#rm_r ⇒ Object
79 80 81 |
# File 'lib/harshed/harsh.rb', line 79 def rm_r FileUtils.remove_dir folder_path end |
#sample ⇒ Object
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_a ⇒ Object
43 44 45 |
# File 'lib/harshed/harsh.rb', line 43 def to_a @hash.values end |
#to_disk ⇒ Object
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
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 |