class Entity::Base
Attributes
attributes[R]
Public Class Methods
fields()
click to toggle source
# File lib/entity_rb/base.rb, line 34 def self.fields return @fields if @fields @fields = parent && parent.respond_to?(:fields) ? parent.fields.dup : [] field @fields end
new(args={})
click to toggle source
# File lib/entity_rb/base.rb, line 5 def initialize(args={}) initialize_attributes(args || {}) set_attributes(args) end
Protected Class Methods
define_writer(key)
click to toggle source
# File lib/entity_rb/base.rb, line 65 def self.define_writer(key) define_method "#{key}=", ->(value){ key = __method__[0..-2] attributes[key.to_sym] = value self.instance_variable_set("@#{key}".to_sym, value) } end
field(key)
click to toggle source
# File lib/entity_rb/base.rb, line 47 def self.field(key) raise ArgumentError.new('Fields must be a String, Symbol or Array') unless [String, Symbol, Array].include? key.class if key.is_a? Array key.each do |k| begin self.field k rescue ArgumentError end end else fields.push key.to_sym unless fields.include?(key.to_sym) attr_reader key.to_sym define_writer(key) end end
Private Class Methods
parent()
click to toggle source
# File lib/entity_rb/base.rb, line 83 def self.parent (respond_to?(:superclass) && superclass != Object) ? superclass : nil end
Public Instance Methods
attributes=(args)
click to toggle source
# File lib/entity_rb/base.rb, line 14 def attributes=(args) @attributes ||= {} args.each do |key, value| set_field(key, value) end end
Also aliased as: initialize_attributes
fields()
click to toggle source
# File lib/entity_rb/base.rb, line 42 def fields self.class.fields end
set_attributes(args)
click to toggle source
# File lib/entity_rb/base.rb, line 11 def set_attributes(args) end
to_h()
click to toggle source
# File lib/entity_rb/base.rb, line 24 def to_h {}.tap do |hash| fields.each do |key| hash[key] = send(key) unless send(key).nil? end end end
Also aliased as: to_hash
Protected Instance Methods
set_field(key, value)
click to toggle source
# File lib/entity_rb/base.rb, line 73 def set_field(key, value) key = key.to_sym if fields.include? key send("#{key}=", value) @attributes[key] = value end end