Module: Her::Model::ORM

Extended by:
ActiveSupport::Concern
Includes:
ComparisonMethods, CreateMethods, DestroyMethods, ErrorMethods, FieldsDefinition, FindMethods, PersistanceMethods, RelationMapper, SaveMethods, SerializationMethods, UpdateMethods
Included in:
Her::Model
Defined in:
lib/her/model/orm.rb,
lib/her/model/orm/find_methods.rb,
lib/her/model/orm/save_methods.rb,
lib/her/model/orm/error_methods.rb,
lib/her/model/orm/create_methods.rb,
lib/her/model/orm/update_methods.rb,
lib/her/model/orm/destroy_methods.rb,
lib/her/model/orm/relation_mapper.rb,
lib/her/model/orm/fields_definition.rb,
lib/her/model/orm/comparison_methods.rb,
lib/her/model/orm/persistance_methods.rb,
lib/her/model/orm/serialization_methods.rb

Overview

This module adds ORM-like capabilities to the model

Defined Under Namespace

Modules: ClassMethods, ComparisonMethods, CreateMethods, DestroyMethods, ErrorMethods, FieldsDefinition, FindMethods, PersistanceMethods, RelationMapper, SaveMethods, SerializationMethods, UpdateMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SerializationMethods

#hash, #to_params

Methods included from PersistanceMethods

#destroyed?, #new?, #persisted?

Methods included from ComparisonMethods

#==, #eql?

Methods included from ErrorMethods

#invalid?, #valid?

Methods included from UpdateMethods

#update_attribute, #update_attributes, #update_attributes!

Methods included from SaveMethods

#create_or_update, #save, #save!

Methods included from DestroyMethods

#delete, #destroy

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object

Handles missing methods by routing them through @data



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/her/model/orm.rb', line 69

def method_missing(method, *args, &blk)
  if method.to_s.end_with?('=')
    @data[method.to_s.chomp('=').to_sym] = args.first
  elsif method.to_s.end_with?('?')
    @data.include?(method.to_s.chomp('?').to_sym)
  elsif @data.include?(method)
    @data[method]
  else
    super
  end
end

Instance Attribute Details

#dataObject Also known as: attributes

Returns the value of attribute data.



18
19
20
# File 'lib/her/model/orm.rb', line 18

def data
  @data
end

#errorsObject

Returns the value of attribute errors.



18
19
20
# File 'lib/her/model/orm.rb', line 18

def errors
  @errors
end

#metadataObject

Returns the value of attribute metadata.



18
19
20
# File 'lib/her/model/orm.rb', line 18

def 
  @metadata
end

Class Method Details

.initialize_collection(klass, parsed_data = {}) ⇒ Object

Initialize a collection of resources



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/her/model/orm.rb', line 53

def self.initialize_collection(klass, parsed_data={})
  collection_data = parsed_data[:data].map do |item_data|
    resource = klass.new(klass.parse(item_data))
    klass.wrap_in_hooks(resource, :find)
    resource
  end

  collection_class = Her::Collection
  if parsed_data[:metadata].is_a?(Hash) && parsed_data[:metadata].has_key?(:current_page)
    collection_class = Her::PaginatedCollection
  end
  collection_class.new(collection_data, parsed_data[:metadata], parsed_data[:errors])
end

.use_setter_methods(model, params) ⇒ Object

Use setter methods of model for each key / value pair in params Return key / value pairs for which no setter method was defined on the model



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/her/model/orm.rb', line 112

def self.use_setter_methods(model, params)
  setter_method_names = model.class.setter_method_names
  params.inject({}) do |memo, (key, value)|
    setter_method = key.to_s + '='
    if setter_method_names.include?(setter_method)
      model.send(setter_method, value)
    else
      if key.is_a?(String)
        key = key.to_sym
      end
      memo[key] = value
    end
    memo
  end
end

Instance Method Details

#assign_data(new_data) ⇒ Object Also known as: assign_attributes

Assign new data to an instance



87
88
89
90
# File 'lib/her/model/orm.rb', line 87

def assign_data(new_data)
  new_data = Her::Model::ORM.use_setter_methods(self, new_data)
  @data.update new_data
end

#convert_types(parsed_data) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/her/model/orm.rb', line 36

def convert_types(parsed_data)
  parsed_data.each do |key, value|
    "2013-01-26T09:29:39.358Z"
    if value.is_a?(String)
      m = value.match(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?Z/)
      next if m.nil?
      if m[1].nil?
        parsed_data[key] = Time.strptime(value, '%Y-%m-%dT%H:%M:%S%Z')
      else
        parsed_data[key] = Time.strptime(value, '%Y-%m-%dT%H:%M:%S.%L%Z')
      end
    end
  end
end

#get_data(attribute_name) ⇒ Object

Handles returning attribute value from data



99
100
101
# File 'lib/her/model/orm.rb', line 99

def get_data(attribute_name)
  @data[attribute_name]
end

#has_data?(attribute_name) ⇒ Boolean

Handles returning true for the accessible attributes

Returns:

  • (Boolean)


94
95
96
# File 'lib/her/model/orm.rb', line 94

def has_data?(attribute_name)
  @data.include?(attribute_name)
end

#idObject

Override the method to prevent from returning the object ID (in ruby-1.8.7)



105
106
107
# File 'lib/her/model/orm.rb', line 105

def id
  @data[:id] || super
end

#initialize(params = {}) ⇒ Object

Initialize a new object with data received from an HTTP request



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/her/model/orm.rb', line 23

def initialize(params={})
  fields = self.class.instance_variable_defined?(:@fields) ? self.class.instance_variable_get(:@fields) : []
  @data = Hash[fields.map{ |field| [field, nil] }]
  @metadata = params.delete(:_metadata) || {}
  @errors = params.delete(:_errors) || {}

  # Use setter methods first, then translate attributes of relationships
  # into relationship instances, then merge the parsed_data into @data.
  unset_data = Her::Model::ORM.use_setter_methods(self, params)
  parsed_data = self.class.parse_relationships(unset_data)
  @data.update(convert_types(parsed_data))
end

#respond_to?(method, include_private = false) ⇒ Boolean

Handles returning true for the cases handled by method_missing

Returns:

  • (Boolean)


82
83
84
# File 'lib/her/model/orm.rb', line 82

def respond_to?(method, include_private = false)
  method.to_s.end_with?('=') || method.to_s.end_with?('?') || @data.include?(method) || super
end