Class: TsJsonApi::Logging::File

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Defined in:
lib/ts_json_api/logging/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ File

Returns a new instance of File.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ts_json_api/logging/file.rb', line 9

def initialize(options={})

  @options = options
  @url = options[:url]

  relative_path = options[:relative_path] || ''
  if relative_path.blank? 
    @path = options[:path] unless options[:path].blank?
  else
    unless relative_path.end_with? '.log'

      relative_path.gsub! ".log", ''
      if Configure.timestamped_logs?
        relative_path << "_t#{Time.now.to_i}.log"
      else
        relative_path << ".log"
      end

    end

    @path = Configure::LOG_FILE_DIRECTORY.join(relative_path)
  end

  raise ArgumentError, 'need help finding a path!' if @path.blank?

end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/ts_json_api/logging/file.rb', line 7

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/ts_json_api/logging/file.rb', line 7

def path
  @path
end

Instance Method Details

#basenameObject



49
50
51
# File 'lib/ts_json_api/logging/file.rb', line 49

def basename
  ::File.basename(@path)
end

#contentsObject



42
43
44
45
46
47
# File 'lib/ts_json_api/logging/file.rb', line 42

def contents
  raise 'Only available when reading a file' unless @options[:readonly]
  json = JSON.parse(IO.read path).symbolize_keys
  json[:content] = JSON.parse(json[:content])
  json
end

#to_keyObject

Support for dom_id in views



54
55
56
# File 'lib/ts_json_api/logging/file.rb', line 54

def to_key
  [basename.parameterize]
end

#write(content) ⇒ Object



36
37
38
39
40
# File 'lib/ts_json_api/logging/file.rb', line 36

def write(content)
  raise 'Cannot write to log file if opened as readonly' if @options[:readonly]
  create_dir_if_not_exists
  ::File.open(@path, 'w') { |f| f.write content_string(content) }
end