Sha256: 9509fd48874466d33305c61cee9fecdc5f1a9e2d6f98f6f04a28b54ca4fd424f

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

module Presenting
  # represents an attribute meant to be read from a record
  # used for things like Grid and Details.
  # not intended for things like Form or FieldSearch
  class Attribute
    include Presenting::Configurable
    
    def name=(val)
      self.value ||= val # don't lazy define :value, because we're about to typecast here
      if val.is_a? Symbol
        @name = val.to_s.titleize
      else
        @name = val.to_s
      end
    end
    attr_reader :name
    
    # The short programmatic name for this field. Can be used as a CSS class, sorting name, etc.
    def id=(val)
      @id = val.to_s
    end
    
    def id
      @id ||= name.to_s.underscore.gsub(/[^a-z0-9]/i, '_').gsub(/__+/, '_').sub(/_$/, '')
    end
  
    # Where a field's value comes from. Depends heavily on the data type you provide.
    # - String: fixed value (as provided)
    # - Symbol: a method on the record (no arguments)
    # - Proc: a custom block that accepts the record as an argument
    attr_accessor :value
    
    def value_from(obj) #:nodoc:
      case value
        when Symbol: obj.is_a?(Hash) ? obj[value] : obj.send(value)
        when String: value
        when Proc:   value.call(obj)
      end
    end
    
    # whether html should be sanitize. right now this actually means html escaping.
    # consider: by default, do not sanitize if value is a String?
    attr_writer :sanitize
    def sanitize?
      unless defined? @sanitize
        @sanitize = Presenting::Defaults.sanitize_fields
      end
      @sanitize
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
presenting-2.0.1 lib/presenting/attribute.rb