Sha256: 067733f0c62e5bb38c4036702b4ae66767326db312a660ba3ad64aff8fdc4910

Contents?: true

Size: 591 Bytes

Versions: 1

Compression:

Stored size: 591 Bytes

Contents

module JSONModel::Attributes
  def self.included(base)
    base.extend ClassMethods
    #TODO raise if the method already exists
    base.send(:attr_accessor, *base.attribute_names)
  end
  
  def initialize(attributes = {})
    attributes.each do |attribute, value|
      send("#{attribute}=", value)
    end
    super
  end
  
  def attributes
    self.class.attribute_names.inject({}) do |memo,name|
      memo[name] = send(name)
      memo
    end
  end
  
  module ClassMethods
    def attribute_names
      @attribute_names ||= schema['properties'].keys.map(&:to_sym)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jsonmodel-0.0.1 lib/jsonmodel/attributes.rb