Class: Helioth::Dsl

Inherits:
Object
  • Object
show all
Defined in:
lib/helioth/dsl.rb

Constant Summary collapse

DSL_FILE =
Pathname.new(Rails.root || '').join("config", "helioth.rb").to_s

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDsl

Returns a new instance of Dsl.



6
7
8
# File 'lib/helioth/dsl.rb', line 6

def initialize
  Rails.logger.debug("Loading the DSL for the first time")
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

In case the DSL is not well used



20
21
22
# File 'lib/helioth/dsl.rb', line 20

def method_missing(method_name, *args, &block)
  raise "No such method #{__method__} in #{__FILE__}"
end

Class Method Details

.load(file = nil) ⇒ Object

Should be loaded only one time using Helioth::DSL.dsl



11
12
13
14
15
16
17
# File 'lib/helioth/dsl.rb', line 11

def self.load(file = nil)
  dsl = new
  dsl.instance_eval(file.nil? ? File.read(DSL_FILE) : File.read(file))
  return(dsl)
rescue Errno::ENOENT
  raise "Helioth::DSL DSL file missing"
end

Instance Method Details

#action(feature_name, action_name) ⇒ Object

Get feature action



51
52
53
54
55
# File 'lib/helioth/dsl.rb', line 51

def action(feature_name, action_name)
  feature(feature_name).actions.map{|action|
    action if action.name == action_name
  }.compact.first
end

#authorized_for_instance?(feature_name, *actions_name, role) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/helioth/dsl.rb', line 66

def authorized_for_instance?(feature_name, *actions_name, role)
  authorized_for(feature_name, actions_name.flatten, {role: role, type: :instance})
end

#authorized_for_locale?(feature_name, *actions_name, locale) ⇒ Boolean

Check authorization

Returns:

  • (Boolean)


58
59
60
# File 'lib/helioth/dsl.rb', line 58

def authorized_for_locale?(feature_name, *actions_name, locale)
  authorized_for(feature_name, actions_name.flatten, {locale: locale})
end

#authorized_for_user?(feature_name, *actions_name, role) ⇒ Boolean

Returns:

  • (Boolean)


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

def authorized_for_user?(feature_name, *actions_name, role)
  authorized_for(feature_name, actions_name.flatten, {role: role, type: :user})
end

#feature(feature_name) ⇒ Object

Get feature



44
45
46
47
48
# File 'lib/helioth/dsl.rb', line 44

def feature(feature_name)
  @features.list.map{|feature|
    feature if feature.name == feature_name
  }.compact.first
end

#features(&block) ⇒ Object

Configure features



35
36
37
38
39
40
41
# File 'lib/helioth/dsl.rb', line 35

def features(&block)
  if block
    @features ||= Features.new(&block)
  else
    @features.list
  end
end

#relations(&block) ⇒ Object

Configure relations



30
31
32
# File 'lib/helioth/dsl.rb', line 30

def relations(&block)
  @relations ||= Relation.new(&block)
end

#roles(&block) ⇒ Object

Configure roles



25
26
27
# File 'lib/helioth/dsl.rb', line 25

def roles(&block)
  @roles ||= Role.new(&block)
end