Module: EfoNelfo::Properties::ClassMethods

Defined in:
lib/efo_nelfo/properties.rb

Instance Method Summary collapse

Instance Method Details

#propertiesObject

Returns all properties defined for the class



76
77
78
# File 'lib/efo_nelfo/properties.rb', line 76

def properties
  @_properties ||= {}
end

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

Creates an attribute with given name.

Options

- type      String, Integer etc. Default is String
- required  whether attribute is required. Default is false
- limit     Length the attribute can be. Default is nil
- alias     Norwegian alias name for the attribute


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/efo_nelfo/properties.rb', line 51

def property(name, options={})
  options = {
    type: :string,
    required: false,
  }.update options

  name       = name.to_sym
  alias_name = options.delete(:alias)

  # ensure all options are valid
  EfoNelfo::Property.validate_options! options

  # ensure property is unique
  raise EfoNelfo::DuplicateProperty if properties.has_key?(name)

  # setup getters and setters
  create_reader_for(name, options)
  create_setter_for(name, options) unless options[:read_only]
  create_question_for(name)        if options[:type] == :boolean
  create_alias_for(name, alias_name, options)  if alias_name

  properties[name] = options
end