class AbstractObject

Public Class Methods

accept(key, options = {}) click to toggle source
# File lib/services/abstract_object.rb, line 3
def self.accept(key, options = {})
        key = key.to_sym
        set_definitions key, options
        define_accessor key, options
end
new(options = {}) click to toggle source
# File lib/services/abstract_object.rb, line 9
def initialize(options = {})
        raise "Definitions can not be empty" if self.class.definitions.empty?
        @attributes = rename filter options.kind_of?(AbstractObject) ? options.to_hash : options
end

Private Class Methods

define_accessor(key, options) click to toggle source
# File lib/services/abstract_object.rb, line 20
def self.define_accessor(key, options)
        aliases = options.delete(:as) || []
        define_reader key, [key, *aliases], options.delete(:default)
        define_writer key, [key, *aliases], options.delete(:callback)
end
define_reader(key, aliases, default) click to toggle source
# File lib/services/abstract_object.rb, line 26
def self.define_reader(key, aliases, default)
        aliases.each { |name| define_method name, &reader_block(key, default) }
end
define_writer(key, aliases, callbacks) click to toggle source
# File lib/services/abstract_object.rb, line 34
def self.define_writer(key, aliases, callbacks)
        aliases.each { |name| define_method "#{ name }=", &writer_block(key, callbacks) }
end
definitions() click to toggle source
# File lib/services/abstract_object.rb, line 46
def self.definitions
        @definitions ||= (parent = self.superclass).respond_to?(:definitions) ? parent.definitions.dup : {}
end
reader_block(key, default) click to toggle source
# File lib/services/abstract_object.rb, line 30
def self.reader_block(key, default)
        default.is_a?(Proc) ? -> { attributes[key] ||= default.call(self) } : -> { attributes[key] ||= default }
end
set_definitions(key, options) click to toggle source
# File lib/services/abstract_object.rb, line 42
def self.set_definitions(key, options)
        [key, *options.fetch(:as, [])].each { |name| definitions[name] = key }
end
writer_block(key, callback) click to toggle source
# File lib/services/abstract_object.rb, line 38
def self.writer_block(key, callback)
        callback.is_a?(Proc) ? -> (value) { attributes[key] = callback.call(value) } : -> (value) { attributes[key] = value }
end

Public Instance Methods

to_hash() click to toggle source
# File lib/services/abstract_object.rb, line 14
def to_hash
        attributes.dup
end

Private Instance Methods

attributes() click to toggle source
# File lib/services/abstract_object.rb, line 50
def attributes
        @attributes ||= {}
end
filter(attributes) click to toggle source
# File lib/services/abstract_object.rb, line 54
def filter(attributes)
        attributes.delete_if { |key| self.class.definitions[key].nil? }
end
rename(attributes) click to toggle source
# File lib/services/abstract_object.rb, line 58
def rename(attributes)
        attributes.keys.each do |key|
                if (name = self.class.definitions[key])
                        attributes[name] = attributes.delete(key)
                end
        end
        attributes
end