Module: ActiveJson::Base

Defined in:
lib/active_json/base.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



8
9
10
# File 'lib/active_json/base.rb', line 8

def method_missing(method_name, *args, &block)
  database.public_send(method_name, *args, &block)
end

Instance Method Details

#[](index) ⇒ Object



16
17
18
# File 'lib/active_json/base.rb', line 16

def [](index)
  database.dig(index)
end

#configure {|@config| ... } ⇒ Object

Yields:

  • (@config)


3
4
5
6
# File 'lib/active_json/base.rb', line 3

def configure
  @config ||= OpenStruct.new(path: nil, readonly: false)
  yield(@config)
end

#inspectObject



38
39
40
# File 'lib/active_json/base.rb', line 38

def inspect
  database.to_h
end

#reloadObject



20
21
22
23
24
# File 'lib/active_json/base.rb', line 20

def reload
  @database = nil

  database
end

#respond_to_missing?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/active_json/base.rb', line 12

def respond_to_missing?(method_name, *)
  database.respond_to?(method_name)
end

#saveObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/active_json/base.rb', line 26

def save
  raise ActiveJson::ReadOnlyDatabase if @config[:readonly]

  result = JSON.pretty_generate(database.to_h)

  File.open(@config[:path], 'w') do |file|
    file.write(result)
  end

  true
end