Module: StorageRoom::Accessors::ClassMethods

Defined in:
lib/storage_room/accessors.rb

Instance Method Summary collapse

Instance Method Details

#attribute_optionsObject



84
85
86
# File 'lib/storage_room/accessors.rb', line 84

def attribute_options
  @attribute_options ||= {}
end

#attribute_options_including_superclassesObject



88
89
90
91
92
93
# File 'lib/storage_room/accessors.rb', line 88

def attribute_options_including_superclasses
  hash = attribute_options.dup
  hash.merge!(superclass.attribute_options_including_superclasses) if superclass.respond_to?(:attribute_options_including_superclasses)

  hash
end

#define_attribute_methods(name, options) ⇒ Object

Creates getter and setter for an attribute



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/storage_room/accessors.rb', line 72

def define_attribute_methods(name, options) # :nodoc:
  define_method name do
    attributes[name] || options[:default]
  end

  define_method "#{name}=" do |object|
    attributes[name] = object
  end

  self.attribute_options[name] = options
end

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

Defines a basic key for a Resource



54
55
56
57
# File 'lib/storage_room/accessors.rb', line 54

def key(name, options = {})
  options.merge!(:type => :key, :default => nil)
  define_attribute_methods(name, options)
end

#load(url, parameters = {}) ⇒ Object

Load an object with the specified URL from the API



19
20
21
22
23
24
25
26
27
# File 'lib/storage_room/accessors.rb', line 19

def load(url, parameters = {})
  return nil if url.blank?

  StorageRoom.log("Loading #{url}")
  httparty = get(url, parameters)

  handle_critical_response_errors(httparty)
  new_from_response_data(httparty.parsed_response.first[1])
end

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

Defines a to-many association for a Resource (embedded or association)



66
67
68
69
# File 'lib/storage_room/accessors.rb', line 66

def many(name, options = {})
  options.merge!(:type => :many, :default => [])
  define_attribute_methods(name, options)
end

#new_from_json_file(path) ⇒ Object

Build an object out of a local file



30
31
32
33
# File 'lib/storage_room/accessors.rb', line 30

def new_from_json_file(path)
  json = ::File.read(path)
  new_from_json_string(json)
end

#new_from_json_string(json_string) ⇒ Object

Build an object out of the POST body



36
37
38
39
# File 'lib/storage_room/accessors.rb', line 36

def new_from_json_string(json_string)
  hash = JSON.parse(json_string)
  new_from_response_data(hash.first[1])
end

#new_from_response_data(response_data) ⇒ Object

Creates a new object of the correct class and initializes it from the response data



42
43
44
45
46
47
48
49
50
51
# File 'lib/storage_room/accessors.rb', line 42

def new_from_response_data(response_data)
  object = StorageRoom.class_for_name(response_data['@type']).new.set_from_response_data(response_data)

  if object.is_a?(Entry) && !object.loaded? && !object.proxy?
    StorageRoom.log("Return  #{response_data['url']} proxied")
    Proxy.new(object)
  else
    object
  end
end

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

Defines a to-one association for a Resource (embedded or association)



60
61
62
63
# File 'lib/storage_room/accessors.rb', line 60

def one(name, options = {})
  options.merge!(:type => :one, :default => nil)
  define_attribute_methods(name, options)
end

#response_data_is_association?(hash) ⇒ Boolean

Is the passed hash of response data a real object or only an association with a URL but no actual data

Returns:

  • (Boolean)


14
15
16
# File 'lib/storage_room/accessors.rb', line 14

def response_data_is_association?(hash)
  hash.size == 2 && hash.include?('@type') && hash.include?('url')
end