2015-01-08 01:54:38 +08:00
|
|
|
#
|
2017-04-28 04:04:26 +08:00
|
|
|
# Copyright (C) 2011 - present Instructure, Inc.
|
2015-01-08 01:54:38 +08:00
|
|
|
#
|
|
|
|
# This file is part of Canvas.
|
|
|
|
#
|
|
|
|
# Canvas is free software: you can redistribute it and/or modify it under
|
|
|
|
# the terms of the GNU Affero General Public License as published by the Free
|
|
|
|
# Software Foundation, version 3 of the License.
|
|
|
|
#
|
|
|
|
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Affero General Public License along
|
|
|
|
# with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
# Filters added to this controller apply to all controllers in the application.
|
|
|
|
# Likewise, all the methods added will be available for all controllers.
|
|
|
|
|
|
|
|
module Lti
|
|
|
|
class VariableExpansion
|
|
|
|
|
2017-02-08 04:51:01 +08:00
|
|
|
attr_reader :name, :permission_groups, :default_name
|
2015-01-08 01:54:38 +08:00
|
|
|
|
2017-02-08 04:51:01 +08:00
|
|
|
def initialize(name, permission_groups, expansion_proc, *guards, default_name: nil)
|
2015-01-08 01:54:38 +08:00
|
|
|
@name = name
|
|
|
|
@permission_groups = permission_groups
|
2016-10-28 03:57:01 +08:00
|
|
|
@expansion_proc = expansion_proc
|
|
|
|
@guards = guards
|
|
|
|
@guards << -> { true } if @guards.empty?
|
2017-02-08 04:51:01 +08:00
|
|
|
@default_name = default_name
|
2015-01-08 01:54:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def expand(expander)
|
2018-02-17 09:44:44 +08:00
|
|
|
expand_for?(expander) ? expander.instance_exec(&@expansion_proc) : "$#{name}"
|
2015-01-08 01:54:38 +08:00
|
|
|
end
|
|
|
|
|
2016-10-28 03:57:01 +08:00
|
|
|
private
|
|
|
|
def expand_for?(expander)
|
2018-02-17 09:44:44 +08:00
|
|
|
@guards.map {|guard| expander.instance_exec(&guard) }.
|
|
|
|
inject { |memo, obj| memo && obj }
|
2016-10-28 03:57:01 +08:00
|
|
|
end
|
2015-01-08 01:54:38 +08:00
|
|
|
end
|
2016-10-28 03:57:01 +08:00
|
|
|
end
|