Class: Ricordami::Attribute

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Attribute.



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

def initialize(name, options = {})
  options.assert_valid_keys(:default, :read_only, :initial, :indexed, :type)
  if options[:indexed] && ![:value, :unique].include?(options[:indexed])
    raise InvalidIndexDefinition.new(options[:indexed].to_s)
  end
  options[:type] ||= :string
  @options = options
  @name = name.to_sym
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#converterObject



44
45
46
47
48
49
50
# File 'lib/ricordami/attribute.rb', line 44

def converter
  case @options[:type]
  when :string  then :to_s
  when :integer then :to_i
  when :float   then :to_f
  end
end

#indexedObject



32
33
34
# File 'lib/ricordami/attribute.rb', line 32

def indexed
  @options[:indexed]
end

#indexed?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/ricordami/attribute.rb', line 36

def indexed?
  !!@options[:indexed]
end

#read_only?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/ricordami/attribute.rb', line 28

def read_only?
  !!@options[:read_only]
end

#typeObject



40
41
42
# File 'lib/ricordami/attribute.rb', line 40

def type
  @options[:type]
end