Class: Representable::Config

Inherits:
Inheritable::Hash show all
Extended by:
Forwardable
Defined in:
lib/representable/config.rb

Overview

Config contains three independent, inheritable directives: features, options and definitions. It is a hash - just add directives if you need them.

You may access/alter the property Definitions using #each, #collect, #add, #get.

  • features, [options]: “horizontally”+“vertically” inherited values (inline representer)

  • definitions, [options], wrap: “vertically” inherited (class inheritance, module inclusion)

Inheritance works via Config#inherit!(parent).

Defined Under Namespace

Classes: Definitions

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Inheritable::Hash::InstanceMethods

#clone, #inherit!

Constructor Details

#initialize(definition_class = Definition) ⇒ Config

Returns a new instance of Config.



55
56
57
58
59
60
61
62
# File 'lib/representable/config.rb', line 55

def initialize(definition_class=Definition)
  super()
  merge!(
    :features    => @features     = Inheritable::Hash.new,
    :definitions => @definitions  = Definitions.new(definition_class),
    :options     => @options      = Inheritable::Hash.new,
    :wrap        => nil )
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



63
64
65
# File 'lib/representable/config.rb', line 63

def options
  @options
end

Instance Method Details

#featuresObject



65
66
67
# File 'lib/representable/config.rb', line 65

def features
  @features.keys
end

#wrap=(value) ⇒ Object

#collect comes from Hash and then gets delegated to @definitions. don’t like that.



74
75
76
77
# File 'lib/representable/config.rb', line 74

def wrap=(value)
  value = value.to_s if value.is_a?(Symbol)
  self[:wrap] = Uber::Options::Value.new(value)
end

#wrap_for(name, context, *args, &block) ⇒ Object

Computes the wrap string or returns false.



80
81
82
83
84
85
86
87
88
89
# File 'lib/representable/config.rb', line 80

def wrap_for(name, context, *args, &block)
  return unless self[:wrap]

  value = self[:wrap].evaluate(context, *args)

  name = yield if block_given? # DISCUSS: deprecate/restructure the entire wrapping flow.

  return infer_name_for(name) if value === true
  value
end