Sha256: c6ab589c2b9f1d92f2030bde71899c3676cbc22d7d9d3503f6c7b5ed9f875e20

Contents?: true

Size: 1.55 KB

Versions: 15

Compression:

Stored size: 1.55 KB

Contents

require_relative 'values'

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

    def initialize(name, options)
      @name = name
      @default = options[:default]
      @required = options[:required] || false
      @transform = options[:transform] || lambda { |x| x }
      @configurable =
          options[:configurable].nil? ? true : !!options[:configurable]
    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 = Values.resolve(stored_value).evaluate([self])
          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 read_from(instance)
      instance.send(reader_method)
    end

    def configurable?
      @configurable
    end

    def dissatisfied_by?(instance)
      @required && read_from(instance).nil?
    end

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

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
rake_factory-0.30.0.pre.5 lib/rake_factory/parameter.rb
rake_factory-0.30.0.pre.4 lib/rake_factory/parameter.rb
rake_factory-0.30.0.pre.3 lib/rake_factory/parameter.rb
rake_factory-0.30.0.pre.2 lib/rake_factory/parameter.rb
rake_factory-0.30.0.pre.1 lib/rake_factory/parameter.rb
rake_factory-0.29.0 lib/rake_factory/parameter.rb
rake_factory-0.28.0.pre.2 lib/rake_factory/parameter.rb
rake_factory-0.28.0.pre.1 lib/rake_factory/parameter.rb
rake_factory-0.27.0 lib/rake_factory/parameter.rb
rake_factory-0.26.0.pre.1 lib/rake_factory/parameter.rb
rake_factory-0.24.0.pre.2 lib/rake_factory/parameter.rb
rake_factory-0.25.0 lib/rake_factory/parameter.rb
rake_factory-0.24.0.pre.1 lib/rake_factory/parameter.rb
rake_factory-0.23.0 lib/rake_factory/parameter.rb
rake_factory-0.22.0.pre.1 lib/rake_factory/parameter.rb