Sha256: e4dc8ca63e9e7b67f8eb3d315ae969543705ee113075e83b1d941f83ba82d8dc

Contents?: true

Size: 947 Bytes

Versions: 4

Compression:

Stored size: 947 Bytes

Contents

require 'darthjee/core_ext'

module Danica::VariablesHolder
  class VariablesBuilder
    attr_reader :instance, :attr_names

    def initialize(attr_names, instance)
      @instance = instance
      @attr_names = attr_names
    end

    def build
      attr_names.extract_options!.each do |name, default|
        add_setter(name)
        add_reader(name, default)
        instance.variables_names << name
      end

      attr_names.each do |name|
        add_setter(name)
        add_reader(name, name)
        instance.variables_names << name
      end
    end

    private

    def add_setter(name)
      instance.send(:define_method, "#{name}=") do |value|
        variables_hash[name.to_sym] = wrap_value(value)
        @variables = variables_hash.values
      end
    end

    def add_reader(name, default)
      instance.send(:define_method, name) do
        variables_hash[name.to_sym] ||= wrap_value(default)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
danica-2.2.1 lib/danica/variables_holder/variables_builder.rb
danica-2.2.0 lib/danica/variables_holder/variables_builder.rb
danica-2.1.1 lib/danica/variables_holder/variables_builder.rb
danica-2.1.0 lib/danica/variables_holder/variables_builder.rb