Class: Swift::Attribute

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

Overview

– NOTE: Default method is defined in the extension.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Attribute.



7
8
9
10
11
12
13
14
# File 'lib/swift/attribute.rb', line 7

def initialize scheme, 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_scheme_methods(scheme)
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



5
6
7
# File 'lib/swift/attribute.rb', line 5

def field
  @field
end

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/swift/attribute.rb', line 5

def key
  @key
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/swift/attribute.rb', line 5

def name
  @name
end

#serialObject (readonly)

Returns the value of attribute serial.



5
6
7
# File 'lib/swift/attribute.rb', line 5

def serial
  @serial
end

Instance Method Details

#defaultObject



5
6
7
8
9
10
11
12
13
14
# File 'ext/attribute.cc', line 5

VALUE attribute_default(VALUE self) {
  VALUE value = rb_iv_get(self, "@default");

  if (NIL_P(value) || rb_special_const_p(value))
    return value;
  else if (rb_respond_to(value, fcall))
    return rb_funcall(value, fcall, 0);
  else
    return rb_obj_dup(value);
}

#define_scheme_methods(scheme) ⇒ Object



16
17
18
19
20
21
# File 'lib/swift/attribute.rb', line 16

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