Class: ScheduledResource::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/scheduled_resource/config.rb

Overview

Schedule configuration data itself is fairly brief and generally can be kept in the session. An instance of this class (though reflecting the same information) isn’t suitable for session storage. It is more useful for processing a request and also mediates (later) changes to the configuration.

Constant Summary collapse

CONFIG_FILE =

For now we have a single, static configuration…

"config/resource_schedule.yml"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(yml) ⇒ Config

(hi-lock-face-buffer “yml\” ‘hi-yellow) A Hash, as from a yml file.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/scheduled_resource/config.rb', line 48

def initialize( yml )                   # fka config_from_yaml
  @yml = yml
  @resource_list                 = []   # fka :all_resources
  @block_class_for_resource_kind = {}
  
  yml['ResourceKinds'].each do |key, val| # {"Channel" => "Program"...}
    @block_class_for_resource_kind[key] = val # class name (string)
  end

  do_resource_kind_lists( yml['Resources'] || [] )


  # Eval is FAR TOO DANGEROUS for user input.  Rather, parse a string
  # into (number).(unit) (maybe a couple of other patterns) to sanitize.
  vt = yml['visibleTime']
  @visible_time   = vt ? (eval vt) : 3.hours

  t0 = yml['timeRangeMin']
  @time_range_min = t0 ? (eval t0) : (Time.now - 1.week)

  tn = yml['timeRangeMax']
  @time_range_max = tn ? (eval tn) : (Time.now + 1.week)


  rsrcs_by_kind.each do |kind, rsrcs|   # fka config_from_yaml2
    klass = kind.constantize
    rsrcs.each {|rsrc| klass.decorate_resource rsrc }
  end
end

Instance Attribute Details

#resource_listObject (readonly)

Returns the value of attribute resource_list.



18
19
20
# File 'lib/scheduled_resource/config.rb', line 18

def resource_list
  @resource_list
end

#time_range_maxObject (readonly)

Returns the value of attribute time_range_max.



17
18
19
# File 'lib/scheduled_resource/config.rb', line 17

def time_range_max
  @time_range_max
end

#time_range_minObject (readonly)

Returns the value of attribute time_range_min.



17
18
19
# File 'lib/scheduled_resource/config.rb', line 17

def time_range_min
  @time_range_min
end

#visible_timeObject (readonly)

Returns the value of attribute visible_time.



17
18
19
# File 'lib/scheduled_resource/config.rb', line 17

def visible_time
  @visible_time
end

Class Method Details

.ensure(session) ⇒ Object

– Restore configuration from session or base as needed.

When we depend on data in the configuration to satisfy a query we are not being RESTful. On the other hand we are not maintaining changeable state here – it’s just a cache. If there were changeable state it would likely be kept, eg, in a per-user table in the database. ++



30
31
32
33
34
35
# File 'lib/scheduled_resource/config.rb', line 30

def self.ensure( session )              # fka ensure_config
  yml_string = session[:schedule_config]
  return new( YAML.load(yml_string) ) if yml_string

  from_base( session )
end

.from_base(session) ⇒ Object

ToDo: Generalize this so configuration can be loaded on per-user.



39
40
41
42
43
# File 'lib/scheduled_resource/config.rb', line 39

def self.from_base( session )           # fka config_from_yaml1
  yml = YAML.load_file CONFIG_FILE
  session[:schedule_config] = yml.to_yaml
  new( yml )
end

Instance Method Details

#add_resources(rsrcs) ⇒ Object

Hmm… DOM Row uniqueness vs resource (tag) uniqueness.



94
95
96
97
98
# File 'lib/scheduled_resource/config.rb', line 94

def add_resources(rsrcs)
  rsrcs.each do |rsrc| 
    @resource_list << rsrc unless @resource_list.include? rsrc 
  end
end

#block_class_for_resource_name(name) ⇒ Object

Parameters

  • name - The class name (string) of a schedule resource.

Returns

  • Class - The class representing the use of that resource for an interval of time.



109
110
111
# File 'lib/scheduled_resource/config.rb', line 109

def block_class_for_resource_name( name )
  @block_class_for_resource_kind[name].constantize
end

#do_resource_kind_lists(rkls) ⇒ Object

Resource Kind Lists, Each line (in yml format) is a ResourceKind followed by rids such as “Channel 702 703 …”



82
83
84
85
86
87
88
89
90
# File 'lib/scheduled_resource/config.rb', line 82

def do_resource_kind_lists( rkls )
  rkls.each do |rkl|
    rkl   = rkl.split(/[, ]+/)   # ["Channel",  "702", "703" ...]
    rk    = rkl.shift
    rsrcs = rkl.map{|rid| ScheduledResource.make_resource_of_kind(rk, rid)}

    add_resources rsrcs
  end
end

#rsrcs_by_kindObject



100
101
102
# File 'lib/scheduled_resource/config.rb', line 100

def rsrcs_by_kind
  @resource_list.group_by{ |r| r.kind }
end