Class: Episode::Config
- Inherits:
-
Object
- Object
- Episode::Config
- Defined in:
- lib/episode/config.rb
Instance Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
- #dir ⇒ Object
- #dir=(new_val) ⇒ Object
- #global ⇒ Object
- #global? ⇒ Boolean
-
#initialize(opts = {}) ⇒ Config
constructor
A new instance of Config.
- #last ⇒ Object
- #last=(new_val) ⇒ Object
- #last_played_at ⇒ Object
- #last_played_at=(new_val) ⇒ Object
- #save(io) ⇒ Object
- #to_h(remove_defaults: false) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Config
Returns a new instance of Config.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/episode/config.rb', line 25 def initialize(opts = {}) @global_cfg = opts[:global] unless global? @local = { dir: opts[:dir], last: opts[:last], last_played_at: (Time.parse opts[:last_played_at] rescue nil) } end @viewer = opts[:viewer] @index_from = opts[:index_from] @pointer = opts[:pointer] @formats = opts[:formats] end |
Instance Attribute Details
#formats ⇒ Object
115 116 117 |
# File 'lib/episode/config.rb', line 115 def formats @formats || default(:formats) end |
#index_from ⇒ Object
107 108 109 |
# File 'lib/episode/config.rb', line 107 def index_from @index_from || default(:index_from) end |
#pointer ⇒ Object
111 112 113 |
# File 'lib/episode/config.rb', line 111 def pointer @pointer || default(:pointer) end |
#viewer ⇒ Object
103 104 105 |
# File 'lib/episode/config.rb', line 103 def viewer @viewer || default(:viewer) end |
Class Method Details
.load(io, opts = {}) ⇒ Object
21 22 23 |
# File 'lib/episode/config.rb', line 21 def self.load(io, opts = {}) new JSON.parse(io.read, symbolize_names: true).merge(opts) end |
Instance Method Details
#dir ⇒ Object
79 80 81 |
# File 'lib/episode/config.rb', line 79 def dir get_local :dir end |
#dir=(new_val) ⇒ Object
83 84 85 |
# File 'lib/episode/config.rb', line 83 def dir=(new_val) set_local :dir, new_val end |
#global ⇒ Object
75 76 77 |
# File 'lib/episode/config.rb', line 75 def global @global_cfg end |
#global? ⇒ Boolean
71 72 73 |
# File 'lib/episode/config.rb', line 71 def global? @global_cfg.nil? end |
#last ⇒ Object
87 88 89 |
# File 'lib/episode/config.rb', line 87 def last get_local :last end |
#last=(new_val) ⇒ Object
91 92 93 |
# File 'lib/episode/config.rb', line 91 def last=(new_val) set_local :last, new_val end |
#last_played_at ⇒ Object
95 96 97 |
# File 'lib/episode/config.rb', line 95 def last_played_at get_local :last_played_at end |
#last_played_at=(new_val) ⇒ Object
99 100 101 |
# File 'lib/episode/config.rb', line 99 def last_played_at=(new_val) set_local :last_played_at, new_val end |
#save(io) ⇒ Object
43 44 45 |
# File 'lib/episode/config.rb', line 43 def save(io) io.write JSON.pretty_generate(to_h(remove_defaults: true)) end |
#to_h(remove_defaults: false) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/episode/config.rb', line 47 def to_h(remove_defaults: false) if remove_defaults (@local || {}).merge({ viewer: @viewer, index_from: @index_from, pointer: @pointer, formats: @formats }).compact else local_h = { dir: dir, last: last, last_played_at: last_played_at } unless global? (local_h || {}).merge({ viewer: viewer, index_from: index_from, pointer: pointer, formats: formats }) end end |