module Subroutine::Fields::ClassMethods
Public Instance Methods
field(field_name, options = {})
click to toggle source
# File lib/subroutine/fields.rb, line 39 def field(field_name, options = {}) config = ::Subroutine::Fields::Configuration.from(field_name, options) config.validate! self.field_configurations = field_configurations.merge(field_name.to_sym => config) ensure_field_accessors(config) config.groups.each do |group_name| ensure_group_accessors(group_name) end config end
Also aliased as: input
fields_from(*things)
click to toggle source
# File lib/subroutine/fields.rb, line 55 def fields_from(*things) options = things.extract_options! excepts = options.key?(:except) ? Array(options.delete(:except)) : nil onlys = options.key?(:only) ? Array(options.delete(:only)) : nil things.each do |thing| local_excepts = excepts.map { |field| thing.get_field_config(field)&.related_field_names }.flatten.compact.uniq if excepts local_onlys = onlys.map { |field| thing.get_field_config(field)&.related_field_names }.flatten.compact.uniq if onlys thing.field_configurations.each_pair do |field_name, config| next if local_excepts&.include?(field_name) next if local_onlys && !local_onlys.include?(field_name) config.required_modules.each do |mod| include mod unless included_modules.include?(mod) end field(field_name, config.merge(options)) end end end
Also aliased as: inputs_from
fields_in_group(group_name)
click to toggle source
# File lib/subroutine/fields.rb, line 78 def fields_in_group(group_name) field_configurations.each_with_object({}) do |(field_name, config), h| next unless config.in_group?(group_name) h[field_name] = config end end
get_field_config(field_name)
click to toggle source
# File lib/subroutine/fields.rb, line 86 def get_field_config(field_name) field_configurations[field_name.to_sym] end
method_missing(method_name, *args, &block)
click to toggle source
Calls superclass method
# File lib/subroutine/fields.rb, line 94 def method_missing(method_name, *args, &block) caster = ::Subroutine::TypeCaster.casters[method_name.to_sym] if caster field_name, options = args options ||= {} options[:type] = method_name.to_sym field(field_name, options) else super end end
respond_to_missing?(method_name, *args, &block)
click to toggle source
Calls superclass method
# File lib/subroutine/fields.rb, line 90 def respond_to_missing?(method_name, *args, &block) ::Subroutine::TypeCaster.casters.key?(method_name.to_sym) || super end
Protected Instance Methods
ensure_field_accessors(config)
click to toggle source
# File lib/subroutine/fields.rb, line 136 def ensure_field_accessors(config) if config.field_writer? class_eval <<-EV, __FILE__, __LINE__ + 1 try(:silence_redefinition_of_method, :#{config.field_name}=) def #{config.field_name}=(v) set_field(:#{config.field_name}, v) end EV end if config.field_reader? class_eval <<-EV, __FILE__, __LINE__ + 1 try(:silence_redefinition_of_method, :#{config.field_name}) def #{config.field_name} get_field(:#{config.field_name}) end EV end end
ensure_group_accessors(group_name)
click to toggle source
# File lib/subroutine/fields.rb, line 108 def ensure_group_accessors(group_name) group_name = group_name.to_sym return if field_groups.include?(group_name) self.field_groups |= [group_name] class_eval <<-EV, __FILE__, __LINE__ + 1 def #{group_name}_params param_groups[:#{group_name}] end def #{group_name}_default_params group_field_names = fields_in_group(:#{group_name}).keys all_default_params.slice(*group_field_names) end alias #{group_name}_defaults #{group_name}_default_params def #{group_name}_params_with_default_params #{group_name}_default_params.merge(param_groups[:#{group_name}]) end alias #{group_name}_params_with_defaults #{group_name}_params_with_default_params def without_#{group_name}_params all_params.except(*#{group_name}_params.keys) end EV end