Sha256: d84791e1bb712f82d7e6582ef7a26f27b5e01bc24d367b01be59096c09595bc7

Contents?: true

Size: 1.55 KB

Versions: 4

Compression:

Stored size: 1.55 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
    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?
      deserialize_class.is_a?(Class) or representer_module or options[:instance]  # 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 deserialize_class
      options[:class]
    end

    def from
      # TODO: deprecate :from.
      (options[:from] || options[:as] || 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] or options[:decorator]
    end

    def attribute
      options[:attribute]
    end

    def content
      options[:content]
    end

    def skipable_nil_value?(value)
      value.nil? and not options[:render_nil]
    end

    def default
      options[:default]
    end

    def binding
      options[:binding]
    end

    def create_binding(*args)
      binding.call(self, *args)
    end

    def sync?
      options[:parse_strategy] == :sync
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
representable-1.7.7 lib/representable/definition.rb
representable-1.7.6 lib/representable/definition.rb
representable-1.7.5 lib/representable/definition.rb
representable-1.7.4 lib/representable/definition.rb