Module: TinyDyno::Fields::ClassMethods

Defined in:
lib/tiny_dyno/fields.rb

Instance Method Summary collapse

Instance Method Details

#attribute_namesArray<String>

Returns an array of names for the attributes available on this object.

Provides the field names in an ORM-agnostic way. Rails v3.1+ uses this method to automatically wrap params in JSON requests.

Examples:

Get the field names

Model.attribute_names

Returns:

  • (Array<String>)

    The field names

Since:

  • 3.0.0



81
82
83
# File 'lib/tiny_dyno/fields.rb', line 81

def attribute_names
  fields.keys
end

#database_field_name(name) ⇒ String

Get the name of the provided field as it is stored in the database. Used in determining if the field is aliased or not.

Examples:

Get the database field name.

Model.database_field_name(:authorization)

Parameters:

  • name (String, Symbol)

    The name to get.

Returns:

  • (String)

    The name of the field as it’s stored in the db.

Since:

  • 3.0.7



96
97
98
99
# File 'lib/tiny_dyno/fields.rb', line 96

def database_field_name(name)
  return nil unless name
  normalized = name.to_s
end

#field(name, options = {}) ⇒ Field

Defines all the fields that are accessible on the Document For each field that is defined, a getter and setter will be added as an instance method to the Document.

Examples:

Define a field.

field :score, :type => Integer, :default => 0

Parameters:

  • name (Symbol)

    The name of the field.

  • options (Hash) (defaults to: {})

    The options to pass to the field.

Options Hash (options):

  • :type (Class)

    The type of the field.

  • :label (String)

    The label for the field.

  • :default (Object, Proc)

    The field’s default

Returns:

  • (Field)

    The generated field



116
117
118
119
120
# File 'lib/tiny_dyno/fields.rb', line 116

def field(name, options = {})
  named = name.to_s
  added = add_field(named, options)
  added
end