Module: LiveResource::Declarations::ClassMethods

Defined in:
lib/live_resource/declarations.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/live_resource/declarations.rb', line 27

def self.extended(base)
  class << base
    # Override the regular new routine with a custom new
    # which auto-registers the resource.
    alias :ruby_new :new

    def new(*params)
      obj = ruby_new(*params)
      LiveResource::register obj
      obj
    end
  end
end

Instance Method Details

#method_added(m) ⇒ Object



189
190
191
192
# File 'lib/live_resource/declarations.rb', line 189

def method_added(m)
  @_instance_methods ||= Set.new
  @_instance_methods << m
end

#remote_accessor(*params) ⇒ Object

call-seq:

remote_accessor :attr
remote_accessor :attr, { :opt => val }
remote_accessor :attr1, :attr2, :attr3

Declare remote attribute reader and writer. One or more symbols are used to declare multiple attributes, as in remote_writer. One symbol with a hash is used to declare an accessor with options; currently these options are only supported on the attribute write, and they are ignored on the attribute read.



148
149
150
151
# File 'lib/live_resource/declarations.rb', line 148

def remote_accessor(*params)
  remote_reader(*params)
  remote_writer(*params)
end

#remote_instance_attributesObject



153
154
155
156
# File 'lib/live_resource/declarations.rb', line 153

def remote_instance_attributes
  @_instance_attributes ||= Set.new
  @_instance_attributes.to_a
end

#remote_instance_methodsObject

Remote-callable methods for an instance.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/live_resource/declarations.rb', line 159

def remote_instance_methods
  @_instance_methods ||= Set.new
  @_instance_attributes ||= Set.new

  # Remove all instance attributes, then fiter out private and
  # protected methods.
  (@_instance_methods - @_instance_attributes).find_all do |m|
    if private_method_defined?(m) or protected_method_defined?(m)
      nil
    else
      m
    end
  end
end

#remote_reader(*params) ⇒ Object

call-seq:

remote_reader :attr
remote_reader :attr, { :opt => val }
remote_reader :attr1, :attr2, :attr3

Declare a remote attribute reader. A list of symbols is used to create multiple attribute readers.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/live_resource/declarations.rb', line 75

def remote_reader(*params)
  @_instance_attributes ||= Set.new
  options = {}

  # One symbol and one hash is treated as a reader with options;
  # right now there are no reader options, so just pop them off.
  if (params.length == 2) && (params.last.is_a? Hash)
    options = params.pop
  end

  # Everything left in params should be a symbol (i.e., method name).
  if params.find { |m| !m.is_a? Symbol }
    raise ArgumentError.new("Invalid or ambiguous arguments to remote_reader: #{params.inspect}")
  end

  params.each do |m|
    @_instance_attributes << m

    define_method("#{m}") do
      remote_attribute_read(m, options)
    end
  end
end

#remote_singleton_methodsObject

Remote-callable methods for a resource class.



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/live_resource/declarations.rb', line 175

def remote_singleton_methods
  @_singleton_methods ||= Set.new
  c = singleton_class

  # Filter out private and protected methods of the singleton class.
  @_singleton_methods.find_all do |m|
    if c.private_method_defined?(m) or c.protected_method_defined?(m)
      nil
    else
      m
    end
  end
end

#remote_writer(*params) ⇒ Object

call-seq:

remote_writer :attr
remote_writer :attr, { :opt => val }
remote_writer :attr1, :attr2, :attr3

Declare a remote attribute writer. One or more symbols are used to declare writers with default options. This creates methods matching the symbols provided, e.g.:

remote_writer :attr   ->    def attr=(value) [...]

One symbol and a hash is used to declare an attribute writer with options. Currently supported options:

  • :ttl (integer): time-to-live of attribute. After (TTL) seconds, the value of the attribute returns to nil.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/live_resource/declarations.rb', line 115

def remote_writer(*params)
  @_instance_attributes ||= Set.new
  options = {}

  # One symbol and one hash is treated as a writer with options.
  if (params.length == 2) && (params.last.is_a? Hash)
    options = params.pop
  end

  # Everything left in params should be a symbol (i.e., method name).
  if params.find { |m| !m.is_a? Symbol }
    raise ArgumentError.new("Invalid or ambiguous arguments to remote_writer: #{params.inspect}")
  end

  params.each do |m|
    @_instance_attributes << "#{m}=".to_sym

    define_method("#{m}=") do |value|
      remote_attribute_write(m, value, options)
    end
  end
end

#resource_class(class_name = nil) ⇒ Object

FIXME: comment this



53
54
55
56
57
58
59
60
61
# File 'lib/live_resource/declarations.rb', line 53

def resource_class(class_name = nil)
  if class_name
    # Called from class definition to set the resource's class.
    @_resource_class = class_name.to_sym
  else
    # Get the class-level resource class, which we'll always call :class.
    :class
  end
end

#resource_name(attribute_name = nil) ⇒ Object

FIXME: comment this



42
43
44
45
46
47
48
49
50
# File 'lib/live_resource/declarations.rb', line 42

def resource_name(attribute_name = nil)
  if attribute_name
    # Called from class definition to set the attribute from which we get a resource's name.
    @_resource_name = attribute_name.to_sym
  else
    # Get the class-level resource name.
    @_resource_class
  end
end

#resource_name_attrObject

Get the attribute which defines this resource’s name. (For internal use only.)



64
65
66
# File 'lib/live_resource/declarations.rb', line 64

def resource_name_attr
  @_resource_name
end

#singleton_method_added(m) ⇒ Object



194
195
196
197
# File 'lib/live_resource/declarations.rb', line 194

def singleton_method_added(m)
  @_singleton_methods ||= Set.new
  @_singleton_methods << m
end