Class: Tramp::Attribute

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

Constant Summary collapse

FORMATS =
{}
CONVERTERS =

lifted from the implementation of Time.xmlschema

{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, owner_class, options) ⇒ Attribute

Returns a new instance of Attribute.


48
49
50
51
52
53
54
55
# File 'lib/tramp/attribute.rb', line 48

def initialize(name, owner_class, options)
  @name = name.to_s
  @owner_class = owner_class
  @options = options

  # append_validations!
  define_methods!
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name


47
48
49
# File 'lib/tramp/attribute.rb', line 47

def name
  @name
end

Instance Method Details

#append_validations!Object


86
87
88
89
90
# File 'lib/tramp/attribute.rb', line 86

def append_validations!
  if f = FORMATS[expected_type]
    @owner_class.validates_format_of @name, :with => f, :unless => lambda {|obj| obj.send(name).is_a? expected_type }, :allow_nil => @options[:allow_nil]
  end
end

#check_value!(value) ⇒ Object

I think this should live somewhere in Amo


58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/tramp/attribute.rb', line 58

def check_value!(value)
  # Allow nil and Strings to fall back on the validations for typecasting
  # Everything else gets checked with is_a?
  if value.nil?
    nil
  elsif value.is_a?(String)
    value
  elsif value.is_a?(expected_type)
    value
  else
    raise TypeError, "Expected #{expected_type.inspect} but got #{value.inspect}"
  end
end

#define_methods!Object


92
93
94
# File 'lib/tramp/attribute.rb', line 92

def define_methods!
  @owner_class.define_attribute_methods(true)
end

#expected_typeObject


72
73
74
# File 'lib/tramp/attribute.rb', line 72

def expected_type
  @options[:type] || String
end

#primary_key?Boolean

Returns:

  • (Boolean)

96
97
98
# File 'lib/tramp/attribute.rb', line 96

def primary_key?
  @options[:primary_key]
end

#type_cast(value) ⇒ Object


76
77
78
79
80
81
82
83
84
# File 'lib/tramp/attribute.rb', line 76

def type_cast(value)
  if value.is_a?(expected_type)
    value
  elsif (converter = CONVERTERS[expected_type]) && (value =~ FORMATS[expected_type])
    converter.call(value)
  else
    value
  end
end