class PPT::Presenters::Entity

Public Class Methods

attribute(name, options = Hash.new) click to toggle source
# File lib/simple-orm/presenters.rb, line 109
def self.attribute(name, options = Hash.new)
  self.attributes[name] = Attribute.new(name)
end
attributes() click to toggle source
# File lib/simple-orm/presenters.rb, line 105
def self.attributes
  @attributes ||= Hash.new
end
new(values = Hash.new) click to toggle source
# File lib/simple-orm/presenters.rb, line 113
def initialize(values = Hash.new)
  # Let's consider it safe since this is not user input.
  # It might not be the best idea, but for now, who cares.
  values = PPT.symbolise_keys(values)

  values.each do |key, value|
    unless attribute = self.attributes[key]
      raise ArgumentError.new("No such attribute: #{key}")
    end

    attribute.set(value)
  end
end

Public Instance Methods

attributes() click to toggle source
# File lib/simple-orm/presenters.rb, line 127
def attributes
  @attributes ||= self.class.attributes.reduce(Hash.new) do |buffer, (name, attribute)|
    buffer.merge(name => attribute.dup.tap { |attribute| attribute.instance = self })
  end
end
method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/simple-orm/presenters.rb, line 133
def method_missing(name, *args, &block)
  if self.attributes.has_key?(name)
    self.attributes[name].get
  elsif name[-1] == '=' && self.attributes.has_key?(name.to_s[0..-2].to_sym)
    self.attributes[name.to_s[0..-2].to_sym].set(args.first)
  else
    super(name, *args, &block)
  end
end
respond_to_missing?(name, include_private = false) click to toggle source
Calls superclass method
# File lib/simple-orm/presenters.rb, line 143
def respond_to_missing?(name, include_private = false)
  self.attributes.has_key?(name) ||
    name[-1] == '=' && self.attributes.has_key?(name.to_s[0..-2].to_sym) ||
    super(name, include_private)
end
to_json() click to toggle source
# File lib/simple-orm/presenters.rb, line 157
def to_json
  self.values.to_json
end
validate() click to toggle source
# File lib/simple-orm/presenters.rb, line 161
def validate
  self.attributes.each do |_, attribute|
    attribute.validate!
  end
end
values(stage = nil) click to toggle source
# File lib/simple-orm/presenters.rb, line 149
def values(stage = nil)
  self.attributes.reduce(Hash.new) do |buffer, (name, attribute)|
    value = attribute.get(stage)
    buffer[name] = value if value && value != ''
    buffer
  end
end