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.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/episode/config.rb', line 24 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_zero = opts[:index_from_zero] @pointer = opts[:pointer] @formats = opts[:formats] end |
Instance Attribute Details
#formats ⇒ Object
114 115 116 |
# File 'lib/episode/config.rb', line 114 def formats @formats || default(:formats) end |
#index_from_zero ⇒ Object
106 107 108 |
# File 'lib/episode/config.rb', line 106 def index_from_zero @index_from_zero || default(:index_from_zero) end |
#pointer ⇒ Object
110 111 112 |
# File 'lib/episode/config.rb', line 110 def pointer @pointer || default(:pointer) end |
#viewer ⇒ Object
102 103 104 |
# File 'lib/episode/config.rb', line 102 def viewer @viewer || default(:viewer) end |
Class Method Details
.load(io, opts = {}) ⇒ Object
20 21 22 |
# File 'lib/episode/config.rb', line 20 def self.load(io, opts = {}) new JSON.parse(io.read, symbolize_names: true).merge(opts) end |
Instance Method Details
#dir ⇒ Object
78 79 80 |
# File 'lib/episode/config.rb', line 78 def dir get_local(:dir) { Dir.pwd } end |
#dir=(new_val) ⇒ Object
82 83 84 |
# File 'lib/episode/config.rb', line 82 def dir=(new_val) set_local :dir, new_val end |
#global ⇒ Object
74 75 76 |
# File 'lib/episode/config.rb', line 74 def global @global_cfg end |
#global? ⇒ Boolean
70 71 72 |
# File 'lib/episode/config.rb', line 70 def global? @global_cfg.nil? end |
#last ⇒ Object
86 87 88 |
# File 'lib/episode/config.rb', line 86 def last get_local :last end |
#last=(new_val) ⇒ Object
90 91 92 |
# File 'lib/episode/config.rb', line 90 def last=(new_val) set_local :last, new_val end |
#last_played_at ⇒ Object
94 95 96 |
# File 'lib/episode/config.rb', line 94 def last_played_at get_local :last_played_at end |
#last_played_at=(new_val) ⇒ Object
98 99 100 |
# File 'lib/episode/config.rb', line 98 def last_played_at=(new_val) set_local :last_played_at, new_val end |
#save(io) ⇒ Object
42 43 44 |
# File 'lib/episode/config.rb', line 42 def save(io) io.write JSON.pretty_generate(to_h(remove_defaults: true)) end |
#to_h(remove_defaults: false) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/episode/config.rb', line 46 def to_h(remove_defaults: false) if remove_defaults (@local || {}).merge({ viewer: @viewer, index_from_zero: @index_from_zero, 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_zero: index_from_zero, pointer: pointer, formats: formats }) end end |