Class: Swift::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/swift/attribute.rb

Overview

An attribute (column) definition. – NOTE: Default method is defined in the extension.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, name, options = {}) ⇒ Attribute

Returns a new instance of Attribute.

Examples:

user = Class.new(Swift::Record)
Swift::Attribute.new(user, :name, Swift::Type::String)

Parameters:

  • record (Swift::Record)
  • name (Symbol)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :default (Object, Proc)
  • :field (Symbol)
  • :key (TrueClass, FalseClass)
  • :serial (TrueClass, FalseClass)

See Also:



23
24
25
26
27
28
29
30
# File 'lib/swift/attribute.rb', line 23

def initialize record, name, options = {}
  @name    = name
  @default = options.fetch(:default, nil)
  @field   = options.fetch(:field,   name)
  @key     = options.fetch(:key,     false)
  @serial  = options.fetch(:serial,  false)
  define_record_methods(record)
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



7
8
9
# File 'lib/swift/attribute.rb', line 7

def field
  @field
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/swift/attribute.rb', line 7

def key
  @key
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/swift/attribute.rb', line 7

def name
  @name
end

#serialObject (readonly)

Returns the value of attribute serial.



7
8
9
# File 'lib/swift/attribute.rb', line 7

def serial
  @serial
end

Instance Method Details

#defaultObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/swift/attribute.rb', line 32

def default
  case @default
    when Numeric, Symbol, true, false, nil
      @default
    when Proc
      @default.call
    else
      @default.dup
  end
end

#define_record_methods(record) ⇒ Object

Evals attribute accessors for this attribute into the record.



51
52
53
54
55
56
# File 'lib/swift/attribute.rb', line 51

def define_record_methods record
  record.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{name};        tuple.fetch(:#{field}, nil)   end
    def #{name}= value; tuple.store(:#{field}, value) end
  RUBY
end

#to_sString

The attributes field.

Returns:

  • (String)


46
47
48
# File 'lib/swift/attribute.rb', line 46

def to_s
  field.to_s
end