Class: TsJsonApi::Logging::File
- Inherits:
-
Object
- Object
- TsJsonApi::Logging::File
- Extended by:
- ActiveModel::Naming
- Defined in:
- lib/ts_json_api/logging/file.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #basename ⇒ Object
- #contents ⇒ Object
-
#initialize(options = {}) ⇒ File
constructor
A new instance of File.
-
#to_key ⇒ Object
Support for dom_id in views.
- #write(content) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ File
Returns a new instance of File.
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 = @url = [:url] relative_path = [:relative_path] || '' if relative_path.blank? @path = [:path] unless [:path].blank? else unless relative_path.end_with? '.log' relative_path.gsub! ".log", '' if Configure. 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
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/ts_json_api/logging/file.rb', line 7 def @options end |
#path ⇒ Object (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
#basename ⇒ Object
49 50 51 |
# File 'lib/ts_json_api/logging/file.rb', line 49 def basename ::File.basename(@path) end |
#contents ⇒ Object
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_key ⇒ Object
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 |