Class: Browsable::Policy

Inherits:
Object
  • Object
show all
Defined in:
lib/browsable/policy.rb

Overview

The effective ‘allow_browser` policy for a specific controller#action.

Policy is what runtime mode produces per response — distinct from PolicyScanner::Policy, which is a discovery record for one callsite. A Policy carries enough information to (a) build a Target against which the endpoint’s assets are audited, and (b) explain why this is the policy in play, when the TestReport renders findings.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(versions:, note: nil, scope: nil, only: nil, except: nil, source: :controller) ⇒ Policy

Returns a new instance of Policy.

Parameters:

  • versions (Hash, Symbol, nil)

    the resolved allow_browser argument

  • note (String, nil) (defaults to: nil)

    caveat when the versions could not be resolved

  • scope (String, nil) (defaults to: nil)

    the owning class name (nil for the fallback)

  • only (Array<String>, nil) (defaults to: nil)

    action filter from the call

  • except (Array<String>, nil) (defaults to: nil)

    action filter from the call

  • source (Symbol) (defaults to: :controller)

    :controller, :ancestor, or :default



20
21
22
23
24
25
26
27
# File 'lib/browsable/policy.rb', line 20

def initialize(versions:, note: nil, scope: nil, only: nil, except: nil, source: :controller)
  @versions = versions
  @note = note
  @scope = scope
  @only = only
  @except = except
  @source = source
end

Instance Attribute Details

#exceptObject (readonly)

Returns the value of attribute except.



12
13
14
# File 'lib/browsable/policy.rb', line 12

def except
  @except
end

#noteObject (readonly)

Returns the value of attribute note.



12
13
14
# File 'lib/browsable/policy.rb', line 12

def note
  @note
end

#onlyObject (readonly)

Returns the value of attribute only.



12
13
14
# File 'lib/browsable/policy.rb', line 12

def only
  @only
end

#scopeObject (readonly)

Returns the value of attribute scope.



12
13
14
# File 'lib/browsable/policy.rb', line 12

def scope
  @scope
end

#sourceObject (readonly)

Returns the value of attribute source.



12
13
14
# File 'lib/browsable/policy.rb', line 12

def source
  @source
end

#versionsObject (readonly)

Returns the value of attribute versions.



12
13
14
# File 'lib/browsable/policy.rb', line 12

def versions
  @versions
end

Instance Method Details

#as_jsonObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/browsable/policy.rb', line 52

def as_json
  {
    versions: versions,
    note: note,
    scope: scope,
    only: only,
    except: except,
    source: source.to_s
  }
end

#default?Boolean

True when this Policy is the application-wide fallback rather than a specific allow_browser call. Distinguished so reports can say “no controller policy — audited against the project default”.

Returns:

  • (Boolean)


50
# File 'lib/browsable/policy.rb', line 50

def default? = source == :default

#labelObject

A short human label, e.g. “:modern” or “{ chrome: 120, … }” — used by the TestReport when it wants to print which policy applied.



39
40
41
42
43
44
45
# File 'lib/browsable/policy.rb', line 39

def label
  case versions
  when Symbol then ":#{versions}"
  when Hash   then "{ #{versions.map { |k, v| "#{k}: #{v}" }.join(', ')} }"
  else            "(unresolved)"
  end
end

#targetObject

The Browsable::Target this policy implies. Falls back to the browserslist ‘defaults` query when no allow_browser versions could be resolved.



31
32
33
34
35
# File 'lib/browsable/policy.rb', line 31

def target
  return Target.from_rails_policy(versions) if versions

  Target.new("defaults")
end