Module: Card::Setting

Defined in:
lib/card/setting.rb

Overview

Used to extend setting modules like Card::Set::Self::Create in the settings mod

Constant Summary collapse

SETTING_OPTIONS =
%i[
  restricted_to_type
  rule_type_editable
  short_help_text
  raw_help_text
  applies
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#codenameObject

Let M = Card::Setting (module)

E = Card::Set::Self::Create (module extended with M)
O = Card['*create']         (object)

accessible in E



11
12
13
# File 'lib/card/setting.rb', line 11

def codename
  @codename
end

Class Method Details

.codenamesObject



35
36
37
# File 'lib/card/setting.rb', line 35

def codenames
  groups.values.flatten.compact.map(&:codename)
end

.extended(host_class) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/card/setting.rb', line 24

def extended host_class
  # accessible in E and O
  host_class.mattr_accessor(*SETTING_OPTIONS)
  setting_class_name = host_class.to_s.split("::").last
  host_class.ensure_set { "Card::Set::Right::#{setting_class_name}" }

  host_class.mattr_accessor :right_set
  host_class.right_set = Card::Set::Right.const_get(setting_class_name)
  host_class.right_set.mattr_accessor :raw_help_text
end

.preference?(codename) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/card/setting.rb', line 39

def preference? codename
  preferences.include? codename
end

Instance Method Details

#applies_to_cardtype(type_id, prototype = nil) ⇒ Object



76
77
78
79
# File 'lib/card/setting.rb', line 76

def applies_to_cardtype type_id, prototype=nil
  (!restricted_to_type || restricted_to_type.include?(type_id)) &&
    (!prototype || applies_to_prototype?(prototype))
end

#applies_to_prototype?(prototype) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
# File 'lib/card/setting.rb', line 81

def applies_to_prototype? prototype
  return true unless applies
  applies.call(prototype)
end

#register_setting(opts) ⇒ Object

usage: setting_opts group: :permission | :event | …

position:     <Fixnum> (starting at 1, default: add to end)
rule_type_editable: true | false (default: false)
restricted_to_type: <cardtype> | [ <cardtype>, ...]


65
66
67
68
69
70
71
72
73
74
# File 'lib/card/setting.rb', line 65

def register_setting opts
  group = opts[:group] || :other
  Card::Setting.groups[group] ||= []
  set_position group, opts[:position]

  register_preference opts[:codename], opts[:preference]
  standard_setting_opts opts, :rule_type_editable, :short_help_text, :applies
  restrict_setting_to_type opts[:restricted_to_type]
  help_text_for_setting opts[:help_text]
end