Sha256: 63390adc1ced61049e3677b89facbfc769ecb06c30246ae46ab0bec6b615c554

Contents?: true

Size: 1.29 KB

Versions: 41

Compression:

Stored size: 1.29 KB

Contents

module Tracksperanto::Parameters
  
  def self.included(into)
    into.extend(self)
    super
  end
  
  class Parameter
    include Tracksperanto::BlockInit
    
    # The name of the paramenter and the related object attribute
    attr_accessor :name
    
    # Whether the attribute is required
    attr_accessor :required
    
    # Default value of the attribute
    attr_accessor :default
    
    # The cast for the attribute (like :int or :float)
    attr_accessor :cast
    
    # Attribute description for the UI
    attr_accessor :desc
    
    def apply_to(to_class)
      to_class.send(:attr_accessor, name)
      
      if cast
        cast_call = "cast_to_#{cast}"
        to_class.send(cast_call, name)
      end
      
      if required
        to_class.safe_reader name
      end
      
    end
  end
  
  # Defines a parameter, options conform to the Parameter class.
  # The parameter will of course add an attr_accessor to your class for the specified parameter,
  # but it will also 
  def parameter(name, options = {})
    options = {:name => name}.merge(options)
    param = Parameter.new(options)
    parameters.push(param)
    param.apply_to(self)
  end
  
  # Returns the array of the parameters defined for this class
  def parameters
    @ui_parameters ||= []
    @ui_parameters
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
tracksperanto-4.2.0 lib/tracksperanto/parameters.rb
tracksperanto-4.1.3 lib/tracksperanto/parameters.rb
tracksperanto-4.1.2 lib/tracksperanto/parameters.rb
tracksperanto-4.1.0 lib/tracksperanto/parameters.rb
tracksperanto-4.0.0 lib/tracksperanto/parameters.rb
tracksperanto-3.5.9 lib/tracksperanto/parameters.rb
tracksperanto-3.5.8 lib/tracksperanto/parameters.rb
tracksperanto-3.5.7 lib/tracksperanto/parameters.rb
tracksperanto-3.5.6 lib/tracksperanto/parameters.rb
tracksperanto-3.5.5 lib/tracksperanto/parameters.rb
tracksperanto-3.5.4 lib/tracksperanto/parameters.rb
tracksperanto-3.5.2 lib/tracksperanto/parameters.rb
tracksperanto-3.5.1 lib/tracksperanto/parameters.rb
tracksperanto-3.5.0 lib/tracksperanto/parameters.rb
tracksperanto-3.4.1 lib/tracksperanto/parameters.rb
tracksperanto-3.4.0 lib/tracksperanto/parameters.rb
tracksperanto-3.3.13 lib/tracksperanto/parameters.rb
tracksperanto-3.3.12 lib/tracksperanto/parameters.rb
tracksperanto-3.3.11 lib/tracksperanto/parameters.rb
tracksperanto-3.3.10 lib/tracksperanto/parameters.rb