Module: Typecaster::ClassMethods

Defined in:
lib/typecaster.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/typecaster.rb', line 8

def attribute(name, options={})
  attribute_name = name.to_sym

  raw_attributes[attribute_name] = options
  attributes[attribute_name] = nil

  define_method(name) do
    if instance_variable_defined?("@#{name}")
      instance_variable_get("@#{name}")
    elsif raw_attributes[attribute_name].has_key?(:default)
      define_instance_variable(name, raw_attributes[attribute_name][:default])
    else
      define_instance_variable(name, nil)
    end
  end

  define_method("#{name}=") do |val|
    define_instance_variable(name, val)
  end
end

#attributesObject



29
30
31
# File 'lib/typecaster.rb', line 29

def attributes
  @attributes ||= Hash.new
end

#raw_attributesObject



33
34
35
# File 'lib/typecaster.rb', line 33

def raw_attributes
  @raw_attributes ||= Hash.new
end