class Subroutine::Fields::Configuration

Constants

INHERITABLE_OPTIONS
NO_GROUPS
PROTECTED_GROUP_IDENTIFIERS

Attributes

field_name[R]

Public Class Methods

from(field_name, options) click to toggle source
# File lib/subroutine/fields/configuration.rb, line 13
def self.from(field_name, options)
  case options
  when Subroutine::Fields::Configuration
    options.class.new(field_name, options)
  else
    new(field_name, options)
  end
end
new(field_name, options) click to toggle source
Calls superclass method
# File lib/subroutine/fields/configuration.rb, line 24
def initialize(field_name, options)
  @field_name = field_name.to_sym
  config = sanitize_options(options)
  super(config)
  validate!
end

Public Instance Methods

behavior() click to toggle source
# File lib/subroutine/fields/configuration.rb, line 45
def behavior
  nil
end
field_reader?() click to toggle source
# File lib/subroutine/fields/configuration.rb, line 77
def field_reader?
  config[:field_reader] != false
end
field_writer?() click to toggle source
# File lib/subroutine/fields/configuration.rb, line 73
def field_writer?
  config[:field_writer] != false
end
get_default() click to toggle source
# File lib/subroutine/fields/configuration.rb, line 53
def get_default
  value = config[:default]
  if value.respond_to?(:call)
    value = value.call
  elsif value.try(:duplicable?) # from active_support
    # Some classes of default values need to be duplicated, or the instance field value will end up referencing
    # the class global default value, and potentially modify it.
    value = value.deep_dup # from active_support
  end
  value
end
groups() click to toggle source
# File lib/subroutine/fields/configuration.rb, line 81
def groups
  config[:groups] || NO_GROUPS
end
has_default?() click to toggle source
# File lib/subroutine/fields/configuration.rb, line 49
def has_default?
  config.key?(:default)
end
in_group?(group_name) click to toggle source
# File lib/subroutine/fields/configuration.rb, line 85
def in_group?(group_name)
  groups.include?(group_name.to_sym)
end
inheritable_options() click to toggle source
# File lib/subroutine/fields/configuration.rb, line 65
def inheritable_options
  config.slice(*INHERITABLE_OPTIONS)
end
inspect() click to toggle source
# File lib/subroutine/fields/configuration.rb, line 106
def inspect
  "#<#{self.class}:#{object_id} name=#{field_name} config=#{config.inspect}>"
end
mass_assignable?() click to toggle source
# File lib/subroutine/fields/configuration.rb, line 69
def mass_assignable?
  config[:mass_assignable] != false
end
merge(options = {}) click to toggle source
# File lib/subroutine/fields/configuration.rb, line 33
def merge(options = {})
  self.class.new(field_name, config.merge(options))
end
required_modules() click to toggle source
# File lib/subroutine/fields/configuration.rb, line 37
def required_modules
  []
end
sanitize_options(options) click to toggle source
# File lib/subroutine/fields/configuration.rb, line 97
def sanitize_options(options)
  opts = (options || {}).to_h.dup
  groups = opts[:group] || opts[:groups]
  groups = nil if groups == false
  opts[:groups] = Array(groups).map(&:to_sym).presence
  opts.delete(:group)
  opts
end
validate!() click to toggle source
# File lib/subroutine/fields/configuration.rb, line 89
def validate!
  PROTECTED_GROUP_IDENTIFIERS.each do |group_name|
    next unless in_group?(group_name)

    raise ArgumentError, "Cannot assign a field to protected group `#{group}`. Protected groups are: #{PROTECTED_GROUP_IDENTIFIERS.join(", ")}"
  end
end