Sha256: e18ab01391ac26dcd5567bbc2e172aef3c310d86ab22e5f1ad1cf3abc3671d7d

Contents?: true

Size: 1.57 KB

Versions: 3

Compression:

Stored size: 1.57 KB

Contents

module RakeFactory
  class Parameter
    attr_reader(
        :name,
        :default,
        :required,
        :configurable,
        :transform)
    attr_writer(:default)

    def initialize(name, options)
      @name = name
      @default = options[:default] || nil
      @required = options[:required] || false
      @configurable =
          options[:configurable].nil? ? true : !!options[:configurable]
      @transform = options[:transform] || lambda { |x| x }
    end

    def writer_method
      "#{name}="
    end

    def reader_method
      name
    end

    def instance_variable
      "@#{name}"
    end

    def define_on(klass)
      parameter = self
      klass.class_eval do
        define_method parameter.writer_method do |value|
          instance_variable_set(parameter.instance_variable, value)
        end

        define_method parameter.reader_method do
          stored_value = instance_variable_get(parameter.instance_variable)
          resolved_value = stored_value.respond_to?(:call) ?
              stored_value.call(*[self].slice(0, stored_value.arity)) :
              stored_value
          transformed_value = parameter.transform.call(resolved_value)
          transformed_value
        end
      end
    end

    def apply_default_to(instance)
      instance.send(writer_method, @default) unless @default.nil?
    end

    def configurable?
      @configurable
    end

    def dissatisfied_by?(instance)
      value = instance.send(reader_method)
      @required && value.nil?
    end

    def satisfied_by?(instance)
      !dissatisfied_by?(instance)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rake_factory-0.4.0 lib/rake_factory/parameter.rb
rake_factory-0.3.0 lib/rake_factory/parameter.rb
rake_factory-0.2.0 lib/rake_factory/parameter.rb