smooth_operator 0.3.12 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -52,31 +52,51 @@ module SmoothOperator
52
52
 
53
53
  private ######################### PRIVATE ###############################
54
54
 
55
- def get_nested_object_variable(nested_object_symbol, nested_object_class)
55
+ def get_nested_object_variable(nested_object_symbol, nested_object_class_or_options)
56
56
  nested_object_variable = instance_variable_get("@#{nested_object_symbol}")
57
57
 
58
58
  return nested_object_variable if nested_object_variable.present?
59
59
 
60
- nested_object_variable = initialize_nested_object_variable(table_hash[nested_object_symbol], nested_object_class, nested_object_symbol)
60
+ nested_object_variable = initialize_nested_object_variable(table_hash[nested_object_symbol], nested_object_class_or_options, nested_object_symbol)
61
61
 
62
62
  instance_variable_set("@#{nested_object_symbol}", nested_object_variable)
63
63
 
64
64
  nested_object_variable
65
65
  end
66
66
 
67
- def initialize_nested_object_variable(attributes, nested_object_class, nested_object_symbol)
68
- if attributes.kind_of? Array
69
- attributes.map { |_attributes| get_nested_object(_attributes, nested_object_class, nested_object_symbol) }
67
+ def initialize_nested_object_variable(attributes, nested_object_class_or_options, nested_object_symbol)
68
+ if attributes.kind_of?(Array)
69
+ get_nested_objects(attributes, nested_object_class_or_options, nested_object_symbol)
70
70
  else
71
- get_nested_object(attributes, nested_object_class, nested_object_symbol)
71
+ get_nested_object(attributes, nested_object_class_or_options, nested_object_symbol)
72
72
  end
73
73
  end
74
74
 
75
- def get_nested_object(nested_objects_attributes, nested_object_class, nested_object_symbol)
76
- if nested_objects_attributes.blank?
77
- plural?(nested_object_symbol) ? [] : nil
78
- else
79
- nested_object_class.new(nested_objects_attributes)
75
+ def get_nested_objects(nested_objects_attributes, nested_object_class_or_options, nested_object_symbol)
76
+ nested_objects = nested_objects_attributes.map { |attributes| get_nested_object(attributes, nested_object_class_or_options, nested_object_symbol) }
77
+
78
+ if nested_object_class_or_options.kind_of?(Hash)
79
+ nested_objects = order_by(nested_objects, nested_object_class_or_options[:order_by], nested_object_class_or_options[:order])
80
+ end
81
+
82
+ nested_objects
83
+ end
84
+
85
+ def get_nested_object(nested_objects_attributes, nested_object_class_or_options, nested_object_symbol)
86
+ return (plural?(nested_object_symbol) ? [] : nil) if nested_objects_attributes.blank?
87
+
88
+ return get_nested_object_according_to_options(nested_objects_attributes, nested_object_class_or_options, nested_object_symbol) if nested_object_class_or_options.kind_of?(Hash)
89
+
90
+ nested_object_class_or_options.new(nested_objects_attributes)
91
+ end
92
+
93
+ def get_nested_object_according_to_options(nested_objects_attributes, options, nested_object_symbol)
94
+ nested_objects_attributes = options[:parser].call(nested_objects_attributes) if options[:parser].present?
95
+
96
+ begin
97
+ options[:class].new(nested_objects_attributes)
98
+ rescue
99
+ options[:default].present? ? options[:default] : nested_objects_attributes
80
100
  end
81
101
  end
82
102
 
@@ -85,6 +105,16 @@ module SmoothOperator
85
105
  string == string.pluralize
86
106
  end
87
107
 
108
+ def order_by(list, method, order)
109
+ return list if method.blank? || order.blank?
110
+
111
+ if order == :asc
112
+ list.sort { |a, b| a.send(method) <=> b.send(method) }
113
+ elsif order == :desc
114
+ list.sort { |a, b| b.send(method) <=> a.send(method) }
115
+ end
116
+ end
117
+
88
118
 
89
119
  def self.included(base)
90
120
  base.extend(ClassMethods)
@@ -93,9 +123,9 @@ module SmoothOperator
93
123
  module ClassMethods
94
124
 
95
125
  def nested_objects_classes(hash)
96
- hash.each do |nested_object_symbol, nested_object_class|
126
+ hash.each do |nested_object_symbol, nested_object_class_or_options|
97
127
  define_method(nested_object_symbol.to_s) do
98
- get_nested_object_variable(nested_object_symbol, nested_object_class)
128
+ get_nested_object_variable(nested_object_symbol, nested_object_class_or_options)
99
129
  end
100
130
  end
101
131
  end
@@ -1,3 +1,3 @@
1
1
  module SmoothOperator
2
- VERSION = "0.3.12"
2
+ VERSION = "0.4.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smooth_operator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.12
4
+ version: 0.4.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-20 00:00:00.000000000 Z
12
+ date: 2013-10-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -94,3 +94,4 @@ specification_version: 3
94
94
  summary: Simple and fully customizable alternative to ActiveResource, based on httparty
95
95
  gem
96
96
  test_files: []
97
+ has_rdoc: