module Card::Setting

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

Constants

SETTING_OPTIONS

Attributes

codename[RW]

Let M = Card::Setting (module)

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

accessible in E

Public Class Methods

codenames() click to toggle source
# File lib/card/setting.rb, line 35
def codenames
  groups.values.flatten.compact.map(&:codename)
end
extended(host_class) click to toggle source
# 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) click to toggle source
# File lib/card/setting.rb, line 39
def preference? codename
  preferences.include? codename
end

Public Instance Methods

applies_to_cardtype(type_id, prototype=nil) click to toggle source
# File lib/card/setting.rb, line 73
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) click to toggle source
# File lib/card/setting.rb, line 78
def applies_to_prototype? prototype
  return true unless applies

  applies.call(prototype)
end
register_setting(opts) click to toggle source

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>, ...]
# File lib/card/setting.rb, line 62
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

Private Instance Methods

help_text_for_setting(help_text) click to toggle source
# File lib/card/setting.rb, line 111
def help_text_for_setting help_text
  right_set.raw_help_text = self.raw_help_text = help_text
end
permitted_type_ids(types) click to toggle source
# File lib/card/setting.rb, line 115
def permitted_type_ids types
  return unless types

  type_ids = Array.wrap(types).flatten.map do |cardtype|
    Card::Codename.id cardtype
  end
  ::Set.new(type_ids)
end
register_preference(codename, preference) click to toggle source
# File lib/card/setting.rb, line 94
def register_preference codename, preference
  @codename = codename || name.match(/::(\w+)$/)[1].underscore.to_sym

  Card::Setting.preferences << @codename if preference
end
restrict_setting_to_type(types) click to toggle source
# File lib/card/setting.rb, line 90
def restrict_setting_to_type types
  self.restricted_to_type = permitted_type_ids types
end
set_position(group, pos) click to toggle source
# File lib/card/setting.rb, line 100
def set_position group, pos
  grp = Card::Setting.groups[group]
  return (grp << self) unless pos

  if grp[pos - 1]
    grp.insert(pos - 1, self)
  else
    grp[pos - 1] = self
  end
end
standard_setting_opts(hash, *options) click to toggle source
# File lib/card/setting.rb, line 86
def standard_setting_opts hash, *options
  options.each { |opt| send "#{opt}=", hash[opt] }
end