Module: ActiveInteraction

Defined in:
lib/active_interaction.rb,
lib/active_interaction/base.rb,
lib/active_interaction/input.rb,
lib/active_interaction/errors.rb,
lib/active_interaction/filter.rb,
lib/active_interaction/inputs.rb,
lib/active_interaction/version.rb,
lib/active_interaction/exceptions.rb,
lib/active_interaction/hash_input.rb,
lib/active_interaction/array_input.rb,
lib/active_interaction/filter/error.rb,
lib/active_interaction/filter/column.rb,
lib/active_interaction/grouped_input.rb,
lib/active_interaction/concerns/hashable.rb,
lib/active_interaction/concerns/missable.rb,
lib/active_interaction/concerns/runnable.rb,
lib/active_interaction/modules/validation.rb,
lib/active_interaction/filters/date_filter.rb,
lib/active_interaction/filters/file_filter.rb,
lib/active_interaction/filters/hash_filter.rb,
lib/active_interaction/filters/time_filter.rb,
lib/active_interaction/filters/array_filter.rb,
lib/active_interaction/filters/float_filter.rb,
lib/active_interaction/filters/object_filter.rb,
lib/active_interaction/filters/record_filter.rb,
lib/active_interaction/filters/string_filter.rb,
lib/active_interaction/filters/symbol_filter.rb,
lib/active_interaction/filters/boolean_filter.rb,
lib/active_interaction/filters/decimal_filter.rb,
lib/active_interaction/filters/integer_filter.rb,
lib/active_interaction/filters/date_time_filter.rb,
lib/active_interaction/filters/interface_filter.rb,
lib/active_interaction/concerns/active_modelable.rb,
lib/active_interaction/concerns/active_recordable.rb,
lib/active_interaction/filters/abstract_numeric_filter.rb,
lib/active_interaction/filters/abstract_date_time_filter.rb

Overview

Manage application specific business logic.

Author:

Defined Under Namespace

Classes: ArrayInput, Base, Errors, Filter, HashInput, Input, Inputs, InvalidInteractionError

Constant Summary collapse

VERSION =

The version number.

Returns:

  • (Gem::Version)
Gem::Version.new('5.3.0')
Error =

Top-level error class. All other errors subclass this.

Class.new(StandardError)
InvalidNameError =

Raised if a constant name is invalid.

Class.new(Error)
InvalidConverterError =

Raised if a converter is invalid.

Class.new(Error)
InvalidDefaultError =

Raised if a default value is invalid.

Class.new(Error)
InvalidFilterError =

Raised if a filter has an invalid definition.

Class.new(Error)
MissingFilterError =

Raised if a filter cannot be found.

Class.new(Error)
NoDefaultError =

Raised if there is no default value.

Class.new(Error)

Class Method Summary collapse

Class Method Details

.array(*attributes, options = {}, &block) ⇒ Object

Creates accessors for the attributes and ensures that values passed to the attributes are Arrays.

Examples:

array :ids
array :ids do
  integer
end
array :ids do
  integer default: nil
end

Parameters:

  • *attributes (Array<Symbol>)

    Attributes to create.

  • options (Hash{Symbol => Object}) (defaults to: {})
  • block (Proc)

    filter method to apply to each element

Options Hash (options):

  • :default (Object)

    Fallback value if nil is given.

  • :desc (String)

    Human-readable description of this input.

  • :index_errors (Boolean) — default: ActiveRecord.index_nested_attribute_errors

    returns errors with an index



# File 'lib/active_interaction/filters/array_filter.rb', line 5

.boolean(*attributes, options = {}) ⇒ Object

Creates accessors for the attributes and ensures that values passed to the attributes are Booleans. The strings "1", "true", and "on" (case-insensitive) are converted to true while the strings "0", "false", and "off" are converted to false. Blank strings are treated as a nil value.

Examples:

boolean :subscribed

Parameters:

  • *attributes (Array<Symbol>)

    Attributes to create.

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :default (Object)

    Fallback value if nil is given.

  • :desc (String)

    Human-readable description of this input.



# File 'lib/active_interaction/filters/boolean_filter.rb', line 5

.date(*attributes, options = {}) ⇒ Object

Creates accessors for the attributes and ensures that values passed to the attributes are Dates. String values are processed using parse unless the format option is given, in which case they will be processed with strptime. Blank strings are treated as a nil value.

Examples:

date :birthday
date :birthday, format: '%Y-%m-%d'

Parameters:

  • *attributes (Array<Symbol>)

    Attributes to create.

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :default (Object)

    Fallback value if nil is given.

  • :desc (String)

    Human-readable description of this input.

  • :format (String)

    parse strings using this format string



# File 'lib/active_interaction/filters/date_filter.rb', line 5

.date_time(*attributes, options = {}) ⇒ Object

Creates accessors for the attributes and ensures that values passed to the attributes are DateTimes. String values are processed using parse unless the format option is given, in which case they will be processed with strptime. Blank strings are treated as a nil value.

Examples:

date_time :start_date
date_time :start_date, format: '%Y-%m-%dT%H:%M:%SZ'

Parameters:

  • *attributes (Array<Symbol>)

    Attributes to create.

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :default (Object)

    Fallback value if nil is given.

  • :desc (String)

    Human-readable description of this input.

  • :format (String)

    parse strings using this format string



# File 'lib/active_interaction/filters/date_time_filter.rb', line 5

.decimal(*attributes, options = {}) ⇒ Object

Creates accessors for the attributes and ensures that values passed to the attributes are BigDecimals. Numerics and String values are converted into BigDecimals. Blank strings are treated as a nil value.

Examples:

decimal :amount, digits: 4

Parameters:

  • *attributes (Array<Symbol>)

    Attributes to create.

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :default (Object)

    Fallback value if nil is given.

  • :desc (String)

    Human-readable description of this input.



# File 'lib/active_interaction/filters/decimal_filter.rb', line 7

.file(*attributes, options = {}) ⇒ Object

Creates accessors for the attributes and ensures that values passed to the attributes respond to the rewind method. This is useful when passing in Rails params that include a file upload or another generic IO object.

Examples:

file :image

Parameters:

  • *attributes (Array<Symbol>)

    Attributes to create.

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :default (Object)

    Fallback value if nil is given.

  • :desc (String)

    Human-readable description of this input.



# File 'lib/active_interaction/filters/file_filter.rb', line 5

.float(*attributes, options = {}) ⇒ Object

Creates accessors for the attributes and ensures that values passed to the attributes are Floats. Integer and String values are converted into Floats. Blank strings are treated as a nil value.

Examples:

float :amount

Parameters:

  • *attributes (Array<Symbol>)

    Attributes to create.

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :default (Object)

    Fallback value if nil is given.

  • :desc (String)

    Human-readable description of this input.



# File 'lib/active_interaction/filters/float_filter.rb', line 5

.hash(*attributes, options = {}, &block) ⇒ Object

Creates accessors for the attributes and ensures that values passed to the attributes are Hashes.

Examples:

hash :order
hash :order do
  object :item
  integer :quantity, default: 1
end

Parameters:

  • *attributes (Array<Symbol>)

    Attributes to create.

  • options (Hash{Symbol => Object}) (defaults to: {})
  • block (Proc)

    filter methods to apply for select keys

Options Hash (options):

  • :default (Object)

    Fallback value if nil is given.

  • :desc (String)

    Human-readable description of this input.

  • :strip (Boolean) — default: true

    remove unknown keys



# File 'lib/active_interaction/filters/hash_filter.rb', line 5

.integer(*attributes, options = {}) ⇒ Object

Creates accessors for the attributes and ensures that values passed to the attributes are Integers. String values are converted into Integers. Blank strings are treated as a nil value.

Examples:

integer :quantity

Parameters:

  • *attributes (Array<Symbol>)

    Attributes to create.

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :default (Object)

    Fallback value if nil is given.

  • :desc (String)

    Human-readable description of this input.

  • :base (Integer) — default: 10

    The base used to convert strings into integers. When set to 0 it will honor radix indicators (i.e. 0, 0b, and 0x).



# File 'lib/active_interaction/filters/integer_filter.rb', line 5

.interface(*attributes, options = {}) ⇒ Object

Creates accessors for the attributes and ensures that values passed to the attributes implement an interface. An interface can be based on a set of methods or the existance of a class or module in the ancestors of the passed value.

Examples:

interface :concern
interface :person,
  from: Manageable
interface :serializer,
  methods: %i[dump load]

Parameters:

  • *attributes (Array<Symbol>)

    Attributes to create.

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :default (Object)

    Fallback value if nil is given.

  • :desc (String)

    Human-readable description of this input.

  • :from (Constant, String, Symbol) — default: use the attribute name

    The class or module representing the interface to check for.

  • :methods (Array<String, Symbol>) — default: []

    the methods that objects conforming to this interface should respond to



# File 'lib/active_interaction/filters/interface_filter.rb', line 5

.object(*attributes, options = {}) ⇒ Object

Creates accessors for the attributes and ensures that values passed to the attributes are the correct class.

Examples:

object :account
object :account, class: User

Parameters:

  • *attributes (Array<Symbol>)

    Attributes to create.

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :default (Object)

    Fallback value if nil is given.

  • :desc (String)

    Human-readable description of this input.

  • :class (Class, String, Symbol) — default: use the attribute name

    Class name used to ensure the value.

  • :converter (Proc, Symbol)

    A symbol specifying the name of a class method of :class or a Proc that is called when a new value is assigned to the value object. The converter is passed the single value that is used in the assignment and is only called if the new value is not an instance of :class. The class method or proc are passed the value. Any error thrown inside the converter is trapped and the value provided is treated as invalid. Any returned value that is not the correct class will also be treated as invalid.



# File 'lib/active_interaction/filters/object_filter.rb', line 5

.record(*attributes, options = {}) ⇒ Object

Creates accessors for the attributes and ensures that values passed to the attributes are the correct class. Blank strings passed in will be treated as nil and the finder will not be called.



# File 'lib/active_interaction/filters/record_filter.rb', line 5

.string(*attributes, options = {}) ⇒ Object

Creates accessors for the attributes and ensures that values passed to the attributes are Strings.

Examples:

string :first_name
string :first_name, strip: false

Parameters:

  • *attributes (Array<Symbol>)

    Attributes to create.

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :default (Object)

    Fallback value if nil is given.

  • :desc (String)

    Human-readable description of this input.

  • :strip (Boolean) — default: true

    strip leading and trailing whitespace



# File 'lib/active_interaction/filters/string_filter.rb', line 5

.symbol(*attributes, options = {}) ⇒ Object

Creates accessors for the attributes and ensures that values passed to the attributes are Symbols. Strings will be converted to Symbols.

Examples:

symbol :condiment

Parameters:

  • *attributes (Array<Symbol>)

    Attributes to create.

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :default (Object)

    Fallback value if nil is given.

  • :desc (String)

    Human-readable description of this input.



# File 'lib/active_interaction/filters/symbol_filter.rb', line 5

.time(*attributes, options = {}) ⇒ Object

Creates accessors for the attributes and ensures that values passed to the attributes are Times. Numeric values are processed using at. Strings are processed using parse unless the format option is given, in which case they will be processed with strptime. Blank strings are treated as a nil value. If Time.zone is available it will be used so that the values are time zone aware.

Examples:

time :start_date
time :start_date, format: '%Y-%m-%dT%H:%M:%S%Z'

Parameters:

  • *attributes (Array<Symbol>)

    Attributes to create.

  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :default (Object)

    Fallback value if nil is given.

  • :desc (String)

    Human-readable description of this input.

  • :format (String)

    parse strings using this format string



# File 'lib/active_interaction/filters/time_filter.rb', line 5