Sha256: 764fbf5a2678c5a74144246a8feaea3bb9e9e4ab28df8fcf62dad57c63f03ba7

Contents?: true

Size: 1.18 KB

Versions: 40

Compression:

Stored size: 1.18 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

40 entries across 40 versions & 1 rubygems

Version Path
inch-0.9.0.rc1 lib/inch/api/options/base.rb
inch-0.8.0 lib/inch/api/options/base.rb
inch-0.8.0.rc2 lib/inch/api/options/base.rb
inch-0.8.0.rc1 lib/inch/api/options/base.rb
inch-0.7.1 lib/inch/api/options/base.rb
inch-0.7.0 lib/inch/api/options/base.rb
inch-0.6.4 lib/inch/api/options/base.rb
inch-0.6.3 lib/inch/api/options/base.rb
inch-0.6.2 lib/inch/api/options/base.rb
inch-0.6.1 lib/inch/api/options/base.rb
inch-0.6.0 lib/inch/api/options/base.rb
inch-0.6.0.rc6 lib/inch/api/options/base.rb
inch-0.6.0.rc5 lib/inch/api/options/base.rb
inch-0.6.0.rc4 lib/inch/api/options/base.rb
inch-0.6.0.rc3 lib/inch/api/options/base.rb
inch-0.6.0.rc2 lib/inch/api/options/base.rb
inch-0.6.0.rc1 lib/inch/api/options/base.rb
inch-0.5.10 lib/inch/api/options/base.rb
inch-0.5.9 lib/inch/api/options/base.rb
inch-0.5.8 lib/inch/api/options/base.rb