brujula 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 627bde01017a2c5a99bb77c5888f7658675204c5
4
- data.tar.gz: 09b0aa8cd8ef8373365e39215c72ac2caadaee6b
3
+ metadata.gz: 728af83883cc6088ff24fc157a824221a04668fe
4
+ data.tar.gz: bb27ac407bb64ef0840073ad168cc1abb30e0184
5
5
  SHA512:
6
- metadata.gz: 56b5d1e7b3f629efc13cd78921795dd1d0ffd30383b42d209b62258453a12ba1c708662f6853196d9bbe7be6ae6e301b41f5f256fe57d85e6870f68a99ec690e
7
- data.tar.gz: 3ab91bd63e736f10fae5fdd6e5c116dd177cf56a9a270c1b3e2da214f517b3a338db8adbd2f4ff09901d8926650eb4b1bb47d62a9b5e8f59997d3bb208132dab
6
+ metadata.gz: 146647cbd16918d460e3c036b8c886940488fddf4bc6cd9e2edddad732bc61612320fd925fe5c103eb4505b5103ebba629e753cd419d3d524d07d014a631b225
7
+ data.tar.gz: c531af198a63ffbeb311213b7cdb12c9e494b3332e5f8adefc1a158c91e056b6717b866c7fcf29a700f2b8cde6a359099de9e263934de249f53d48da1d3a6ac5
data/README.md CHANGED
@@ -3,7 +3,7 @@ Brújula
3
3
 
4
4
  [![Build Status](https://travis-ci.org/nogates/brujula.svg?branch=master)](https://travis-ci.org/nogates/brujula)
5
5
  [![Code Climate](https://codeclimate.com/github/nogates/brujula/badges/gpa.svg)](https://codeclimate.com/github/nogates/brujula)
6
- [![Test Coverage](https://codeclimate.com/github/nogates/brujula/badges/coverage.svg)](https://codeclimate.com/github/nogates/brujula)
6
+ [![Test Coverage](https://codeclimate.com/github/nogates/brujula/badges/coverage.svg)](https://codeclimate.com/github/nogates/brujula/coverage)
7
7
 
8
8
  _Yet another Ruby RAML parser aiming for version 1.0 fully support_
9
9
 
@@ -18,11 +18,11 @@ Install the gem `brujula` via your preferred method, and use `parse_file` to loa
18
18
  ```ruby
19
19
  require 'brujula'
20
20
 
21
- root = Brujula.parse_file('path/my_example_api.raml')
21
+ raml = Brujula.parse_file('path/my_example_api.raml')
22
22
 
23
23
  # Alternatively
24
24
 
25
- raml = <<-RAML
25
+ raml_string = <<-RAML
26
26
  # version 1.0
27
27
  title: 'My example API'
28
28
  /examples:
@@ -30,20 +30,20 @@ title: 'My example API'
30
30
 
31
31
  RAML
32
32
 
33
- root = Brujula.parse(raml)
33
+ raml = Brujula.parse(raml_string)
34
34
  ```
35
35
 
36
36
  As expected, the properties of the root object can be accessed from the root object
37
37
 
38
38
  ```ruby
39
- root.title
39
+ raml.root.title
40
40
  => 'My example API'
41
41
  ```
42
42
 
43
43
  Object collections, such as Resources, Methods or Security Schemes, inherits from `Brujula::MapObject`, which implements the `Enumerable` module. Thus, you can use any of this module's method to get collection objects.
44
44
 
45
45
  ```ruby
46
- root.resources.each_with_object([]) do |resource, resource_names|
46
+ raml.root.resources.each_with_object([]) do |resource, resource_names|
47
47
  resource_names << resource.name
48
48
  end
49
49
  ```
@@ -51,7 +51,7 @@ end
51
51
  Alternatively, you can you use hash methods such as `[]` or `fetch` to get specific objects by its key name
52
52
 
53
53
  ```ruby
54
- root.resources['/examples']
54
+ raml.root.resources['/examples']
55
55
  ```
56
56
 
57
57
 
@@ -61,4 +61,24 @@ Pull request are kindly requested. Specially in the form of RAML examples. If yo
61
61
 
62
62
  ## License
63
63
 
64
- See [LICENSE](https://raw.githubusercontent.com/lonelyplanet/brujula/master/LICENSE).
64
+ The MIT License (MIT)
65
+
66
+ Copyright (c) 2014 David
67
+
68
+ Permission is hereby granted, free of charge, to any person obtaining a copy
69
+ of this software and associated documentation files (the "Software"), to deal
70
+ in the Software without restriction, including without limitation the rights
71
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
72
+ copies of the Software, and to permit persons to whom the Software is
73
+ furnished to do so, subject to the following conditions:
74
+
75
+ The above copyright notice and this permission notice shall be included in all
76
+ copies or substantial portions of the Software.
77
+
78
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
79
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
80
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
81
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
82
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
83
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
84
+ SOFTWARE.
data/SPEC_SUPPORT.md CHANGED
@@ -139,10 +139,10 @@ Feature | status | comments
139
139
  ---|---|---
140
140
  Query Parameters, Responses, and Headers collection mergin | :white_check_mark: |
141
141
  usage description | :white_check_mark: |
142
- Optional properties (`/^?/`) | :warning: | Needs testing and real examples
143
- Algorithm of Merging Traits With Methods | :warning: | Needs testing and real examples
144
- Parameters (both custom and reserved parameters) | :x: |
145
- Modifier functions | :x: |
142
+ Optional properties (`/^?/`) | :white_check_mark: :warning: | Needs testing and real examples
143
+ Algorithm of Merging Traits With Methods | :white_check_mark: :warning: | Needs testing and real examples
144
+ Parameters (both custom and reserved parameters) | :white_check_mark: |
145
+ Modifier functions | :white_check_mark: |
146
146
 
147
147
 
148
148
  * [Security Schemes](https://github.com/raml-org/raml-spec/blob/raml-10-rc2/versions/raml-10/raml-10.md#security-schemes)
data/lib/brujula.rb CHANGED
@@ -6,6 +6,7 @@ require_relative 'brujula/initializers/yaml_include'
6
6
 
7
7
  require_relative 'brujula/object_parser'
8
8
  require_relative 'brujula/object_builder'
9
+ require_relative 'brujula/referrer'
9
10
  require_relative 'brujula/scheme'
10
11
  require_relative 'brujula/raml'
11
12
  require_relative 'brujula/object'
@@ -14,6 +15,11 @@ require_relative 'brujula/key'
14
15
  require_relative 'brujula/basic_type'
15
16
  require_relative 'brujula/raml/exceptions'
16
17
 
18
+ # parameters for trait and resource types
19
+ require_relative 'brujula/parameters/transformer'
20
+ require_relative 'brujula/parameters/parser'
21
+ require_relative 'brujula/parameters/builder'
22
+
17
23
  # data transformers
18
24
  require_relative 'brujula/data_transformers/property_declaration'
19
25
  require_relative 'brujula/data_transformers/security_scheme_settings_declaration'
@@ -11,17 +11,20 @@ module Brujula
11
11
  def call
12
12
  case
13
13
  when superinstance.is_a?(Brujula::MapObject)
14
- MapObjectMerger.new(
15
- instance: instance, superinstance: superinstance
16
- ).call
14
+ MapObjectMerger.new(merger_options).call
17
15
  when superinstance.is_a?(Brujula::Object)
18
- ObjectMerger.new(
19
- instance: instance, superinstance: superinstance
20
- ).call
16
+ ObjectMerger.new(merger_options).call
21
17
  else
22
18
  superinstance.dup
23
19
  end
24
20
  end
21
+
22
+ def merger_options
23
+ {
24
+ instance: instance,
25
+ superinstance: superinstance
26
+ }
27
+ end
25
28
  end
26
29
  end
27
30
  end
@@ -13,10 +13,10 @@ module Brujula
13
13
  instance.dup.tap do |object|
14
14
  each_inheritable_attributes do |name, attribute|
15
15
  original_item = object.instance_variable_get("@#{ name }")
16
- thing = Merger.new(
16
+ merged_item = Merger.new(
17
17
  instance: original_item, superinstance: attribute
18
18
  ).call
19
- object.instance_variable_set("@#{ name }", thing)
19
+ object.instance_variable_set("@#{ name }", merged_item)
20
20
  end
21
21
  end
22
22
  end
@@ -11,8 +11,7 @@ module Brujula
11
11
 
12
12
  def call
13
13
  return nil if key_data.nil?
14
- return object_reference if is_object_reference?
15
- return map_object_reference if is_map_object_reference?
14
+ return object_reference if key.referrable
16
15
 
17
16
  object_class.new(new_object_arguments).expand
18
17
  end
@@ -41,48 +40,16 @@ module Brujula
41
40
  end
42
41
  end
43
42
 
44
- def ramelize_name
45
- Inflecto.ramelize(key.expression.to_s)
46
- end
47
-
48
- def is_object_reference?
49
- key.referrable? &&
50
- key_data.is_a?(String) &&
51
- object_class < Brujula::Object
52
- end
53
-
54
- def is_map_object_reference?
55
- key.referrable? &&
56
- key_data.is_a?(Array) &&
57
- object_class == Brujula::MapObject
58
- end
59
-
60
43
  def object_reference
61
- case
62
- when object_class == Brujula::Raml::V1_0::ResourceType
63
- definition.root.resource_types.fetch(key_data)
64
- else
65
- raise "Invalid reference"
66
- end
44
+ @object_reference ||= Brujula::Referrer.new(
45
+ name: key.name,
46
+ data: key_data,
47
+ definition: definition
48
+ ).call
67
49
  end
68
50
 
69
- def map_object_reference
70
- case
71
- when key.expression == :secured_by
72
- key_data.map do |security_scheme_ref|
73
- if security_scheme_ref.nil? # NullSecuritySchema
74
- Brujula::Raml::V1_0::NullSecuritySchema.new
75
- else
76
- definition.root.security_schemes.fetch(security_scheme_ref)
77
- end
78
- end
79
- when key.expression == :is
80
- key_data.map do |trait_ref|
81
- definition.root.traits.fetch(trait_ref)
82
- end
83
- else
84
- raise "Invalid reference"
85
- end
51
+ def ramelize_name
52
+ Inflecto.ramelize(key.expression.to_s)
86
53
  end
87
54
 
88
55
  def object_class
@@ -0,0 +1,51 @@
1
+ module Brujula
2
+ module Parameters
3
+ class Builder
4
+
5
+ attr_reader :object
6
+
7
+ def initialize(object:)
8
+ @object = object
9
+ end
10
+
11
+ def resource_params(resource)
12
+ object_parameters.merge(resource_parameters(resource))
13
+ end
14
+
15
+ def method_params(resource, method)
16
+ object_parameters.merge(
17
+ resource_parameters(resource)
18
+ ).merge(
19
+ method_parameters(method)
20
+ )
21
+ end
22
+
23
+ private
24
+
25
+ def object_parameters
26
+ object.instance_variable_get("@parameters") || {}
27
+ end
28
+
29
+ def method_parameters(method)
30
+ {
31
+ "methodName" => method.name,
32
+ }
33
+ end
34
+
35
+ def resource_parameters(resource)
36
+ {
37
+ "resourcePath" => resource_path(resource),
38
+ "resourcePathName" => resource_path_name(resource)
39
+ }
40
+ end
41
+
42
+ def resource_path(resource)
43
+ resource.name
44
+ end
45
+
46
+ def resource_path_name(resource)
47
+ resource.name.split('/').last.gsub(/[\{\}]/, "")
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,72 @@
1
+ module Brujula
2
+ module Parameters
3
+ class Parser
4
+ attr_reader :object, :parameters
5
+
6
+ def initialize(object:, parameters:)
7
+ @object = object
8
+ @parameters = parameters
9
+ end
10
+
11
+ def call
12
+ parametize(object)
13
+ end
14
+
15
+ private
16
+
17
+ def parametize(extend_object)
18
+ extend_object.dup.tap do |definition|
19
+ applicable_attributes(definition).each do |attribute|
20
+ property = definition.instance_variable_get(attribute)
21
+ extended_property = parse_attribute(property)
22
+
23
+ definition.instance_variable_set(attribute, extended_property)
24
+ end
25
+ end
26
+ end
27
+
28
+ def parse_attribute(definition)
29
+ case
30
+ when definition.is_a?(Brujula::Object)
31
+ parametize(definition)
32
+ when definition.is_a?(Brujula::MapObject)
33
+ parametize_map_object(definition)
34
+ when definition.is_a?(::String)
35
+ parametize_string(definition)
36
+ else
37
+ definition
38
+ end
39
+ end
40
+
41
+ def parametize_map_object(definition)
42
+ definition.dup.tap do |map_object|
43
+ items = map_object.instance_variable_get("@collection")
44
+ items = items.each_with_object({}) do |(name, item), collection|
45
+ collection.merge!(parametize_string(name) => parametize(item))
46
+ end
47
+ map_object.instance_variable_set("@collection", items)
48
+ end
49
+ end
50
+
51
+ def parametize_string(string)
52
+ string.gsub(/<<(.*?)>>/) do |expression|
53
+ parameter_transformer.call(expression)
54
+ end
55
+ end
56
+
57
+ def parameter_transformer
58
+ @parameter_transformer = Brujula::Parameters::Transformer.new(
59
+ parameters: parameters
60
+ )
61
+ end
62
+
63
+ def applicable_attributes(object)
64
+ object.instance_variables - excluded_attributes
65
+ end
66
+
67
+ def excluded_attributes
68
+ %i( @parent @parameters )
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,92 @@
1
+ module Brujula
2
+ module Parameters
3
+ class Transformer
4
+
5
+ attr_reader :parameters
6
+
7
+ def initialize(parameters:)
8
+ @parameters = parameters
9
+ end
10
+
11
+ def call(string)
12
+ content = string.match(/<<(.*?)>>/)[1]
13
+
14
+ apply_functions(content)
15
+ end
16
+
17
+ private
18
+
19
+ def apply_functions(string)
20
+ value, function_names = extract_functions(string)
21
+
22
+ return value if function_names.empty?
23
+
24
+ function_names.inject(value) do |result, function|
25
+ call_function(result, function)
26
+ end
27
+ end
28
+
29
+ def call_function(value, function)
30
+ function_ref = get_function_name(function)
31
+
32
+ send function_ref, value
33
+ end
34
+
35
+ def extract_functions(string)
36
+ expression_chain = string.split('|')
37
+ parameter_name = expression_chain.first.strip
38
+ value = parameters.fetch(parameter_name)
39
+
40
+ [ value, expression_chain[1..-1].map(&:strip) ]
41
+ end
42
+
43
+ def get_function_name(function)
44
+ name = Inflecto.underscore(function).gsub(/^!/, '').to_sym
45
+
46
+ return name if private_methods.include?(name)
47
+
48
+ raise "Invalid trasformer function #{ function }"
49
+ end
50
+
51
+ def singularize(string)
52
+ Inflecto.singularize(string)
53
+ end
54
+
55
+ def pluralize(string)
56
+ Inflecto.pluralize(string)
57
+ end
58
+
59
+ def uppercase(string)
60
+ string.upcase
61
+ end
62
+
63
+ def lowercase(string)
64
+ string.downcase
65
+ end
66
+
67
+ def lowercamelcase(string)
68
+ Inflecto.ramelize(string)
69
+ end
70
+
71
+ def uppercamelcase(string)
72
+ Inflecto.camelize(string)
73
+ end
74
+
75
+ def lowerunderscorecase(string)
76
+ Inflecto.underscore(string)
77
+ end
78
+
79
+ def upperunderscorecase(string)
80
+ Inflecto.underscore(string).upcase
81
+ end
82
+
83
+ def lowerhyphencase(string)
84
+ Inflecto.dasherize(Inflecto.underscore(string)).downcase
85
+ end
86
+
87
+ def upperhyphencase(string)
88
+ Inflecto.dasherize(Inflecto.underscore(string)).upcase
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,71 @@
1
+ module Brujula
2
+ class Referrer
3
+
4
+ attr_reader :name, :definition, :data
5
+
6
+ def initialize(name:, definition:, data:)
7
+ @name = name
8
+ @definition = definition
9
+ @data = data
10
+ end
11
+
12
+ def call
13
+ object_to_reference(data)
14
+ end
15
+
16
+ private
17
+
18
+ def object_to_reference(reference)
19
+ case reference
20
+ when String
21
+ string_reference(reference)
22
+ when Array
23
+ map_object_reference(reference)
24
+ when Hash
25
+ object_reference(reference)
26
+ when NilClass
27
+ nil_reference(reference)
28
+ else
29
+ raise "Invalid reference"
30
+ end
31
+ end
32
+
33
+ def map_object_reference(reference)
34
+ data.each_with_object([]) do |reference, collection|
35
+ collection << object_to_reference(reference)
36
+ end
37
+ end
38
+
39
+ def object_reference(reference_hash) # TODO validate parameters name
40
+ reference, params = reference_hash.first
41
+
42
+ string_reference(reference).tap do |object|
43
+ parameters = object.instance_variable_get("@parameters") || {}
44
+ parameters.merge!(params)
45
+ object.instance_variable_set("@parameters", parameters)
46
+ end
47
+ end
48
+
49
+ def nil_reference(reference)
50
+ case name
51
+ when "secured_by"
52
+ Brujula::Raml::V1_0::NullSecuritySchema.new
53
+ else
54
+ raise "The reference from key #{ key } cannot be nil"
55
+ end
56
+ end
57
+
58
+ def string_reference(reference)
59
+ case name
60
+ when "secured_by"
61
+ definition.root.security_schemes.fetch(reference)
62
+ when "type"
63
+ definition.root.resource_types.fetch(reference)
64
+ when "is"
65
+ definition.root.traits.fetch(reference)
66
+ else
67
+ raise "Invalid reference"
68
+ end.dup
69
+ end
70
+ end
71
+ end
File without changes
@@ -1,6 +1,7 @@
1
1
  module Brujula
2
2
  module TypeExtender
3
3
  class Method
4
+
4
5
  attr_reader :definition
5
6
 
6
7
  def initialize(definition:)
@@ -20,10 +21,27 @@ module Brujula
20
21
  def apply_inherit_chain
21
22
  definition.is.inject(definition.dup) do |object, trait|
22
23
  Brujula::Mergers::ObjectMerger.new(
23
- superinstance: trait, instance: object
24
+ superinstance: parametized_trait(trait), instance: object
24
25
  ).call
25
26
  end
26
27
  end
28
+
29
+ def parametized_trait(trait)
30
+ Brujula::Parameters::Parser.new(
31
+ object: trait,
32
+ parameters: parameter_builder(trait).method_params(resource, definition)
33
+ ).call
34
+ end
35
+
36
+ def parameter_builder(object)
37
+ Brujula::Parameters::Builder.new(
38
+ object: object
39
+ )
40
+ end
41
+
42
+ def resource
43
+ definition.parent.parent
44
+ end
27
45
  end
28
46
  end
29
47
  end
@@ -26,7 +26,8 @@ module Brujula
26
26
 
27
27
  def apply_type(object)
28
28
  Brujula::Mergers::ObjectMerger.new(
29
- superinstance: definition.type, instance: object
29
+ superinstance: parametized_type,
30
+ instance: object,
30
31
  ).call
31
32
  end
32
33
 
@@ -43,10 +44,30 @@ module Brujula
43
44
  def apply_traits_to_method(method)
44
45
  definition.is.inject(method.dup) do |object, trait|
45
46
  Brujula::Mergers::ObjectMerger.new(
46
- superinstance: trait, instance: object
47
+ superinstance: parametized_trait(trait, object), instance: object
47
48
  ).call
48
49
  end
49
50
  end
51
+
52
+ def parametized_trait(trait, method)
53
+ Brujula::Parameters::Parser.new(
54
+ object: trait,
55
+ parameters: parameter_builder(trait).method_params(definition, method)
56
+ ).call
57
+ end
58
+
59
+ def parametized_type
60
+ Brujula::Parameters::Parser.new(
61
+ object: definition.type,
62
+ parameters: parameter_builder(definition.type).resource_params(definition)
63
+ ).call
64
+ end
65
+
66
+ def parameter_builder(object)
67
+ Brujula::Parameters::Builder.new(
68
+ object: object
69
+ )
70
+ end
50
71
  end
51
72
  end
52
73
  end
@@ -1,3 +1,3 @@
1
1
  module Brujula
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brujula
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Tapiador
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-21 00:00:00.000000000 Z
11
+ date: 2016-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: inflecto
@@ -164,6 +164,9 @@ files:
164
164
  - lib/brujula/object.rb
165
165
  - lib/brujula/object_builder.rb
166
166
  - lib/brujula/object_parser.rb
167
+ - lib/brujula/parameters/builder.rb
168
+ - lib/brujula/parameters/parser.rb
169
+ - lib/brujula/parameters/transformer.rb
167
170
  - lib/brujula/raml.rb
168
171
  - lib/brujula/raml/definition.rb
169
172
  - lib/brujula/raml/exceptions.rb
@@ -190,7 +193,9 @@ files:
190
193
  - lib/brujula/raml/v1_0/trait.rb
191
194
  - lib/brujula/raml/v1_0/uri_parameter.rb
192
195
  - lib/brujula/raml/v1_0/uri_template.rb
196
+ - lib/brujula/referrer.rb
193
197
  - lib/brujula/scheme.rb
198
+ - lib/brujula/transformer_functions.rb
194
199
  - lib/brujula/type_extender/method.rb
195
200
  - lib/brujula/type_extender/resource.rb
196
201
  - lib/brujula/type_extender/resource_type.rb