Sha256: 7650f4feab15387943ac0271baad29d62fb4abe12abce260c773aab4b3fb7257
Contents?: true
Size: 1001 Bytes
Versions: 2
Compression:
Stored size: 1001 Bytes
Contents
module Structure def self.included(base) base .extend(ClassMethods) .instance_variable_set(:@attribute_names, []) end def attributes self.class.attribute_names.reduce({}) { |ret, name| ret.update(name => self.send(name)) } end def ==(other) attributes == other.attributes end def inspect "#<#{self.class} #{ attributes .map { |k, v| "#{k}=#{v.inspect}" } .join(', ') }>" end alias_method :to_h, :attributes alias_method :eql?, :== alias_method :to_s, :inspect module ClassMethods attr_reader :attribute_names def to_struct Struct.new(name, *attribute_names) do def initialize(data = {}) data.each { |k, v| self.send("#{k}=", v) } end end end def inherited(subclass) subclass.instance_variable_set(:@attribute_names, attribute_names.dup) end def attribute(name, &blk) @attribute_names << define_method(name, &blk) end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
structure-0.27.2 | structure.rb |
structure-0.27.1 | structure.rb |