Class: TransientModel::Fields::BaseField

Inherits:
Object
  • Object
show all
Defined in:
lib/transient_model/fields/base_field.rb

Direct Known Subclasses

StandardField

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ BaseField

Returns a new instance of BaseField.

Parameters:

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

Since:

  • 0.1.0



15
16
17
18
19
20
21
22
# File 'lib/transient_model/fields/base_field.rb', line 15

def initialize(name, options = {})
  defaults = {
    type: String
  }

  @name = name
  @options = defaults.merge options
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/transient_model/fields/base_field.rb', line 6

def name
  @name
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/transient_model/fields/base_field.rb', line 6

def options
  @options
end

Instance Method Details

#default_valueany

Return the default value that should be used for the field.

Returns:

  • (any)

    returns the default value set for the field.

Since:

  • 0.1.0



29
30
31
# File 'lib/transient_model/fields/base_field.rb', line 29

def default_value
  @options.fetch(:default, nil)
end

#getter_method_nameObject



52
53
54
# File 'lib/transient_model/fields/base_field.rb', line 52

def getter_method_name
  "#{ @name }".to_sym
end

#instance_variable_nameSymbol

Return the symbol representation of the instance variable used by the defining model object.

Returns:

  • (Symbol)

    the instance variable name of the field

Since:

  • 0.1.0



48
49
50
# File 'lib/transient_model/fields/base_field.rb', line 48

def instance_variable_name
  "@#{ @name }".to_sym
end

#setter_method_nameObject



56
57
58
# File 'lib/transient_model/fields/base_field.rb', line 56

def setter_method_name
  "#{ @name }=".to_sym
end

#typeany

Return the data type for this field.

Returns:

  • (any)

Since:

  • 0.1.0



38
39
40
# File 'lib/transient_model/fields/base_field.rb', line 38

def type
  @type ||= options.fetch(:type, String)
end