Class: Toast::ConfigDSL::Expose

Inherits:
Object
  • Object
show all
Includes:
Common, DefaultHandlers
Defined in:
lib/toast/config_dsl/expose.rb

Overview

context for expose{} blocks

Instance Method Summary collapse

Methods included from DefaultHandlers

#canonical_delete_handler, #canonical_get_handler, #canonical_patch_handler, #collection_get_handler, #collection_post_handler, #plural_assoc_get_handler, #plural_assoc_link_handler, #plural_assoc_post_handler, #plural_assoc_unlink_handler, #single_get_handler, #singular_assoc_get_handler, #singular_assoc_link_handler, #singular_assoc_unlink_handler

Methods included from Common

#check_symbol_list, #initialize, #method_missing, #raise_config_error, #stack_push

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Toast::ConfigDSL::Common

Instance Method Details

#association(name, as: 'application/json', &block) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/toast/config_dsl/expose.rb', line 137

def association name, as: 'application/json', &block
  stack_push "association(#{name.inspect})" do


    model_class = @config_data.model_class

    unless name.is_a?(Symbol)
      raise_config_error "association name expected as Symbol"
    end

    unless model_class.reflections[name.to_s].
             try(:macro).in?([:has_many,
                              :has_one,
                              :belongs_to,
                              :has_and_belongs_to_many])
      raise_config_error 'Association expected'
    end

    unless block_given?
      raise_config_error 'Block expected.'
    end

    target_model_class = model_class.reflections[name.to_s].klass
    macro    = model_class.reflections[name.to_s].macro
    singular = macro.in? [:belongs_to, :has_one]

    @config_data.associations[name] =
      OpenStruct.new(base_model_class: model_class,
                     target_model_class: target_model_class,
                     assoc_name: name,
                     media_type: as,
                     macro:      macro,
                     singular:   singular,
                     max_window: singular ? nil : Toast.settings.max_window)

    Toast::ConfigDSL::Association.new(@config_data.associations[name]).
      instance_eval(&block)
  end
end

#collection(name, as: 'application/json', &block) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/toast/config_dsl/expose.rb', line 89

def collection name, as: 'application/json', &block
  stack_push "collection(#{name.inspect})" do
    model_class = @config_data.model_class

    unless block_given?
      raise_config_error 'Block expected.'
    end

    unless name.is_a?(Symbol)
      raise_config_error "collection name expected as Symbol"
    end

    unless model_class.respond_to?(name)
      raise_config_error "`#{name}' must be a callable class method."
    end

    @config_data.collections[name] =
      OpenStruct.new(base_model_class: model_class,
                     collection_name: name,
                     media_type: as,
                     max_window: Toast.settings.max_window)

    Toast::ConfigDSL::Collection.new(@config_data.collections[name]).
      instance_eval(&block)

  end
end

#readables(*attributes) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/toast/config_dsl/expose.rb', line 36

def readables *attributes
  stack_push "readables(#{attributes.map(&:inspect).join(',')})" do

    check_symbol_list attributes

    model_class = @config_data.model_class

    attributes.each do |attr|
      model = model_class.new

      unless (model.respond_to?(attr) and model.method(attr).arity.in?([-1,0]))
        raise_config_error "Exposed attribute getter not found `#{model_class.name}##{attr}'. Typo?"
      end

      @config_data.readables << attr
    end
  end
end

#single(name, &block) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/toast/config_dsl/expose.rb', line 117

def single name, &block
  stack_push "single(#{name.inspect})" do

    model_class = @config_data.model_class

    unless model_class.respond_to?(name)
      raise_config_error "`#{name}' must be a callable class method."
    end

    unless block_given?
      raise_config_error 'Block expected.'
    end

    @config_data.singles[name] = OpenStruct.new(name: name, model_class: model_class)

    Toast::ConfigDSL::Single.new(@config_data.singles[name]).
      instance_eval(&block)
  end
end

#via_delete(&block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/toast/config_dsl/expose.rb', line 77

def via_delete &block
  stack_push 'via_delete' do
    @config_data.via_delete =
      OpenStruct.new(permissions: [],
                     handler: canonical_delete_handler)

    Toast::ConfigDSL::ViaVerb.new(@config_data.via_delete).instance_eval &block

  end
end

#via_get(&block) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/toast/config_dsl/expose.rb', line 55

def via_get &block
  stack_push 'via_get' do
    @config_data.via_get =
      OpenStruct.new(permissions: [],
                     handler: canonical_get_handler)

    Toast::ConfigDSL::ViaVerb.new(@config_data.via_get).instance_eval &block

  end
end

#via_patch(&block) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/toast/config_dsl/expose.rb', line 66

def via_patch &block
  stack_push 'via_patch' do
    @config_data.via_patch =
      OpenStruct.new(permissions: [],
                     handler: canonical_patch_handler)

    Toast::ConfigDSL::ViaVerb.new(@config_data.via_patch).instance_eval &block

  end
end

#writables(*attributes) ⇒ Object

directives



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/toast/config_dsl/expose.rb', line 11

def writables *attributes
  stack_push "writables(#{attributes.map(&:inspect).join(',')})" do

    check_symbol_list attributes

    model_class = @config_data.model_class

    attributes.each do |attr|

      model = model_class.new
      setter = (attr.to_s + '=').to_sym

      unless (model.respond_to?(setter) and model.method(setter).arity == 1)
        raise_config_error "Exposed attribute setter not found: `#{model_class.name}##{attr}='. Typo?"
      end

      unless (model.respond_to?(attr) and model.method(attr).arity.in?([-1,0]))
        raise_config_error "Exposed attribute getter not found `#{model_class.name}##{attr}'. Typo?"
      end

      @config_data.writables << attr
    end
  end
end