Class: TinyDot

Inherits:
Object
  • Object
show all
Defined in:
lib/tiny_dot.rb

Overview

this class gives you read-only access to Hases, JSON, and YAML files using dot notation. it makes clever use of #method_missing to allow you to do the following:

 > t = TinyDot.new({ 'foo' => { 'bar' => 'baz' }})
 > t.foo                                               # returns another instance of TinyDot
=> #<TinyDot:0x0201243 @data={ 'bar' => 'baz' }>
 > t.foo!                                              # ! returns the value under the chain of keys
=> { 'bar' => 'baz' }

… in other words, it gives you a convenient dot notation syntax for accessing nested hashes. you can chain calls to this and you’ll get a new object that’s essentially a #dig into the top-level hash.

if you add ‘!’ to the last method in a chain it will return the value under that key.

finally, you can use dot syntax as deep as you want, and if there’s no key at that level you’ll just get ‘nil’ back:

 > t.foo.bar.baz.whatever.as.deep.as.you.want
=> nil

… which is sort of safe navigation operator-like without the save navigation operator

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ TinyDot

give it a Hash and it’ll give you dot notation over it



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

def initialize(hash)
  @data = hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/tiny_dot.rb', line 55

def method_missing(m, *args)
  val = args.first
  ms = m.to_s

  case @data
  when Hash
    if ms.end_with?('!')
      @data[ms[0..-2]]
    elsif ms.end_with?('=')
      @data[m[0..-2]] = val
    else
      if @data.has_key?(ms)
        TinyDot.new(@data[ms])
      else
        TinyDot.new({})
      end
    end
  else
    TinyDot.new({})
  end
end

Class Method Details

.from_envObject

returns a TinyDot instance from the ENV constant



31
32
33
# File 'lib/tiny_dot.rb', line 31

def self.from_env
  TinyDot.new(ENV)
end

.from_json_file(filename) ⇒ Object

returns a TinyDot instance after parsing the JSON in the named filename



41
42
43
# File 'lib/tiny_dot.rb', line 41

def self.from_json_file(filename)
  TinyDot.new(JSON.parse(IO.read(filename)))
end

.from_json_string(s) ⇒ Object

returns a TinyDot instance after parsing the JSON in the string



46
47
48
# File 'lib/tiny_dot.rb', line 46

def self.from_json_string(s)
  TinyDot.new(JSON.parse(s))
end

.from_yaml_file(filename, permitted_classes: []) ⇒ Object

returns a TinyDot instance after parsing the YAML in the named filename



36
37
38
# File 'lib/tiny_dot.rb', line 36

def self.from_yaml_file(filename, permitted_classes: [])
  TinyDot.new(YAML.safe_load_file(filename, permitted_classes: permitted_classes))
end

Instance Method Details

#to_hashObject

NOTE: this returns the raw hash inside of the TinyDot instance, which means if you modify anything here you’re modifying the internal data in this TinyDot instance



84
85
86
# File 'lib/tiny_dot.rb', line 84

def to_hash
  @data
end

#to_jsonObject



77
78
79
# File 'lib/tiny_dot.rb', line 77

def to_json
  @data.to_json
end

#to_yamlObject



88
89
90
# File 'lib/tiny_dot.rb', line 88

def to_yaml
  @data.to_yaml
end