Sha256: aee5e266810fd0ac7915602d68bcd5d14e1a73203fa3420446f0376a0ed0bcc0

Contents?: true

Size: 1.15 KB

Versions: 36

Compression:

Stored size: 1.15 KB

Contents

module Inch
  module API
    module Options
      class Base
        class << self
          # Creates an attribute with an optional default value
          #
          # @param name [Symbol] the name of the attribute
          # @param default [nil] the default value of the attribute
          # @return [void]
          def attribute(name, default = nil)
            define_method(name) do
              instance_variable_get("@#{name}") || default
            end
            @attributes ||= {}
            @attributes[to_s] ||= []
            @attributes[to_s] << name
          end

          def attribute_names
            @attributes ||= {}
            @attributes[to_s] ||= []
          end
        end

        def initialize(options_or_hash)
          self.class.attribute_names.each do |name|
            read options_or_hash, name
          end
        end

        protected

        def read(options_or_hash, name)
          value = if options_or_hash.is_a?(Hash)
            options_or_hash[name]
          else
            options_or_hash.send(name)
          end
          instance_variable_set("@#{name}", value)
        end
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
inch-0.5.0.rc3 lib/inch/api/options/base.rb
inch-0.5.0.rc2 lib/inch/api/options/base.rb
inch-0.5.0.rc1 lib/inch/api/options/base.rb
inch-0.4.6 lib/inch/api/options/base.rb
inch-0.4.5 lib/inch/api/options/base.rb
inch-0.4.4 lib/inch/api/options/base.rb
inch-0.4.4.rc4 lib/inch/api/options/base.rb
inch-0.4.4.rc3 lib/inch/api/options/base.rb
inch-0.4.4.rc2 lib/inch/api/options/base.rb
inch-0.4.4.rc1 lib/inch/api/options/base.rb
inch-0.4.3 lib/inch/api/options/base.rb
inch-0.4.3.rc2 lib/inch/api/options/base.rb
inch-0.4.3.rc1 lib/inch/api/options/base.rb
inch-0.4.2 lib/inch/api/options/base.rb
inch-0.4.1 lib/inch/api/options/base.rb
inch-0.4.0 lib/inch/api/options/base.rb
inch-0.4.0.rc3 lib/inch/api/options/base.rb
inch-0.4.0.rc2 lib/inch/api/options/base.rb
inch-0.4.0.rc1 lib/inch/api/options/base.rb
inch-0.3.4.rc1 lib/inch/api/options/base.rb