module EfoNelfo::Properties::ClassMethods

Public Instance Methods

properties() click to toggle source

Returns all properties defined for the class

# File lib/efo_nelfo/properties.rb, line 76
def properties
  @_properties ||= {}
end
property(name, options={}) click to toggle source

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
# 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

Private Instance Methods

create_alias_for(name, alias_name, options) click to toggle source

Create an alias getter/setter for the property

# File lib/efo_nelfo/properties.rb, line 105
def create_alias_for(name, alias_name, options)
  define_method(alias_name) do
    send name
  end

  unless options[:read_only]
    define_method("#{alias_name}=") do |val|
      send "#{name}=", val
    end
  end
end
create_question_for(name) click to toggle source

Creates a name? accessor for the property Only applies to boolean types

# File lib/efo_nelfo/properties.rb, line 98
def create_question_for(name)
  define_method "#{name}?" do
    attributes[name].value == true
  end
end
create_reader_for(name, options) click to toggle source

Creates an attribute accessor for the property

# File lib/efo_nelfo/properties.rb, line 83
def create_reader_for(name, options)
  define_method name do
    attributes[name].value
  end
end
create_setter_for(name, options) click to toggle source

Creates an attribute setter for the property

# File lib/efo_nelfo/properties.rb, line 90
def create_setter_for(name, options)
  define_method "#{name}=" do |value|
    attributes[name].value = value
  end
end