Sha256: a73adc0316a35bca411d1ae2172e827bcd26e109a5a0f33cc22f2943c2359f87

Contents?: true

Size: 918 Bytes

Versions: 14

Compression:

Stored size: 918 Bytes

Contents

# frozen_string_literal: true

module Attribeauty
  # base class to inherit from
  # class MyClass < Attribeauty::Base
  class Base
    def self.attribute(name, type)
      @attributes ||= {}
      return if @attributes[name]

      @attributes[name] = type

      class_eval(<<-CODE, __FILE__, __LINE__ + 1)
        def #{name}=(value); @#{name} = TypeCaster.run(value, #{type.inspect}); end

        def #{name};@#{name};end
      CODE

      return unless type == :boolean

      class_eval(<<-CODE, __FILE__, __LINE__ + 1)
        def #{name}?; !!#{name}; end
      CODE
    end

    def initialize(**attributes)
      attributes.each do |key, value|
        method = "#{key}=".to_sym
        public_send(method, value)
      end
    end

    def assign_attributes(**attributes)
      attributes.each do |key, value|
        method = "#{key}=".to_sym
        public_send(method, value)
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
attribeauty-0.4.7 lib/attribeauty/base.rb
attribeauty-0.4.6 lib/attribeauty/base.rb
attribeauty-0.4.5 lib/attribeauty/base.rb
attribeauty-0.4.4 lib/attribeauty/base.rb
attribeauty-0.4.3 lib/attribeauty/base.rb
attribeauty-0.4.2 lib/attribeauty/base.rb
attribeauty-0.4.1 lib/attribeauty/base.rb
attribeauty-0.4.0 lib/attribeauty/base.rb
attribeauty-0.3.4 lib/attribeauty/base.rb
attribeauty-0.3.3 lib/attribeauty/base.rb
attribeauty-0.3.2 lib/attribeauty/base.rb
attribeauty-0.3.1 lib/attribeauty/base.rb
attribeauty-0.3.0 lib/attribeauty/base.rb
attribeauty-0.2.0 lib/attribeauty/base.rb