Class: Friendly::Attribute

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

Instance Attribute Summary

Class Method Summary

Instance Method Summary

Constructor Details

- (Attribute) initialize(klass, name, type, options = {})

A new instance of Attribute



36
37
38
39
40
41
42
# File 'lib/friendly/attribute.rb', line 36

def initialize(klass, name, type, options = {})
  @klass         = klass
  @name          = name
  @type          = type
  @default_value = options[:default]
  build_accessors
end

Instance Attribute Details

- (Object) default_value (readonly)

Returns the value of attribute default_value



34
35
36
# File 'lib/friendly/attribute.rb', line 34

def default_value
  @default_value
end

- (Object) klass (readonly)

Returns the value of attribute klass



34
35
36
# File 'lib/friendly/attribute.rb', line 34

def klass
  @klass
end

- (Object) name (readonly)

Returns the value of attribute name



34
35
36
# File 'lib/friendly/attribute.rb', line 34

def name
  @name
end

- (Object) type (readonly)

Returns the value of attribute type



34
35
36
# File 'lib/friendly/attribute.rb', line 34

def type
  @type
end

Class Method Details

+ (Object) converters



22
23
24
# File 'lib/friendly/attribute.rb', line 22

def converters
  @converters ||= {}
end

+ (Boolean) custom_type?(klass)

Returns:



26
27
28
# File 'lib/friendly/attribute.rb', line 26

def custom_type?(klass)
  !sql_type(klass).nil?
end

+ (Object) deregister_type(type)



9
10
11
12
# File 'lib/friendly/attribute.rb', line 9

def deregister_type(type)
  sql_types.delete(type.name)
  converters.delete(type)
end

+ (Object) register_type(type, sql_type, &block)



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

def register_type(type, sql_type, &block)
  sql_types[type.name] = sql_type
  converters[type]     = block
end

+ (Object) sql_type(type)



14
15
16
# File 'lib/friendly/attribute.rb', line 14

def sql_type(type)
  sql_types[type.name]
end

+ (Object) sql_types



18
19
20
# File 'lib/friendly/attribute.rb', line 18

def sql_types
  @sql_types ||= {}
end

Instance Method Details

- (Object) assign_default_value(document)



63
64
65
# File 'lib/friendly/attribute.rb', line 63

def assign_default_value(document)
  document.send(:#{name}=", default)
end

- (Object) convert(value)



48
49
50
51
# File 'lib/friendly/attribute.rb', line 48

def convert(value)
  assert_converter_exists(value)
  converters[type].call(value)
end

- (Object) default



53
54
55
56
57
58
59
60
61
# File 'lib/friendly/attribute.rb', line 53

def default
  if !default_value.nil?
    default_value
  elsif type.respond_to?(:new)
    type.new
  else
    nil
  end
end

- (Object) typecast(value)



44
45
46
# File 'lib/friendly/attribute.rb', line 44

def typecast(value)
  !type || value.is_a?(type) ? value : convert(value)
end