Sha256: c89c95131e745669942c424b48c9338efb752fe7dd473d2b5daa819d3644e454

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

module Representable
  # Created at class compile time. Keeps configuration options for one property.
  class Definition
    attr_reader :name, :sought_type, :from, :default, :representer_module, :attribute
    alias_method :getter, :name
    
    def initialize(sym, options={})
      @name               = sym.to_s
      @array              = options[:collection]
      @from               = (options[:from] || name).to_s
      @sought_type        = options[:class]
      @default            = options[:default]
      @default            ||= [] if array?
      @representer_module = options[:extend]  # DISCUSS: move to Representable::DCI?
      @attribute          = options[:attribute] 
    end

    def instance_variable_name
      :"@#{name}"
    end

    def setter
      :"#{name}="
    end
    
    def typed?
      sought_type.is_a?(Class)
    end
    
    def array?
      @array
    end
    
    # Applies the block to +value+ which might also be a collection.
    def apply(value)
      return value unless value # DISCUSS: is that ok here?
      
      if array?
        value = value.collect do |item|
          yield item
        end
      else
        value = yield value
      end
      
      value
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
representable-1.0.1 lib/representable/definition.rb
representable-1.0.0 lib/representable/definition.rb
representable-0.13.1 lib/representable/definition.rb