Module: PartyResource::ClassMethods

Includes:
MethodDefine
Defined in:
lib/party_resource/party_resource.rb

Instance Method Summary collapse

Instance Method Details

#connect(name, options = {}) ⇒ nil

Connect a method call to a restful uri

Examples:

connect :find, :get => '/find/:id.ext', :with => :id, :on => :class
connect :update, :put => '/update/:var.ext', :on => :instance, :as => OtherClass
connect :save, :post => '/save/file', :with => :data, :as => :raw
connect :destroy, :delete => '/delete', :as => [OtherClass, :make_boolean]
connect :foo, :get => '/foo', :with => :value, :as => lambda {|data| "New #{data} Improved" }
connect :fetch_json, :get => '/big_data', :as => [:self, :from_json], :rescue => {'ResourceNotFound' => nil}
connect :include, :get => '/include', :on => :instance, :as => OtherClass, :including => {:thing => :value2}

Parameters:

  • name (Symbol)

    for method

  • options (Hash) (defaults to: {})

    the options to use to create the route

Options Hash (options):

  • :get/:put/:post/:delete (String)

    URI to attach to (key provides the HTTP verb)

  • :as (Object) — default: :self

    How to build data returned by the route

    :raw - raw data

    :self - self.new(data)

    class - class.new(data)

    Array(class, method_name) - class.method_name(data)

    lambda - lambda.call(data)

  • :with (Array/Symbol) — default: []

    List of parameter names

  • :on (Object) — default: :class

    Where to attach the method (:class/:instance)

  • :including (Hash<Symbol, Symbol>) — default: {}

    Hash of extra values to pass into object creation

  • :rescue (Hash<String, Object>) — default: {}

    Hash of Exception names to catch and the value to return

Returns:

  • (nil)


60
61
62
63
64
65
66
67
68
69
# File 'lib/party_resource/party_resource.rb', line 60

def connect(name, options={})
  level = options.delete(:on)
  options = {:as => :self, :connector => @party_connector}.merge(options)
  route = Route.new(options)

  define_method_on(level, name) do |*args|
    route.call(self, *args)
  end
  nil
end

#party_connector(name) ⇒ nil

Set the name of the connector to use for this class

Examples:

party_connector :my_shiny_connector

Parameters:

  • name (Symbol)

    Name of connector

Returns:

  • (nil)


127
128
129
130
# File 'lib/party_resource/party_resource.rb', line 127

def party_connector(name)
  @party_connector = name
  nil
end

#property(*names, options = {}) ⇒ nil

Define a property

Examples:

property :value, :from => :input_name
property :value2, :value3
property :nested_value, :from => [:block, :var]
property :other, :as => OtherClass
property :processed, :as => lambda { |data| "Processed: #{data}" }, :to => :output_name
property :child, :as => OtherPartyClass

Parameters:

  • names (Symbol)

    list of property names

  • options (Hash) (defaults to: {})

    the options to use to create the property

Options Hash (options):

  • :as (Object) — default: :self

    How to build property

    :raw - raw data

    :self - self.new(data)

    class - class.new(data)

    Array(class, :method) - class.method(data)

    lambda - lambda.call(data)

  • :from (Object) — default: property name

    where to find property value in incomming data hash

    symbol - name

    array - list of nested hash keys

  • :to (Object) — default: from value

    where to put property value in outgoing incomming data hash

    symbol - name

    array - list of nested hash keys

Returns:

  • (nil)


109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/party_resource/party_resource.rb', line 109

def property(*names)
  options = names.pop if names.last.is_a?(Hash)
  names.each do |name|
    name = name.to_sym
    define_method name do
      get_property(name)
    end
    @property_list ||= []
    @property_list << Property.new(name, options)
  end
  nil
end