Sha256: 7b9aefd6e9035f88daf573bd31d527985b5e62651622a7c8e01aa21e936f3b86

Contents?: true

Size: 1.41 KB

Versions: 20

Compression:

Stored size: 1.41 KB

Contents

module ZTK::DSL::Core

  # @author Zachary Patten <zachary AT jovelabs DOT com>
  # @api private
  module Attributes

    def self.included(base)
      base.class_eval do
        base.send(:extend, ZTK::DSL::Core::Options::ClassMethods)
        base.add_option(:attribute)
        base.send(:extend, ZTK::DSL::Core::Attributes::ClassMethods)
      end
    end

    def attributes
      @attributes ||= Hash.new
    end

    # @author Zachary Patten <zachary AT jovelabs DOT com>
    module ClassMethods

      def attribute(key, options={})
        klass = self.to_s.split('::').last.downcase
        option_key = "#{klass}_#{key}"
        attribute_options[option_key] = options

        send(:define_method, key) do |*args|
          if (attributes[key].nil? && !self.class.attribute_options[option_key][:default].nil?)
            default_value = (self.class.attribute_options[option_key][:default].dup rescue self.class.attribute_options[option_key][:default])

            attributes[key] ||= default_value
          end

          if args.count == 0
            attributes[key]
          else
            send("#{key}=", *args)
          end
        end

        send(:define_method, "#{key}=") do |value|
          attributes[key] = value
          value
        end

        self.class.send(:define_method, "find_by_#{key}") do |value|
          all.select{ |object| (object.send(key) == value) }
        end

      end

    end

  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
ztk-3.1.0 lib/ztk/dsl/core/attributes.rb
ztk-3.0.4 lib/ztk/dsl/core/attributes.rb
ztk-3.0.3 lib/ztk/dsl/core/attributes.rb
ztk-3.0.2 lib/ztk/dsl/core/attributes.rb
ztk-3.0.1 lib/ztk/dsl/core/attributes.rb
ztk-3.0.0 lib/ztk/dsl/core/attributes.rb
ztk-2.4.2 lib/ztk/dsl/core/attributes.rb
ztk-2.4.1 lib/ztk/dsl/core/attributes.rb
ztk-2.4.0 lib/ztk/dsl/core/attributes.rb
ztk-2.3.1 lib/ztk/dsl/core/attributes.rb
ztk-2.3.0 lib/ztk/dsl/core/attributes.rb
ztk-2.2.0 lib/ztk/dsl/core/attributes.rb
ztk-2.1.1 lib/ztk/dsl/core/attributes.rb
ztk-2.1.0 lib/ztk/dsl/core/attributes.rb
ztk-2.0.1 lib/ztk/dsl/core/attributes.rb
ztk-2.0.0 lib/ztk/dsl/core/attributes.rb
ztk-1.19.2 lib/ztk/dsl/core/attributes.rb
ztk-1.19.1 lib/ztk/dsl/core/attributes.rb
ztk-1.19.0 lib/ztk/dsl/core/attributes.rb
ztk-1.18.5 lib/ztk/dsl/core/attributes.rb