Class: Unfuddle::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/unfuddle/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Base

Returns a new instance of Base.



9
10
11
# File 'lib/unfuddle/base.rb', line 9

def initialize(attributes)
  __set_attributes(attributes)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/unfuddle/base.rb', line 38

def method_missing(method_name, *args, &block)
  if has_attribute?(method_name)
    _attributes[method_name.to_s]
  else
    super(method_name, *args, &block)
  end
end

Class Method Details

.find_by(*attributes) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/unfuddle/base.rb', line 76

def find_by(*attributes)
  attributes.each do |attribute|
    instance_eval <<-RUBY
      def find_by_#{attribute}(value)
        all.find { |instance| instance.#{attribute} == value }
      end
    RUBY
  end
end

.has_many(collection_name, options = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/unfuddle/base.rb', line 94

def has_many(collection_name, options={})
  collection_name = collection_name.to_s
  path = collection_name
  individual_name = collection_name.singularize
  class_name = options.fetch(:class_name, individual_name.classify)
  
  require "unfuddle/#{individual_name}"
  
  class_eval <<-RUBY
    def #{collection_name}
      @#{collection_name} ||= get('#{path}').json.map { |attributes| #{class_name}.new(attributes) }
    end
    
    def #{individual_name}(id)
      attributes = find_#{individual_name}(id)
      #{class_name}.new(attributes) if attributes
    end
    
    def find_#{individual_name}(id)
      response = get("#{path}/\#{id}")
      
      return nil if response.status == 404
      Unfuddle.assert_response!(200, response)
      
      response.json
    end
    
    def new_#{individual_name}(attributes)
      #{class_name}.new(attributes)
    end
    
    def create_#{individual_name}(params)
      instance = #{class_name}.new(params)
      response = post('#{path}', instance)
      Unfuddle.assert_response!(201, response)
      
      instance.__set_attributes(response.json)
      
      unless instance.id
        id = response.location.chomp("/")[/\\d+$/].to_i
        instance.__set_attributes("id" => id) if id > 0
      end
      
      @#{collection_name}.push(instance) if @#{collection_name}
      instance
    end
  RUBY
end

Instance Method Details

#__set_attributes(attributes) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
# File 'lib/unfuddle/base.rb', line 21

def __set_attributes(attributes)
  raise ArgumentError, "attributes is expected to be a hash, but it was #{attributes.class} instead" unless attributes.is_a?(Hash)
  if @attributes
    @attributes.merge! attributes
  else
    @attributes = attributes
  end
end

#attributesObject

Respond to attributes



34
35
36
# File 'lib/unfuddle/base.rb', line 34

def attributes
  _attributes.dup
end

#delete_pathObject



209
210
211
# File 'lib/unfuddle/base.rb', line 209

def delete_path
  ""
end

#destroy!Object



195
196
197
# File 'lib/unfuddle/base.rb', line 195

def destroy!
  delete delete_path
end

#fetch!Object



181
182
183
184
185
186
187
188
189
# File 'lib/unfuddle/base.rb', line 181

def fetch!
  response = get(get_path)
  Unfuddle.assert_response!(200, response)
  __set_attributes(response.json)

rescue InvalidResponseError
  binding.pry if binding.respond_to?(:pry)
  raise $!
end

#get_pathObject



201
202
203
# File 'lib/unfuddle/base.rb', line 201

def get_path
  ""
end

#has_attribute?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/unfuddle/base.rb', line 50

def has_attribute?(attribute_name)
  _attributes.key?(attribute_name.to_s)
end

#idObject



17
18
19
# File 'lib/unfuddle/base.rb', line 17

def id
  @attributes["id"]
end

#put_pathObject



205
206
207
# File 'lib/unfuddle/base.rb', line 205

def put_path
  ""
end

#relative_pathObject

Nest Unfuddle requests

Raises:

  • (NotImplementedError)


149
150
151
# File 'lib/unfuddle/base.rb', line 149

def relative_path
  raise NotImplementedError
end

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/unfuddle/base.rb', line 46

def respond_to?(method_name)
  super(method_name) || has_attribute?(method_name)
end

#save!Object



191
192
193
# File 'lib/unfuddle/base.rb', line 191

def save!
  put put_path, self
end

#singular_nameObject



169
170
171
# File 'lib/unfuddle/base.rb', line 169

def singular_name
  self.class.name[/[^:]*$/].tableize.singularize
end

#to_jsonObject



177
178
179
# File 'lib/unfuddle/base.rb', line 177

def to_json
  JSON.dump({singular_name => to_params})
end

#to_paramsObject

Serialization



165
166
167
# File 'lib/unfuddle/base.rb', line 165

def to_params
  attributes
end

#to_xmlObject



173
174
175
# File 'lib/unfuddle/base.rb', line 173

def to_xml
  to_params.to_xml(root: singular_name)
end

#unfetched?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/unfuddle/base.rb', line 13

def unfetched?
  @attributes.keys == %w{id}
end

#update_attribute(attribute, value) ⇒ Object



65
66
67
68
# File 'lib/unfuddle/base.rb', line 65

def update_attribute(attribute, value)
  write_attribute(attribute, value)
  save!
end

#update_attributes!(attributes) ⇒ Object



58
59
60
61
62
63
# File 'lib/unfuddle/base.rb', line 58

def update_attributes!(attributes)
  attributes.each do |key, value|
    write_attribute(key, value)
  end
  save!
end

#write_attribute(attribute, value) ⇒ Object



54
55
56
# File 'lib/unfuddle/base.rb', line 54

def write_attribute(attribute, value)
  _attributes[attribute.to_s] = value
end