Module: TinyDot
- Defined in:
- lib/tiny_dot.rb
Overview
The TinyDot module provides methods for converting data from different formats (environment variables, YAML, JSON and Hashes) into a nested Struct. This allows for easy access to the data using dot notation.
The from_env method converts the current environment variables into a Struct.
The from_yaml method takes a string containing YAML data and converts it into a Struct.
The from_json method takes a string containing JSON data and converts it into a Struct.
The from_csv method takes a string containing raw CSV data and converts it to an Array of Struct.
The from_hash method takes a Hash and converts it into a Struct. It really just calls the _hash_to_struct method.
The _hash_to_struct is a private method that is used by the other methods to convert the data from a hash into a Struct. It handles nested data by recursively calling itself when it encounters a Hash or an array within the data.
The _to_attr_friendly_symbols is a private method that is used to convert keys in the hash to symbols that can be used as attributes in a Struct. It replaces spaces and dashes with underscores and converts the keys to symbols.
Please note that the JSON and YAML are required to be passed as String.
Because this module constructs a series of nested Structs from Hash-like inputs, it means you can read/write data pretty easily:
> s = TinyDot.from_json(<some deeply nested JSON>)
=> <struct ... >
> s.foo..baz
=> 5
> s.foo..baz = 99
< s.foo..baz
=> 99
Class Method Summary collapse
- ._hash_to_struct(obj) ⇒ Object
- ._to_attr_friendly_symbols(keys) ⇒ Object
- .from_csv(csv_string) ⇒ Object
- .from_env ⇒ Object
- .from_hash(hash) ⇒ Object
- .from_json(json_string) ⇒ Object
- .from_yaml(yaml_string) ⇒ Object
Class Method Details
._hash_to_struct(obj) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/tiny_dot.rb', line 69 def _hash_to_struct(obj) case obj when Hash return obj if obj.keys.empty? struct = ::Struct.new(*_to_attr_friendly_symbols(obj.keys)) struct.new(*obj.values.map { |v| _hash_to_struct(v) }) when Array obj.map { |v| _hash_to_struct(v) } else obj end end |
._to_attr_friendly_symbols(keys) ⇒ Object
83 84 85 86 87 |
# File 'lib/tiny_dot.rb', line 83 def _to_attr_friendly_symbols(keys) keys .map{|k| k.to_s.gsub(/[-\s]/, '_') } .map(&:to_sym) end |
.from_csv(csv_string) ⇒ Object
60 61 62 63 |
# File 'lib/tiny_dot.rb', line 60 def from_csv(csv_string) data = CSV.parse(csv_string, headers: true) data.map { |row| _hash_to_struct(row.to_hash) } end |
.from_env ⇒ Object
46 47 48 |
# File 'lib/tiny_dot.rb', line 46 def from_env _hash_to_struct(ENV.to_h) end |
.from_hash(hash) ⇒ Object
65 66 67 |
# File 'lib/tiny_dot.rb', line 65 def from_hash(hash) _hash_to_struct(hash) end |
.from_json(json_string) ⇒ Object
55 56 57 58 |
# File 'lib/tiny_dot.rb', line 55 def from_json(json_string) json = Oj.load(json_string) _hash_to_struct(json) end |
.from_yaml(yaml_string) ⇒ Object
50 51 52 53 |
# File 'lib/tiny_dot.rb', line 50 def from_yaml(yaml_string) yaml = Psych.load(yaml_string) _hash_to_struct(yaml) end |