class Portrayal::Schema

Constants

DEFINITION_OF_OBJECT_ENHANCEMENTS

Attributes

schema[R]

Public Class Methods

new() click to toggle source
# File lib/portrayal/schema.rb, line 38
def initialize; @schema = {}  end

Public Instance Methods

[](name) click to toggle source
# File lib/portrayal/schema.rb, line 40
def [](name);   @schema[name] end
add_keyword(name, default) click to toggle source
# File lib/portrayal/schema.rb, line 48
def add_keyword(name, default)
  name = name.to_sym
  @schema.delete(name) # Forcing keyword to be added at the end of the hash.
  @schema[name] = default.equal?(NULL) ? nil : Default.new(default)
end
attributes(object) click to toggle source
# File lib/portrayal/schema.rb, line 42
def attributes(object)
  Hash[object.class.portrayal.keywords.map { |k| [k, object.send(k)] }]
end
camelize(string) click to toggle source
# File lib/portrayal/schema.rb, line 46
def camelize(string); string.to_s.gsub(/(?:^|_+)([^_])/) { $1.upcase } end
definition_of_initialize() click to toggle source
# File lib/portrayal/schema.rb, line 58
def definition_of_initialize
  init_args = @schema.map { |name, default|
    "#{name}:#{default && " self.class.portrayal[:#{name}]"}"
  }.join(', ')

  init_assigns = @schema.keys.map { |name|
    "@#{name} = #{name}.is_a?(::Portrayal::Default) ? " \
      "(#{name}.call? ? instance_exec(&#{name}.value) : #{name}.value) : " \
      "#{name}"
  }.join('; ')

  "def initialize(#{init_args}); #{init_assigns} end"
end
initialize_dup(other) click to toggle source
Calls superclass method
# File lib/portrayal/schema.rb, line 54
def initialize_dup(other)
  super; @schema = other.schema.transform_values(&:dup)
end
keywords() click to toggle source
# File lib/portrayal/schema.rb, line 39
def keywords;   @schema.keys  end