Sha256: e198265964bf1458caaf54f1dbaf72b0caa10b0705dac671809794c7fa80f3fe

Contents?: true

Size: 1.2 KB

Versions: 6

Compression:

Stored size: 1.2 KB

Contents

module Rumx
  class Attribute
    attr_reader :name, :type, :description, :allow_read, :allow_write

    def initialize(name, type, description, allow_read, allow_write, options)
      @name        = name.to_sym
      @type        = type
      @description = description
      # List and hash attributes might set up the object for reading but the individual elements for writing
      @allow_read  = options[:allow_read] || allow_read
      @allow_write = options[:allow_write] || allow_write
      @options     = options
    end

    def get_value(bean)
      @allow_read ? bean.send(@name) : nil
    end

    def each_attribute_info(bean, ancestry, &block)
      yield AttributeInfo.new(self, bean, ancestry+[@name], get_value(bean))
    end

    def write?(bean, params)
      if @allow_write
        param_value(params) do |value|
          bean.send(@name.to_s+'=', @type.string_to_value(value))
          return true
        end
      end
      return false
    end

    def [](key)
      @options[key]
    end

    protected

    def param_value(params, &block)
      if params.has_key?(@name)
        yield params[@name]
      elsif params.has_key?(@name.to_s)
        yield params[@name.to_s]
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rumx-0.1.5 lib/rumx/attribute.rb
rumx-0.1.4 lib/rumx/attribute.rb
rumx-0.1.3 lib/rumx/attribute.rb
rumx-0.1.2 lib/rumx/attribute.rb
rumx-0.1.1 lib/rumx/attribute.rb
rumx-0.1.0 lib/rumx/attribute.rb