Class: Moonrope::Structure

Inherits:
Object
  • Object
show all
Defined in:
lib/moonrope/structure.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, name) { ... } ⇒ Structure

Initialize a new structure

Parameters:

Yields:

  • instance evals the contents within the structure DSL



36
37
38
39
40
41
42
43
# File 'lib/moonrope/structure.rb', line 36

def initialize(base, name, &block)
  @base = base
  @name = name
  @expansions = {}
  @attributes = {:basic => [], :full => [], :expansion => []}
  @dsl = Moonrope::DSL::StructureDSL.new(self)
  @dsl.instance_eval(&block) if block_given?
end

Instance Attribute Details

#attributesHash (readonly)

Returns attributes which should be included in this structure.

Returns:

  • (Hash)

    attributes which should be included in this structure



25
26
27
# File 'lib/moonrope/structure.rb', line 25

def attributes
  @attributes
end

#baseMoonrope::Base (readonly)

Returns the base API.

Returns:



22
23
24
# File 'lib/moonrope/structure.rb', line 22

def base
  @base
end

#basicProc

Returns the basic data block.

Returns:

  • (Proc)

    the basic data block



10
11
12
# File 'lib/moonrope/structure.rb', line 10

def basic
  @basic
end

#docBool

Returns should this structure be documented.

Returns:

  • (Bool)

    should this structure be documented



28
29
30
# File 'lib/moonrope/structure.rb', line 28

def doc
  @doc
end

#dslMoonrope::DSL::StructureDSL (readonly)

Returns the DSL.

Returns:



16
17
18
# File 'lib/moonrope/structure.rb', line 16

def dsl
  @dsl
end

#expansionsHash (readonly)

Returns all expansions for the structure.

Returns:

  • (Hash)

    all expansions for the structure



19
20
21
# File 'lib/moonrope/structure.rb', line 19

def expansions
  @expansions
end

#fullProc

Returns the full data block.

Returns:

  • (Proc)

    the full data block



13
14
15
# File 'lib/moonrope/structure.rb', line 13

def full
  @full
end

#nameSymbol

Returns the name of the structure.

Returns:

  • (Symbol)

    the name of the structure



7
8
9
# File 'lib/moonrope/structure.rb', line 7

def name
  @name
end

Instance Method Details

#all_expansionsObject

Return an array of all expansions which are available on this structure



128
129
130
# File 'lib/moonrope/structure.rb', line 128

def all_expansions
  @attributes[:expansion].map(&:name) + expansions.keys
end

#attribute(name) ⇒ Object

Return details for the given attribute



48
49
50
51
52
# File 'lib/moonrope/structure.rb', line 48

def attribute(name)
  @attributes[:basic].select { |p| p.name == name }.first ||
  @attributes[:full].select { |p| p.name == name }.first ||
  @attributes[:expansion].select { |p| p.name == name }.first
end

#description_for_condition(condition) ⇒ Object

Return the description for a given condition hash



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/moonrope/structure.rb', line 135

def description_for_condition(condition)
  if condition[:authenticator] && condition[:access_rule]
    if authenticator = base.authenticators[condition[:authenticator]]
      if access_rule = authenticator.rules[condition[:access_rule]]
        access_rule[:description]
      end
    end
  else
    condition[:description]
  end
end

#hash(object, options = {}) ⇒ Hash

Return a hash for this struture

Parameters:

  • object (Object)

    the object

  • options (Hash) (defaults to: {})

    additional options

Returns:

  • (Hash)


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
116
117
118
119
120
121
122
123
# File 'lib/moonrope/structure.rb', line 61

def hash(object, options = {})
  # Set up an environment
  environment = EvalEnvironment.new(base, options[:request], options[:request] ? options[:request].action : nil, :o => object)

  # Set a new hash
  hash = Hash.new

  # Add the 'basic' structured fields
  DeepMerge.deep_merge! hash_for_attributes(@attributes[:basic], object, environment), hash

  # Always get a basic hash to work from
  if self.basic.is_a?(Proc)
    DeepMerge.deep_merge! environment.instance_eval(&self.basic), hash
  end

  # Enhance with the full hash if requested
  if options[:full]

    # Add the 'full' structured fields
    DeepMerge.deep_merge! hash_for_attributes(@attributes[:full], object, environment), hash

    if self.full.is_a?(Proc)
      full_hash = environment.instance_eval(&self.full)
      DeepMerge.deep_merge! full_hash,hash
    end
  end

  if options[:attributes]
    hash.reject! { |k,v| !options[:attributes].include?(k.to_sym) }
  end

  # Add expansions
  if options[:expansions]

    if options[:expansions].is_a?(Array)
      expansions_to_include = options[:expansions].each_with_object({}) do |expan, hash|
        if expan.is_a?(Symbol) || expan.is_a?(String)
          hash[expan.to_sym] = nil
        elsif expan.is_a?(Hash)
          hash[expan.first.first.to_sym] = expan.first.last
        end
      end
    else
      expansions_to_include = true
    end

    # Add structured expansions
    @attributes[:expansion].each do |attribute|
      next if expansions_to_include.is_a?(Hash) && !expansions_to_include.keys.include?(attribute.name.to_sym)
      DeepMerge.deep_merge! hash_for_attributes([attribute], object, environment, :structure_opts => expansions_to_include.is_a?(Hash) && expansions_to_include[attribute.name.to_sym]), hash
    end

    # Add the expansions
    expansions.each do |name, expansion|
      next if options[:expansions].is_a?(Array) && !options[:expansions].include?(name.to_sym)
      next unless check_conditions(environment, expansion[:conditions])
      DeepMerge.deep_merge!({name.to_sym => environment.instance_eval(&expansion[:block])}, hash)
    end
  end

  # Return the hash
  hash
end