Class: AlittleLess

Inherits:
Object show all
Includes:
Router, Util
Defined in:
lib/a_little_less.rb,
lib/a_little_less.rb,
lib/a_little_less/env.rb,
lib/a_little_less/rack_app.rb

Defined Under Namespace

Modules: Router, Util Classes: Env, RackApp

Constant Summary collapse

DB_CONF =
"config/db.yml"
@@controllers =

Routing

{}
@@alias_name_map =
{}
@@default_controller =
nil

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Router

#method, #search_route

Methods included from Util

#add_default_cors_headers, #error_422, #http_options?, #http_origin_allowed?, #not_found, #params, #redir_301, #send_file, #set_options_response, #status, #with

Constructor Details

#initialize(req) ⇒ AlittleLess

RackApp entry point



88
89
90
# File 'lib/a_little_less.rb', line 88

def initialize req
    @req = req
end

Class Method Details

.add_action(verb, action, block) ⇒ Object



75
76
77
# File 'lib/a_little_less.rb', line 75

def add_action verb, action, block
    controllers[ self.to_s.decamelize ][ verb ][ action.to_s ] = block
end

.alias_name(name) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/a_little_less.rb', line 68

def alias_name name
    name = name.to_s
    main_name = self.to_s.decamelize
    if name != main_name
        alias_name_map[name] = main_name
    end
end

.alias_name_mapObject



47
48
49
# File 'lib/a_little_less.rb', line 47

def alias_name_map
    @@alias_name_map
end

.controllersObject



44
45
46
# File 'lib/a_little_less.rb', line 44

def controllers
    @@controllers
end

.default_controllerObject



62
63
64
# File 'lib/a_little_less.rb', line 62

def default_controller
    @@default_controller = self.to_s.decamelize
end

.delete(action, &block) ⇒ Object



82
# File 'lib/a_little_less.rb', line 82

def delete  action, █ add_action __method__, action, block; end

.envObject



22
23
24
# File 'lib/a_little_less/env.rb', line 22

def self.env
    @all_env ||= Env.new ENV['ALL_ENV']
end

.get(action, &block) ⇒ Object



78
# File 'lib/a_little_less.rb', line 78

def get     action, █ add_action __method__, action, block; end

.get_default_controllerObject



65
66
67
# File 'lib/a_little_less.rb', line 65

def get_default_controller
    @@default_controller
end

.head(action, &block) ⇒ Object



83
# File 'lib/a_little_less.rb', line 83

def head    action, █ add_action __method__, action, block; end

.inherited(subclass) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/a_little_less.rb', line 50

def inherited subclass
    controllers[subclass.to_s.decamelize] = {
        get: {
            # action_name string => action proc
        },
        head: {}, # head is the same as get, but doesn`t return body
        post: {},
        patch: {},
        put: {},
        delete: {}
    }
end

.patch(action, &block) ⇒ Object



80
# File 'lib/a_little_less.rb', line 80

def patch   action, █ add_action __method__, action, block; end

.post(action, &block) ⇒ Object



79
# File 'lib/a_little_less.rb', line 79

def post    action, █ add_action __method__, action, block; end

.put(action, &block) ⇒ Object



81
# File 'lib/a_little_less.rb', line 81

def put     action, █ add_action __method__, action, block; end

.rack_appObject



166
167
168
# File 'lib/a_little_less/rack_app.rb', line 166

def self.rack_app
    RackApp
end

Instance Method Details

#conversation!Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/a_little_less.rb', line 92

def conversation!
    if http_options? and http_origin_allowed?
        set_options_response
        add_default_cors_headers
        return
    end

    if route = search_route
        # logi "route found: #{route.klass} #{route.action_proc}"
        action_please! route
        add_default_cors_headers
    else
        # logi 'route not found'
        not_found
    end
end