Advertisement
Guest User

Untitled

a guest
Mar 9th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. require 'json'
  2.  
  3. class Hash
  4. def try(*a, &b)
  5. if a.empty? && block_given?
  6. yield self
  7. else
  8. __send__(*a, &b)
  9. end
  10. end
  11. end
  12.  
  13. PARAMS = {}
  14.  
  15. module ParamsDSL
  16. def params(&block)
  17. parser = ParamsParser.new
  18. parser.instance_eval &block
  19. PARAMS[self.name] = parser.attributes
  20. end
  21.  
  22. def self.attributes
  23. @attributes
  24. # @parser.attributes
  25. end
  26. end
  27.  
  28. class ParamsParser
  29. attr_reader :attributes
  30.  
  31. def initialize
  32. @attributes = {}
  33. @parent = @attributes
  34. end
  35.  
  36. def method_missing(name, *args, &block)
  37. if ['optional', 'requires'].find(name)
  38. attrs = args[1]
  39. attrs[:requires] = true if name == :requires
  40. name = args[0]
  41.  
  42. old_parent = @parent
  43. @parent[name] = attrs
  44.  
  45. if block.nil?
  46. @parent = old_parent
  47. else
  48. @parent[name][:attributes] = {}
  49. @parent = @parent[name][:attributes]
  50. self.instance_eval &block
  51. @parent = old_parent
  52. end
  53. end
  54. end
  55. end
  56.  
  57. class BaseAction
  58. extend ParamsDSL
  59. end
  60.  
  61. class MyAction < BaseAction
  62. params do
  63. optional :id, type: Integer
  64. requires :title, type: String, default: 'demo'
  65. optional :user, type: Hash do
  66. requires :email, type: String
  67. requires :session, type: Hash do
  68. optional :id, type: Integer
  69. requires :token, type: String
  70. end
  71. optional :password, type: String
  72. end
  73. optional :desc, type: String
  74. requires :tags, type: Array do
  75. requires :id, type: Integer
  76. requires :tag, type: String
  77. end
  78. optional :numbers, type: Array
  79. optional :strings, type: Array, default: ['qwe', 'rty']
  80. end
  81. end
  82.  
  83. def declared_params(schema, params)
  84. result = {}
  85. schema.each do |key, value|
  86. params_value = params.try(:[], key)
  87. params_value = value[:default] if params_value.nil? and !value[:default].nil?
  88.  
  89. if value[:type] == Hash
  90. value = declared_params(value[:attributes], params_value)
  91. elsif value[:type] == Array
  92. if value[:attributes].nil?
  93. value = params_value
  94. else
  95. value = params_value.map do |_params_value|
  96. declared_params(value[:attributes], _params_value)
  97. end
  98.  
  99. end
  100. else
  101. value = params_value
  102. end
  103.  
  104. result[key] = value unless value.nil?
  105. end
  106. result
  107. end
  108.  
  109. schema = PARAMS[MyAction.name]
  110. params = {
  111. id: 1,
  112. content: 'qweqweqwe',
  113. user: {
  114. id: 1,
  115. phone_number: '79123456789',
  116. password: '123123',
  117. session: {
  118. token: 'hey',
  119. qwe: 123,
  120. },
  121. },
  122. tags: [
  123. { id: 1, tag: 'qwe' },
  124. { id: 2, tag: 'try', abc: 123},
  125. ],
  126. numbers: [1,2,3,4,5],
  127. }
  128.  
  129. declared = declared_params(schema, params)
  130. puts JSON.pretty_generate(declared)
  131.  
  132. # output:
  133. #
  134. # {
  135. # "id": 1,
  136. # "title": "demo",
  137. # "user": {
  138. # "session": {
  139. # "token": "hey"
  140. # },
  141. # "password": "123123"
  142. # },
  143. # "tags": [
  144. # {
  145. # "id": 1,
  146. # "tag": "qwe"
  147. # },
  148. # {
  149. # "id": 2,
  150. # "tag": "try"
  151. # }
  152. # ],
  153. # "numbers": [
  154. # 1,
  155. # 2,
  156. # 3,
  157. # 4,
  158. # 5
  159. # ],
  160. # "strings": [
  161. # "qwe",
  162. # "rty"
  163. # ]
  164. # }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement