Sha256: 1e5ae98b4a7cfcb33c29443e0b6e436c7f68a24ee1f211b01ec0bfe6ed5bd5cf

Contents?: true

Size: 960 Bytes

Versions: 4

Compression:

Stored size: 960 Bytes

Contents

module Danica::VariablesHolder
  class VariablesBuilder
    include Danica::DSL
    include Danica::Wrapper

    attr_reader :instance, :attr_names

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

    def build
      names_hash = attr_names.extract_options!

      attr_names.each do |name|
        add_setter(name)
        add_reader(name)
        instance.variables_hash[name.to_sym] = wrap_value(name)
      end

      names_hash.each do |name, default|
        add_setter(name)
        add_reader(name)
        instance.variables_hash[name.to_sym] = wrap_value(default)
      end
    end

    private

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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
danica-2.4.3 lib/danica/variables_holder/variables_builder.rb
danica-2.4.2 lib/danica/variables_holder/variables_builder.rb
danica-2.4.1 lib/danica/variables_holder/variables_builder.rb
danica-2.4.0 lib/danica/variables_holder/variables_builder.rb