Sha256: 399d97d00a574d09aad6125f3930c73b7667b71259d5fcc244c3430f9877dd24

Contents?: true

Size: 971 Bytes

Versions: 8

Compression:

Stored size: 971 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!.symbolize_keys

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

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

    private

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

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
danica-2.7.4 lib/danica/variables_holder/variables_builder.rb
danica-2.7.3 lib/danica/variables_holder/variables_builder.rb
danica-2.7.2 lib/danica/variables_holder/variables_builder.rb
danica-2.7.1 lib/danica/variables_holder/variables_builder.rb
danica-2.6.4 lib/danica/variables_holder/variables_builder.rb
danica-2.6.3 lib/danica/variables_holder/variables_builder.rb
danica-2.6.2 lib/danica/variables_holder/variables_builder.rb
danica-2.6.1 lib/danica/variables_holder/variables_builder.rb