Sha256: 49e43c05c2ea3938cdb23255f685a52efc5d53147ce92bff7edccc9080da2e5d

Contents?: true

Size: 1.36 KB

Versions: 7

Compression:

Stored size: 1.36 KB

Contents

module Representable
  # Created at class compile time. Keeps configuration options for one property.
  class Definition
    attr_reader :name, :options
    alias_method :getter, :name
    
    def initialize(sym, options={})
      @name     = sym.to_s
      @options  = options
      
      options[:default] ||= [] if array?  # FIXME: move to CollectionBinding!
    end
    
    def clone
      self.class.new(name, options.clone) # DISCUSS: make generic Definition.cloned_attribute that passes list to constructor.
    end

    def setter
      :"#{name}="
    end
    
    def typed?
      sought_type.is_a?(Class) or representer_module  # also true if only :extend is set, for people who want solely rendering.
    end
    
    def array?
      options[:collection]
    end
    
    def hash?
      options[:hash]
    end
    
    def sought_type
      options[:class]
    end
    
    def from
      (options[:from] || name).to_s
    end
    
    def default_for(value)
      return default if skipable_nil_value?(value)
      value
    end
    
    def has_default?
      options.has_key?(:default)
    end
    
    def representer_module
      options[:extend]
    end
    
    def attribute
      options[:attribute]
    end
    
    def skipable_nil_value?(value)
      value.nil? and not options[:render_nil]
    end
    
    def default
      options[:default]
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
representable-1.2.7 lib/representable/definition.rb
representable-1.2.6 lib/representable/definition.rb
representable-1.2.5 lib/representable/definition.rb
representable-1.2.4 lib/representable/definition.rb
representable-1.2.3 lib/representable/definition.rb
representable-1.2.2 lib/representable/definition.rb
representable-1.2.1 lib/representable/definition.rb