Class: Measurb::DimensionBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/measurb/dimension_builder.rb

Overview

Factory class for building new dimension classes

Defined Under Namespace

Classes: ValueProxy

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, &block) ⇒ DimensionBuilder

Initialize the builder

Parameters:

  • name (Symbol, String)

    Name of the dimension

  • options (Hash) (defaults to: {})
  • block (Proc)

    Block for defining conversions to other dimensions

Options Hash (options):

  • :abbrev (String) — default: nil

    The dimension abbreviation



20
21
22
23
24
25
# File 'lib/measurb/dimension_builder.rb', line 20

def initialize(name, options = {}, &block)
  @name                 = name
  @def_block            = block
  @options              = options
  @dimension_class_name = self.class.get_dimension_class_name(name)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Use to build conversion methods to other dimensions



36
37
38
# File 'lib/measurb/dimension_builder.rb', line 36

def method_missing(name, *args, &block)
  build_default_conversion(name, args.first)
end

Instance Attribute Details

#dimension_classMeasurb::Dimension (readonly)

Get the dimension class, creating it if it’s not available

Returns:



30
31
32
33
# File 'lib/measurb/dimension_builder.rb', line 30

def dimension_class
  build_dimension_class unless defined?(@dimension_class)
  @dimension_class
end

#dimension_class_nameObject (readonly)

Returns the value of attribute dimension_class_name.



4
5
6
# File 'lib/measurb/dimension_builder.rb', line 4

def dimension_class_name
  @dimension_class_name
end

#valueObject (readonly)

Returns the value of attribute value.



4
5
6
# File 'lib/measurb/dimension_builder.rb', line 4

def value
  @value
end

Class Method Details

.get_dimension_class_name(name) ⇒ String

Get an appropriate class name for a dimension name

Parameters:

  • name (Symbol, String)

    Name of the dimension

Returns:

  • (String)

    Name of the dimension class



10
11
12
# File 'lib/measurb/dimension_builder.rb', line 10

def self.get_dimension_class_name(name)
  name.to_s.split('_').map(&:capitalize).join
end