module Recurly::Schema::SchemaFactory

A mixin that allows a class to be treated like a recurly object. This gives the class the power to describe it's schema. It adds the define_attribute method and a schema reader.

Public Instance Methods

schema() click to toggle source

Gets the schema for this class @return [Schema]

# File lib/recurly/schema/schema_factory.rb, line 11
def schema
  @schema ||= ::Recurly::Schema.new
end

Protected Instance Methods

define_attribute(name, type, options = {}) click to toggle source

Macro that allows this class to define it's schema and associated attribute getters and setters.

@example

class Account
  extend Schema::SchemaFactory
  define_attribute :code, String, {:read_only=>true}
end
account = Account.new(code: "mycode")
account.schema
#=> Recurly::Schema
acount.code = "newcode" # this method protected since read_only = true
account.code
#=> "mycode"
# File lib/recurly/schema/schema_factory.rb, line 31
def define_attribute(name, type, options = {})
  attribute = schema.add_attribute(name, type, options)

  # Define the reader
  define_method(name) do
    self.attributes[name]
  end

  # Define the writer
  define_method("#{name}=") do |val|
    self.attributes[name] = val
  end

  self
end