Class: Recurly::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/recurly/schema.rb,
lib/recurly/schema/request_caster.rb,
lib/recurly/schema/schema_factory.rb,
lib/recurly/schema/resource_caster.rb,
lib/recurly/schema/schema_validator.rb

Overview

The class responsible for describing a schema. This is used for requests and resources.

Defined Under Namespace

Modules: RequestCaster, ResourceCaster, SchemaFactory, SchemaValidator Classes: ArrayAttribute, Attribute, BooleanAttribute, DateTimeAttribute, PrimitiveAttribute, ResourceAttribute

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchema

Returns a new instance of Schema.



9
10
11
# File 'lib/recurly/schema.rb', line 9

def initialize
  @attributes = {}
end

Instance Attribute Details

#attributesHash<String,Attribute> (readonly)

The attributes in the schema

Returns:



7
8
9
# File 'lib/recurly/schema.rb', line 7

def attributes
  @attributes
end

Class Method Details

.get_recurly_class(type) ⇒ Request, Resource

Gets a recurly class given a symbol name.

Examples:

Schema.get_recurly_class(:Account)
#=> Recurly::Resources::Account

Parameters:

  • type (Symbol)

    The name of the class you wish to find

Returns:

Raises:

  • ArgumentError If class can't be found.



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/recurly/schema.rb', line 41

def self.get_recurly_class(type)
  raise ArgumentError, "#{type.inspect} must be a symbol but is a #{type.class}" unless type.is_a?(Symbol)

  if type == :Address
    Recurly::Resources::Address
  elsif Recurly::Requests.const_defined?(type, false)
    Recurly::Requests.const_get(type, false)
  elsif Recurly::Resources.const_defined?(type, false)
    Recurly::Resources.const_get(type, false)
  else
    raise ArgumentError, "Recurly type '#{type}' is unknown"
  end
end

Instance Method Details

#add_attribute(name, type, options) ⇒ Object

Adds an attribute to the schema definition

Parameters:

  • name (Symbol)

    The name of the attribute

  • type (Class, Symbol)

    The type of the attribute. Use capitalized symbol for Recurly class. Example: :Account.

  • options (Schema::Attribute)

    The created and registered attribute object.



18
19
20
21
22
# File 'lib/recurly/schema.rb', line 18

def add_attribute(name, type, options)
  attribute = Attribute.build(type, options)
  @attributes[name.to_s] = attribute
  attribute
end

#get_attribute(name) ⇒ Attribute?

Gets an attribute from this schema given a name

Parameters:

  • name (String, Symbol)

    The name/key of the attribute

Returns:

  • (Attribute, nil)

    The found Attribute. nil if not found.



28
29
30
# File 'lib/recurly/schema.rb', line 28

def get_attribute(name)
  @attributes[name.to_s]
end