class Materialize::Entity
Attributes
source[R]
Public Class Methods
new(attributes)
click to toggle source
# File lib/materialize/entity.rb, line 19 def initialize(attributes) @source = attributes raise "Attributes must be a hash" unless attributes.is_a?(Hash) attributes.each_pair do |key, value| value = attempt_entity_conversion(key, value) if collection?(value) instance_variable_set("@#{key}", value) (class << self; self; end).class_eval do attr_reader key.to_sym end end end
wrap(entities_data)
click to toggle source
# File lib/materialize/entity.rb, line 11 def wrap(entities_data) entities_data.map { |entity_data| new(entity_data) } end
Public Instance Methods
empty?()
click to toggle source
# File lib/materialize/entity.rb, line 33 def empty? source.empty? end
Private Instance Methods
attempt_entity_conversion(key, value)
click to toggle source
# File lib/materialize/entity.rb, line 39 def attempt_entity_conversion(key, value) if class_exists?(covert_to_entity_class_name(key)) klass = Module.const_get(covert_to_entity_class_name(key)) if value.is_a?(Array) klass.wrap(value) else klass.new(value) end else value end end
base_name_for(key)
click to toggle source
# File lib/materialize/entity.rb, line 60 def base_name_for(key) key.to_s.singularize.split('_').collect(&:capitalize).join end
collection?(value)
click to toggle source
# File lib/materialize/entity.rb, line 52 def collection?(value) value.is_a? Enumerable end
covert_to_entity_class_name(key)
click to toggle source
# File lib/materialize/entity.rb, line 56 def covert_to_entity_class_name(key) "Entities::#{base_name_for(key)}" end