Module: RestfulObjects::ObjectMacros

Defined in:
lib/restful_objects/object_macros.rb

Instance Method Summary collapse

Instance Method Details

#action(name, options = {}) ⇒ Object



31
32
33
# File 'lib/restful_objects/object_macros.rb', line 31

def action(name, options = {})
  RestfulObjects::DomainModel.current.types[self.name].actions.add(name.to_s, options)
end

#collection(name, type, options = {}) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/restful_objects/object_macros.rb', line 23

def collection(name, type, options = {})
  type = type.name if type.is_a? Class

  RestfulObjects::DomainModel.current.types[self.name].collections.add(name.to_s, type, options)

  self.class_eval { attr_reader name }
end

#property(name, type, options = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/restful_objects/object_macros.rb', line 3

def property(name, type, options = {})
  RestfulObjects::DomainModel.current.types[self.name].properties.add(name.to_s, type, options)
  if options[:read_only]
    self.class_eval { attr_reader name }
  else
    if not options[:max_length]
      self.class_eval { attr_accessor name }
    else
      self.class_eval do
        attr_reader name

        define_method "#{name}=".to_sym do |value|
          raise "string max length exceeded" if value && value.length > options[:max_length]
          instance_variable_set("@#{name}".to_sym, value)
        end
      end
    end
  end
end