moonrope 1.2.4 → 1.2.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7a005ba55f00db5d481ac58a7160e591e993c39b
4
- data.tar.gz: 0189df9139aa0bf21c6a991592632478a2dbd769
3
+ metadata.gz: b3dec6b64d723aeb2e166a548bd1b0979e6f8639
4
+ data.tar.gz: 1578b8fcea65a8fa2a8021cf2e8d1ddb3b5db665
5
5
  SHA512:
6
- metadata.gz: f6d0eb2be42d7b122abf4abc8bfbe7575523511bef0215c669f4455e44768273cc4744fddc5da475f521307e516e684e42ddab466b20014faa0d62bdd5a978ab
7
- data.tar.gz: aa90abdd05744a1fd171a53f278d4b35bc5cdcbf647bfa343933538b7e2c4223617ad511a4bfbdcc5aa3aeb0ebc7a4a4da14be26f619b90317abb6e27a649557
6
+ metadata.gz: e78a8573273c0bc8a0f4f892bbef61142c0b391fd01909130952c606cfd11888974569751659ba4d016763e7f143de9f199f22b6a5f01941c4fd4f4816665c5b
7
+ data.tar.gz: 5829ec72fbbbcce330cf9666927a270b4351a4c9bf26c2bba5edaecbb0301d3b3d7a452af049cba0a1523593a97cd29655f99f47f43936d7fabdaee5a6903cbf
data/lib/moonrope/base.rb CHANGED
@@ -32,12 +32,15 @@ module Moonrope
32
32
  # @return [Proc] the default access condition
33
33
  attr_accessor :default_access
34
34
 
35
- # @return [String] the directory the base was loaded from (if relevant)
36
- attr_accessor :loaded_from
35
+ # @return [Array] the array of directories to load from (if relevant)
36
+ attr_accessor :load_directories
37
37
 
38
38
  # @return [String] the moonrope environment
39
39
  attr_accessor :environment
40
40
 
41
+ # @return [Proc] a proc to execute before every request
42
+ attr_accessor :on_request
43
+
41
44
  #
42
45
  # Initialize a new instance of the Moonrope::Base
43
46
  #
@@ -46,6 +49,7 @@ module Moonrope
46
49
  def initialize(&block)
47
50
  unload
48
51
  @environment = 'development'
52
+ @load_directories = []
49
53
  @dsl = Moonrope::DSL::BaseDSL.new(self)
50
54
  @dsl.instance_eval(&block) if block_given?
51
55
  end
@@ -64,14 +68,17 @@ module Moonrope
64
68
  #
65
69
  # Reload this whole base API from the path
66
70
  #
67
- def load(directory = nil)
68
- directory = self.loaded_from if directory.nil?
69
- if directory
71
+ def load(*directories)
72
+ directories = self.load_directories if directories.empty?
73
+ if directories.size > 0
70
74
  unload
71
- Dir["#{directory}/**/*.rb"].each do |filename|
72
- self.dsl.instance_eval(File.read(filename), filename)
75
+ new_directories = []
76
+ directories.each do |directory|
77
+ if load_directory(directory)
78
+ new_directories << directory
79
+ end
73
80
  end
74
- self.loaded_from = directory
81
+ self.load_directories = new_directories
75
82
  self
76
83
  else
77
84
  raise Moonrope::Errors::Error, "Can't reload Moonrope::Base as it wasn't required from a directory"
@@ -80,6 +87,32 @@ module Moonrope
80
87
 
81
88
  alias_method :reload, :load
82
89
 
90
+ #
91
+ # Load from a given directory
92
+ #
93
+ def load_directory(directory)
94
+ if File.exist?(directory)
95
+ Dir["#{directory}/**/*.rb"].each do |filename|
96
+ self.dsl.instance_eval(File.read(filename), filename)
97
+ end
98
+ true
99
+ else
100
+ false
101
+ end
102
+ end
103
+
104
+ #
105
+ # Add a dirctory to the directories to load
106
+ #
107
+ def add_load_directory(directory)
108
+ if load_directory(directory)
109
+ self.load_directories << directory unless self.load_directories.include?(directory)
110
+ true
111
+ else
112
+ false
113
+ end
114
+ end
115
+
83
116
  #
84
117
  # Return a structure of the given name
85
118
  #
@@ -18,9 +18,13 @@ module Moonrope
18
18
  # @yield instance evals the block within the StructureDSL
19
19
  #
20
20
  def structure(name, &block)
21
- structure = Moonrope::Structure.new(@base, name)
21
+ if existing = @base.structures.select { |s| s.name == name }.first
22
+ structure = existing
23
+ else
24
+ structure = Moonrope::Structure.new(@base, name)
25
+ @base.structures << structure
26
+ end
22
27
  structure.dsl.instance_eval(&block) if block_given?
23
- @base.structures << structure
24
28
  structure
25
29
  end
26
30
 
@@ -37,9 +41,9 @@ module Moonrope
37
41
  controller = existing
38
42
  else
39
43
  controller = Moonrope::Controller.new(@base, name)
44
+ @base.controllers << controller
40
45
  end
41
46
  controller.dsl.instance_eval(&block) if block_given?
42
- @base.controllers << controller
43
47
  controller
44
48
  end
45
49
 
@@ -15,6 +15,8 @@ module Moonrope
15
15
  @options = options
16
16
  end
17
17
 
18
+ attr_reader :base
19
+
18
20
  #
19
21
  # Make a new request
20
22
  #
@@ -28,6 +30,13 @@ module Moonrope
28
30
  @base.load
29
31
  end
30
32
 
33
+ #
34
+ # Call the on request block if one has been defined for the base.
35
+ #
36
+ if @base.on_request.is_a?(Proc)
37
+ @base.on_request.call(@base, env)
38
+ end
39
+
31
40
  #
32
41
  # Create a new request object
33
42
  #
@@ -3,21 +3,19 @@ module Moonrope
3
3
 
4
4
  initializer 'moonrope.initialize' do |app|
5
5
 
6
- # Initialize a new moonrope base from the API defined in
7
- # $RAILS_ROOT/app/api directory.
8
- moonrope_directory = Rails.root.join('api')
9
- if File.directory?(moonrope_directory)
10
- app.config.moonrope = Moonrope::Base.load(moonrope_directory)
11
- else
12
- $stderr.puts "Moonrope is installed but there is no API directory at ROOT/api."
13
- next
14
- end
15
-
6
+ # Initialize a new moonrope base.
7
+ app.config.moonrope = Moonrope::Base.load(Rails.root.join('api'))
8
+
16
9
  # Set the logger
17
10
  Moonrope.logger = Rails.logger
18
11
 
19
12
  # Set the environment to match the Rails environment
20
13
  app.config.moonrope.environment = Rails.env.to_s
14
+
15
+ # Ensure all request use UTC
16
+ app.config.moonrope.on_request = Proc.new do |base, env|
17
+ Time.zone = 'UTC'
18
+ end
21
19
 
22
20
  # Set the request regex if one has been set
23
21
  if app.config.respond_to?(:moonrope_request_path_regex) && app.config.moonrope_request_path_regex.is_a?(Regexp)
@@ -151,7 +151,7 @@ module Moonrope
151
151
  # hash value as appropriate.
152
152
  if structure = self.base.structure(attribute.structure)
153
153
  structure_opts = attribute.structure_opts || {}
154
- if value.is_a?(Array)
154
+ if value.respond_to?(:map)
155
155
  value.map do |v|
156
156
  structure.hash(v, structure_opts.merge(:request => environment.request))
157
157
  end
@@ -1,3 +1,3 @@
1
1
  module Moonrope
2
- VERSION = '1.2.4'
2
+ VERSION = '1.2.5'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moonrope
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.4
4
+ version: 1.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Cooke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-25 00:00:00.000000000 Z
11
+ date: 2014-09-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json